docker exec
is a utility that allows you to connect to an already running Docker container and execute commands without restarting or stopping it. This is very convenient for technical analysis, configuration, and debugging applications. For example, you can check logs, modify configurations, or restart services.
And on a cloud server in Hostman, this command helps manage running applications in real time, without rebuilding containers or interfering with the image.
The docker exec
command allows users to interact with running Docker containers by executing commands directly inside them. This is a critical tool for container management, debugging, and performing administrative tasks without the need to restart or stop containers. It provides a way to troubleshoot and configure containers in real-time, facilitating a seamless workflow for managing containerized applications.
Before using it, make sure Docker is installed and the container is running. If you are just starting out, check out the installation guide for Docker on Ubuntu 22.04.
The basic syntax of docker exec is:
docker exec [options] <container> <command>
Where:
<container>
is the name or ID of the target container;<command>
is the instruction to be executed inside it.Example of launching bash inside a container:
docker exec -it my_container /bin/bash
This way, you can access the container’s environment and run commands directly without stopping it.
ls /app
docker exec -u root my_container whoami
docker exec -e DEBUG=true my_container env
docker exec -w /var/www my_container ls
docker exec -d my_container touch /tmp/testfile
docker exec -it nginx_container nginx -t
Let’s consider some typical but slightly more complex scenarios that may be useful in daily work: running as another user, passing multiple environment variables, specifying a working directory, etc.
docker exec -u www-data my_container ls -la /var/www
docker exec -e DEBUG=true -e STAGE=dev my_container env
docker exec -u root -w /opt/app my_container ls
If you deploy a Laravel application in a container on a Hostman server, docker exec
will be very handy. Suppose you have a container with Laravel and a database in a separate service.
To connect to the server:
ssh root@your-server-ip
After connecting, you can run Artisan commands—Laravel’s built-in CLI—inside the container.
docker exec -it laravel_app php artisan cache:clear
docker exec -it laravel_app php artisan migrate
docker exec -it laravel_app php artisan queue:listen
docker exec -u www-data -it laravel_app php artisan config:cache
docker exec -it mariadb_container mysqldump -u root -p laravel_db > backup.sql
Before running the last command, make sure that a volume for /backup
is mounted, or use SCP to transfer the file to your local machine.
This approach does not require changing the image or direct container access, which makes administration safe and flexible.
In this section, we will look at less common but more flexible uses of the docker exec
command: for example, running psql
in a PostgreSQL container, executing Node.js scripts, or connecting to stopped containers. These cases show how flexible the command can be if something non-standard is required.
The command is not limited to basic tasks: in addition to launching shell
or bash
, you can work with environments, interact with databases, run Node.js scripts, and connect to any running container.
docker exec -it postgres_container psql -U postgres -d my_db
script.js
):docker exec -it node_app node script.js
docker start my_container
docker exec -it my_container bash
-u
:docker exec -u www-data my_container ls -la /var/www
docker exec -it my_container rm -rf /tmp/cache/*
This approach is convenient in cron jobs or when manually cleaning temporary directories.
Despite its convenience, docker exec
is a manual tool for interacting with containers. In production environments, its use can be risky.
Why not use docker exec in production:
Instead, it is recommended to use:
Cause: container not found or stopped
Solution:
docker ps
The command shows a list of running containers. If your container is not listed, it’s not running or hasn’t been created.
Cause: attempt to run a command in a stopped container
Solution:
docker start <container_name>
After starting the container, you can use docker exec
again.
Cause: insufficient user permissions
Solution:
docker exec -u root <container> <command>
The -u root
flag runs the command as root
, providing extended access inside the container. This is especially useful when working with system files or configurations.
In addition to docker exec
, there is another way to interact with a container—the docker attach
command. It connects you directly to the main process running inside the container, as if you launched it in the terminal.
This is convenient if you need to monitor logs or enter data directly, but there are risks: any accidental key press (for example, Ctrl+C) can stop the container. That’s why it’s important to understand the differences.
Also, docker attach
requires TTY (a terminal emulator) for correct work with interactive apps like bash
or sh
.
Parameter |
docker exec |
docker attach |
Requires TTY |
Optional |
Yes |
Multiple connections |
Yes |
No |
Interferes with main process |
No |
Yes |
Usable for debugging |
Yes |
Partially (may harm app) |
Use docker exec
for auxiliary tasks—it provides flexibility and reduces risks.
One of the most common uses of docker exec
is for debugging running containers. You can quickly inspect logs, check the file system contents, or run diagnostic tools inside the container to investigate issues. For example, you could run:
docker exec -it my-container tail -f /var/log/syslog
This command will allow you to stream the contents of a log file in real-time to help identify problems.
docker exec
is useful for making quick configuration changes inside containers. For instance, if you need to update environment variables or adjust configuration files for an application running in a container, you can do so without restarting the container. Example:
docker exec my-container bash -c "echo 'new_value' > /path/to/config/file"
For ongoing maintenance, docker exec allows you to perform various tasks such as running database migrations, executing backups, or installing missing packages within the container. For example:
docker exec my-container /bin/bash -c "apt-get update && apt-get install -y new-package"
This can be helpful when you need to manage container-based services without interrupting their operation.
docker exec
enables security professionals to examine a container's internal state, check for potential vulnerabilities, or review installed software and packages for compliance. You can execute commands like:
docker exec my-container dpkg -l | grep vulnerable-package
This can help in scanning containers for security flaws or outdated software that may pose a risk.
Sometimes, you need to run quick, one-time commands inside a container for tasks such as checking the system status, testing a specific command, or inspecting the environment. docker exec allows you to run such commands without the need to enter the container interactively. Example:
docker exec my-container uptime
This will return the uptime of the container without needing to access the shell.
The docker exec
command is an effective tool for managing containers without interfering with their lifecycle. It allows you to run commands as different users, pass variables, check logs, and perform administrative tasks.
When working in cloud services such as Hostman, this is especially useful: you can perform targeted actions without rebuilding the image and without risking the main process.
It is important to remember: docker exec
is a manual tool and does not replace automated DevOps approaches. For system-level changes, it is better to use Dockerfile and CI/CD, keeping your infrastructure reproducible and secure.
The docker exec command allows you to run commands inside an already running container.
You can execute simple one-off commands (e.g., ls, cat, ps) or launch a full shell session (/bin/bash or /bin/sh).
This is useful for debugging, inspecting processes, modifying configuration, or performing administrative tasks within the container.
Example:docker exec my-container ls -l
This lists files inside the container named my-container.
To open an interactive shell inside a container, you use docker exec with the -it flags:
-i keeps STDIN open (interactive mode).
-t allocates a pseudo-terminal (TTY).
Example: docker exec -it my-container /bin/bash
This drops you into a Bash shell inside the container. If Bash is not available, you can try /bin/sh: docker exec -it my-container /bin/sh
docker exec: Runs a command inside an already running container. You use it when you want to access or inspect a container that’s currently active.
docker run with a shell (like /bin/bash): Creates a new container from an image and immediately launches the specified shell. The lifecycle is different because run starts a new instance, whereas exec attaches to an existing one.
In short:
exec = run command in an existing container.
run = start a new container and run a command inside it.
The exec command is used for:
Debugging and troubleshooting: Open a shell to inspect logs, running processes, or network connectivity inside a container.
Maintenance tasks: Apply updates, perform database migrations, or run backups without stopping the container.
Quick checks: Run one-off commands (like checking disk usage or environment variables).
Security checks: Verify installed packages or scan for vulnerabilities.
Example use case: docker exec -it my-container env
This shows all environment variables inside the container.