How to use docker exec
Jonas ScholzLet's get into how to use the docker exec command. This tool is super handy when you want to run a new command inside a container that's already up and running.
Getting Started with docker exec
First off, you'll need a running container. Let's say we've got a container named mycontainer that we started from an alpine image. We kicked it off with:
docker run -d -it --name mycontainer alpine sh
This command starts the container in the background (-d), keeps the standard input open (-i), and attaches a pseudo-TTY (-t) so we can interact with it later.
Running Commands Inside the Container
Now, let's use docker exec to do something inside mycontainer. Suppose we want to create a new file called execWorks in the /tmp directory of our container. We can do this with:
docker exec -d mycontainer touch /tmp/execWorks
Here, -d means the command runs in the background, detached from our terminal. The touch command creates the file.
If you want to jump into an interactive shell session inside the container, you can do that too:
docker exec -it mycontainer sh
This command starts a new shell session (sh) inside mycontainer. The -i keeps the input open, and -t gives you a terminal-like interface.
Setting Environment Variables
Sometimes, you might need to set specific environment variables for the command you're running. docker exec lets you do that with the --env or -e option. For example, to start a shell session with custom environment variables:
docker exec -it -e VAR_A=1 -e VAR_B=2 mycontainer sh
In this session, VAR_A will be set to 1 and VAR_B to 2. These variables are only available for this particular sh session and won't affect other processes in the container.
Changing the Working Directory
By default, docker exec runs commands in the container's default working directory. If you need to run a command in a different directory, use the --workdir or -w option:
docker exec -it -w /tmp mycontainer sh
This command starts a shell session in the /tmp directory of mycontainer.
Dealing with Privileges
If you need to run a command with escalated privileges, you can use the --privileged option. This gives the command more permissions inside the container, similar to using --privileged with docker run.
What Happens if the Container is Paused?
If you try to run docker exec on a container that's paused, you'll get an error. The command won't work because the container's processes are temporarily stopped.
A Few Things to Remember
- The command you run with
docker execwill only run as long as the container's main process (PID 1) is running. If the container restarts, the command won't start again. - The command must be an executable. You can't use chained commands directly with
docker exec. For example,docker exec -it my_container "echo a && echo b"won't work, butdocker exec -it my_container sh -c "echo a && echo b"will.
Using docker exec is a great way to interact with and manage your running containers. Whether you're debugging, adding files, or just exploring, it's an essential tool in your Docker toolkit.