π³ Day 5: Mastering docker exec Command - With examples!

Table of contents
- π¬ Video Demonstration
- π 1. What is docker exec?
- β 2. Why Use docker exec?
- π οΈ 3. Syntax
- Summery
- π 4. Basic Example
- π₯οΈ 5. Open an Interactive Shell
- π 6. Run as a Specific User
- π 7. Run a Script Inside the Container
- π± 8. Set Environment Variables
- π 9. Run a Command in a Specific Directory
- β 10. Best Practices
- π Additional Resources

Welcome to this Docker tutorial! In this guide, we cover the docker exec
command. Whether you're new to Docker or just need a refresher, this will help you understand how to run commands inside a running container. Let's dive in! π―
π¬ Video Demonstration
π 1. What is docker exec
?
The docker exec
command allows you to run a command in a running container.
π§ It's like opening a terminal inside your container!
β Get Help using help command
docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Execute a command in a running container
Aliases:
docker container exec, docker exec
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: "<name|uid>[:<group|gid>]")
-w, --workdir string Working directory inside the container
β 2. Why Use docker exec
?
Here are some common reasons to use docker exec
:
π Debugging: Access a containerβs shell to troubleshoot issues.
π Inspection: Check files, processes, or environment variables inside a container.
π οΈ Management: Run one-off commands or scripts without modifying the containerβs image.
π¨οΈ Interactivity: Start an interactive session (e.g., a bash shell) to work inside the container.
π οΈ 3. Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
CONTAINER: The name or ID of the running container (find it using
docker ps
).COMMAND: The command you want to run inside the container.
ARG: Optional arguments for the command.
OPTIONS:
-i
,--interactive
: Keep STDIN open.-t
,--tty
: Allocate a pseudo-TTY.-d
,--detach
: Run the command in the background.-u
,--user
: Run the command as a specific user.-e
,--env
: Set environment variables.--workdir
: Set the working directory inside the container.
Summery
π§© -i β Interactive
Keeps STDIN (standard input) open even if not attached.
Allows you to send input to the container, such as typing commands or piping input.
π₯οΈ -t β TTY (Pseudo-TTY)
Allocates a pseudo-terminal.
Makes the session act like a real terminal β this enables features like:
Colored output
Line editing
Clear formatting
Test Case
docker exec -i β you can pipe input in, but the terminal behaves oddly.
docker exec -t β may hang, since it expects interactive input.
docker exec -it ubuntu apt update -y
π 4. Basic Example
Check the current directory inside the container:
docker exec ubuntu pwd
β‘οΈ Use Case: Quickly verify the working directory or check system details.
π₯οΈ 5. Open an Interactive Shell
To start a bash shell inside the container:
docker exec -it ubuntu bash
docker exec -it alpine bash
π If bash
isn't available, try sh
:
docker exec -it alpine sh
π 6. Run as a Specific User
Run a command as the root
user:
docker exec -u root ubuntu whoami
β‘οΈ Use Case: Perform actions requiring elevated permissions.
π 7. Run a Script Inside the Container
To run a script in the background:
docker exec -it ubuntu /root/setup.sh
β‘οΈ Use Case: Execute long-running tasks without tying up your terminal.
π± 8. Set Environment Variables
Set an environment variable for the command:
docker exec -e user=postgres ubuntu env
β‘οΈ Use Case: Test how environment variables affect a command.
π 9. Run a Command in a Specific Directory
Run a command in a specific working directory:
docker exec --workdir /root ubuntu pwd
docker exec --workdir /root ubuntu ls -alsh
β‘οΈ Use Case: Execute commands in a specific directory without altering container defaults.
β 10. Best Practices
β Always use
-it
for interactive commands.β Ensure your container is running before executing commands.
β Use
docker ps
to check container status.β Avoid running long-lived processes via
exec
β it's for one-off tasks.
π Additional Resources
π§° Docker CLI Reference
π Docker Hub
Happy Docking! π³π»
Subscribe to my newsletter
Read articles from Ibrar Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ibrar Ansari
Ibrar Ansari
Hello! I'm a DevOps Engineer dedicated to continuous learning and contributing to the tech community. If you find value in what I share, feel free to spread the word to others who might benefit as well. Your support is greatly appreciated!