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
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.
Getting this script to work is fairly straightforward if you are on a UNIX-like OS. You’ll just need to:
- Download the script.
chmod +xon the script to make it executable.
- Relocate the script so that it is part of your
/usr/local/bin/is a common option.
- That’s it!
Simplify with dexec
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.