docker container exec

Estimated reading time: 4 minutes

Description

Run a command in a running container

Usage

docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]

Options

Name, shorthand Default Description
--detach, -d false Detached mode: run command in the background
--detach-keys   Override the key sequence for detaching a container
--env, -e   Set environment variables
--interactive, -i false Keep STDIN open even if not attached
--privileged false Give extended privileges to the command
--tty, -t false Allocate a pseudo-TTY
--user, -u   Username or UID (format: <name|uid>[:<group|gid>])

Parent command

Command Description
docker container Manage containers
Command Description
docker container attach Attach to a running container
docker container commit Create a new image from a container’s changes
docker container cp Copy files/folders between a container and the local filesystem
docker container create Create a new container
docker container diff Inspect changes to files or directories on a container’s filesystem
docker container exec Run a command in a running container
docker container export Export a container’s filesystem as a tar archive
docker container inspect Display detailed information on one or more containers
docker container kill Kill one or more running containers
docker container logs Fetch the logs of a container
docker container ls List containers
docker container pause Pause all processes within one or more containers
docker container port List port mappings or a specific mapping for the container
docker container prune Remove all stopped containers
docker container rename Rename a container
docker container restart Restart one or more containers
docker container rm Remove one or more containers
docker container run Run a command in a new container
docker container start Start one or more stopped containers
docker container stats Display a live stream of container(s) resource usage statistics
docker container stop Stop one or more running containers
docker container top Display the running processes of a container
docker container unpause Unpause all processes within one or more containers
docker container update Update configuration of one or more containers
docker container wait Block until one or more containers stop, then print their exit codes

Extended description

Run a process in a running container.

The command started using docker exec will only run while the container’s primary process (PID 1) is running, and will not be restarted if the container is restarted.

If the container is paused, then the docker exec command will wait until the container is unpaused, and then run

CAPABILITIES

privileged gives the process extended Linux capabilities when running in a container.

Without this flag, the process run by docker exec in a running container has the same capabilities as the container, which may be limited. Set --privileged to give all capabilities to the process.

USER

user sets the username or UID used and optionally the groupname or GID for the specified command.

The followings examples are all valid: –user [user | user:group | uid | uid:gid | user:gid | uid:group ]

Without this argument the command will be run as root in the container.

Examples

$ docker run --name ubuntu_bash --rm -i -t ubuntu bash

This will create a container named ubuntu_bash and start a Bash session.

$ docker exec -d ubuntu_bash touch /tmp/execWorks

This will create a new file /tmp/execWorks inside the running container ubuntu_bash, in the background.

$ docker exec -it ubuntu_bash bash

This will create a new Bash session in the container ubuntu_bash.

chat icon Feedback? Suggestions? Can't find something in the docs?
Edit this page Request docs changes Get support
Rate this page: