Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
The docker exec
command allows you to run commands inside a running Docker container. This is useful for various tasks such as debugging, checking logs, or modifying files within the container. This tutorial will guide you through the basics of using the docker exec
command.
Prerequisites:
docker run -it --name example-container ubuntu:20.04 /bin/bash
This command will start a new container based on the ubuntu:20.04
image and open an interactive shell session. The --name
flag is used to assign a name to the container for easier reference.
Run a command inside the running container:
To execute a command inside the running container, use the following syntax:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
For example, to run the ls
command inside the example-container
, use:
docker exec example-container ls
This will display the list of files and directories in the container's working directory.
Run an interactive command inside the running container:
Some commands require user input or an interactive session. To run an interactive command, use the -i
(interactive) and -t
(TTY) options:
docker exec -it CONTAINER COMMAND [ARG...]
For example, to open an interactive shell session inside the example-container
, use:
docker exec -it example-container /bin/bash
This will allow you to interact with the container's file system and run commands as if you were working directly inside the container.
Run a command as a different user:
By default, docker exec
runs commands as the root
user. If you want to run a command as a different user, use the -u
or --user
option followed by the desired username or UID:
docker exec -u USER CONTAINER COMMAND [ARG...]
For example, to run the ls
command as the nobody
user inside the example-container
, use:
docker exec -u nobody example-container ls
Detach from the exec process:
If you want to run a command in the background and detach from the process, use the -d
or --detach
option:
docker exec -d CONTAINER COMMAND [ARG...]
For example, to run a script named background_script.sh
in the background inside the example-container
, use:
docker exec -d example-container /bin/bash background_script.sh
This will start the script in the background and return control to the host's terminal.
In this tutorial, we covered the basics of using the docker exec
command to run commands inside a running Docker container. The docker exec
command is a powerful tool for managing and interacting with containers, allowing you to perform tasks such as debugging, checking logs, or modifying files.
How to Use Docker Exec Command:
docker exec
command allows you to run commands inside a running Docker container. It provides a way to interact with a container's filesystem and execute processes within the container.docker exec <container_id_or_name> <command>
Running Commands in Docker Containers with Exec:
docker exec -it <container_id_or_name> ls /app
Docker Exec Command Examples:
docker exec
command.docker exec -it <container_id_or_name> bash docker exec -it <container_id_or_name> ps aux
Interactive Mode with Docker Exec:
-it
flag allows you to interact with the container in real-time, providing an interactive shell.docker exec -it <container_id_or_name> sh
Executing Commands in Running Docker Containers:
docker exec <container_id_or_name> echo "Hello, Docker!"
Docker Exec and Detached Mode:
-d
flag.docker exec -d <container_id_or_name> my_background_process
Passing Environment Variables with Docker Exec:
-e
flag.docker exec -e MY_VAR=value <container_id_or_name> my_command
Executing Scripts with Docker Exec:
docker exec <container_id_or_name> /path/to/script.sh
Docker Exec and Multiple Commands:
docker exec
invocation.docker exec <container_id_or_name> sh -c "echo 'Command 1' && echo 'Command 2'"