exec
command?The exec
command is used to interact with already running containers on the Docker host. It allows you to start a session within the default directory of the container. Sessions created with the exec
command are not restarted when the container is restarted.
docker exec {Options} {Container Name} {Command} {Arguments}
Name, Shorthand | Description |
---|---|
- -detach, -d | Detached mode: run command in the background |
- -detach-keys | Override the key sequence for detaching a container |
- -env, -e | Set environment variables |
- -interactive, -i | Keep STDIN open even if not attached |
- -privileged | Give extended privileges to the command |
- -tty, -t | Allocate a pseudo-TTY |
- -user, -u | Username or UID (format: <name |
- -workdir, -w | Working directory inside the container |
To find out the name of the container, you can run the
ps
command.
docker ps
For the sake of this shot, let’s assume the name/ID of the container is 123456789abc.
We can start bash-shell in an already running docker container using the following command:
docker exec -it 123456789abc bash
Here, -it
specifies that the bash-shell is going to be interactive, i.e., the user will be able to write commands and receive output. Moreover, if bash is not in the directory of the container, you need to specify the directory of bash at the end of the command.
If you want to run a command in a directory outside your container directory, you can use the option -w
and specify the directory.
docker exec -w {path to directory} 123456789abc {command}
To execute multiple commands in a single statement, the user needs to be careful of a couple of things:
""
) instead of single quotation marks ( ''
).-c
to read the command as a string.docker exec 123456789abc bash -c "{command 1} ; {command 2}"
Click here to read the official documentation.
Free Resources