Working with Docker Containers Made Easy with the dexec Bash Script

One of the powerful features Docker offers is the ability to interact with running containers. However, manually entering the necessary commands to access a container can be clunky. This motivated me to write this simple dexec script to make running commands through my Docker containers less clunky. I’ll show you what dexec is and how to use it!

Understanding the Purpose of dexec

The dexec script serves as a quick command to get a shell inside a running Docker container. It streamlines the process of accessing containers, eliminating the need to remember complex Docker commands. After providing the container name and an optional command, dexec automates the steps required to interact with a specific container, making development and debugging tasks more efficient.

Exploring the Script

Here’s the script I currently use.

The command works by searching for the container name you pass the script as the first argument. Then, based on if a second argument is passed, the script can run different code paths.

The default path is when no command is passed in, you’ll get an interactive shell inside your container. This is helpful when you want to explore the file system.

If you pass in a custom command, the script will try to execute it within the context of your container and return. A use case for this is running test suites or to generating a migration file.

The final path is when “logs” is passed in. This will dump the container logs, even if the container is no longer running. I found this useful for figuring out why a container had an early exit.

Installation

Getting this script to work is fairly straightforward if you are on a UNIX-like OS. You’ll just need to:

  1. Download the script.
  2. Run chmod +x on the script to make it executable.
  3. Relocate the script so that it is part of your PATH. /usr/local/bin/ is a common option.
  4. That’s it!

Simplify with dexec

The dexec bash script simplifies the process of accessing and interacting with Docker containers. When provided with a container name and an optional command, the script automates the steps required to execute commands within a running container. This automation saves time and effort, especially in development and debugging scenarios.

Conversation
  • Praveen says:

    A React course is a training program that teaches you how to use React, a JavaScript library for building user interfaces. React is one of the most popular JavaScript libraries in the world, and it is used by many large companies, such as Facebook, Airbnb, and Netflix.

    A React course typically covers the following topics:

    The basics of React, including components, state, and props
    Advanced React topics, such as routing, Redux, and testing
    How to build real-world applications with React

  • Join the conversation

    Your email address will not be published. Required fields are marked *