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.
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.
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.