The container exec command

Sometimes, when you have a container running detached, you might want to get access to it, but don't want to attach to the executing command. You can accomplish this by using the container exec command. This command allows you to execute another command in the running container, without attaching to or interfering with the already-running command. This command is often used to create an interactive session with an already-running container or to execute a single command within the container. The command looks like this:

# start an nginx container detached
docker container run --detach --name web-server1 -p 80:80 nginx

# see that the container is currently running
docker container ls

# execute other commands in the running container
# Usage: docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
docker container exec -it web-server1 bash

docker container exec web-server1 cat /etc/debian_version

# confirm that the container is still running
docker container ls

When the exec command completes, you exit the bash shell, or the file contents have been displaced, then it exits back to the terminal session leaving the container running detached:

Let's take a look at another Docker command before we continue our discussion of the many optional container run parameters. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset