Docker Exec: Access, Commands, and Use Cases
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.
What is Docker exec Command
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.
How to Use docker exec: Parameters and Examples
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.
Key Options:
-i — enables input mode;
-t — attaches a pseudo-terminal, useful for running bash;
-d — runs the task in the background;
-u — allows running the command as a specified user;
-e — sets environment variables;
-w — sets the working directory in which the command will be executed.
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.
Usage Examples
List files inside the container:
ls /app
Run commands with root access:
docker exec -u root my_container whoami
Pass environment variables:
docker exec -e DEBUG=true my_container env
Set working directory:
docker exec -w /var/www my_container ls
Run background tasks:
docker exec -d my_container touch /tmp/testfile
Check Nginx configuration inside a container before restarting it:
docker exec -it nginx_container nginx -t
Advanced Use Cases
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.
Run as web user:
docker exec -u www-data my_container ls -la /var/www
Set multiple environment variables at once:
docker exec -e DEBUG=true -e STAGE=dev my_container env
Set working directory with admin rights:
docker exec -u root -w /opt/app my_container ls
Example with Laravel in Hostman
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.
Clear application cache:
docker exec -it laravel_app php artisan cache:clear
Run migrations:
docker exec -it laravel_app php artisan migrate
Check queue status:
docker exec -it laravel_app php artisan queue:listen
Set permissions:
docker exec -u www-data -it laravel_app php artisan config:cache
Make a backup of a database deployed in a separate container:
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.
Extended Capabilities of docker exec
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.
Connect to PostgreSQL CLI:
docker exec -it postgres_container psql -U postgres -d my_db
Run a Node.js script (if you have script.js):
docker exec -it node_app node script.js
Run a stopped container:
docker start my_container
docker exec -it my_container bash
Manage users explicitly with -u:
docker exec -u www-data my_container ls -la /var/www
Quickly remove temporary files:
docker exec -it my_container rm -rf /tmp/cache/*
This approach is convenient in cron jobs or when manually cleaning temporary directories.
When Not to Use the Command
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:
Changes are not saved in Dockerfile. This can break reproducibility and infrastructure integrity.
No command logging, so it’s difficult to track actions.
Possible desynchronization with CI/CD pipeline.
Instead, it is recommended to use:
Dockerfile and docker-compose.yml for reproducible builds;
CI/CD for automating tasks via GitHub Actions or GitLab CI;
Monitoring for log processes with Prometheus, Grafana, and Loki.
Troubleshooting Common Errors
No such container
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.
exec failed: container not running
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.
permission denied
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.
Difference Between docker exec and docker attach
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.
Use Cases of Docker exec
Debugging and Troubleshooting:
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.
Configuration Modification:
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"
Maintenance Tasks:
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.
Security Audits:
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.
Running Ad-Hoc Commands:
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.
Conclusion
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.
FAQ
1. What does docker exec do?
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.
2. How do I get into docker exec?
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
3. What is the difference between shell and exec in Docker?
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.
4. What is the exec command used for?
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.
05 September 2025 · 10 min to read