Sign In
Sign In

Docker Exec: How to Use It to Run Commands in a Container

Docker Exec: How to Use It to Run Commands in a Container
Minhal Abbas
Technical writer
Docker
29.01.2025
Reading time: 8 min

Docker is an effective and versatile environment built to assist you in the matter of running, creating, as well as deploying apps within containers. One of the significant utilities in it is docker exec. It permits you to run code within a particular container. Furthermore, you can maintain as well as build a reliable, compact container through it. During creation or installation, it is significant to analyze different operations/configurations and examine the current condition or resolve bugs. Therefore, it offers an environment where commands can be run in dockerized apps.

This tutorial will cover docker exec, complete with possible use cases and explanations.

Prerequisites

You must meet certain prerequisites before beginning the article:

  • Installation: Verify that Docker is already installed. If not, check our tutorial to install it.
  • Permissions: The user account should have permissions/privileges to run the script.
  • Running Container: A container needs to be accessible as well as running at the moment. Through the docker ps, you can determine the ID or name of the container.
  • General Concepts: You should be familiar with core concepts of Docker. Familiarity with Linux systems and Docker basics will help in troubleshooting any issues during configuration.

These requirements are necessary before beginning the setup.

Basic Introduction 

docker exec permits greater control, enhanced privacy, as well as better security for your apps. It helps users with regarding, management, monitoring, and debugging running apps within the particular container. Explore its features to boost your productivity and automate workflows. In this way, you can run direct commands by performing several operations like opening sessions, shell commands, and even running scripts.

This significantly enhances workflow by enabling interaction with the active/operational app. You can address issues as well as make configurations without the need for a full container restart, which improves efficiency.

General Syntax 

The general syntax is:

docker exec [OPTIONS] CONTAINER CODE [ARG...]
  • OPTIONS: These are flags for customizing the behaviour of the particular command. Several options are as below:

    • -i: It indicates STDIN is launched even if not connected.

    • -t: It addresses the allocation of a pseudo-TTY.

    • -u USER: It indicates the particular user for running the command.

    • -w WORKDIR: It indicates the directory which is working for the particular command.

  • CONTAINER: It indicates the container ID or name, where instructions are executed.

  • CODE:  This is the script or command that you require to run inside the container

  • ARG: It represents the additional parameters that are required to be passed to the particular CODE.

How to Use Docker Exec to Run Commands in a Container

Through this utility, you can run programs, check logs, and perform other admin operations inside the particular running container by accessing its CLI. It is beneficial for effective management since it increases adaptability and gives more hold of dockerized apps.

Testing with a Sample Container

Before running the specific command, you should have the minimum of one container that is currently operational. If you do not have it yet, execute the below command by with the particular container name. In our case, we use mynginx

docker run -d --name mynginx nginx

Image9

Finding the Active Container ID

Before beginning, you are required to know the ID or name of the running container. Let’s run the below command to obtain the info on all dockerized apps that are currently operational:

docker ps

Image14

In the figure, the operational instance ID is b51dc8e05c77 and the name is mynginx

Working With a Particular Directory

In this first example, you can run the command in the particular directory of the operational container. To achieve this, the --workdir or -w option is used by mentioning the folder name. Look at a use case where the pwd is run within the operational container mynginx:

docker exec --workdir /tmp mynginx pwd

Here:

  • docker exec: It is the core command to run the command within the operational container.
  • --workdir /tmp: This OPTION indicates our working directory.
  • mynginx: It indicates the CONTAINER name. 
  • pwd: It indicates the executed CODE within the container.

Image1

In the figure, the pwd executes within the particular mynginx instance and allocates the working directory to /tmp

Single Command Execution 

In this example, execute a single command. For this, first mention the container name or ID, and afterwards, the particular command that you are required to execute. Here, mynginx is the name of the operational container, and the echo "Hello, Hostman Users!" is the command:

docker exec mynginx echo "Hello, Hostman Users!"

Image11

In the figure, there is an execution of the echo "Hello, Hostman Users!" command within mynginx.

Several Commands Execution 

You can execute several commands in a single line statement by splitting them with semicolon. Let’s look at the below statement:

docker exec mynginx /bin/bash ls; free -m; df -h;

Image7

In the result, ls shows the content inside of the mentioned folder, free -m shows the system memory and df -h disk space usage. It permits you to analyze the memory state, filesystem, and other info in one statement.

Enabling the Shell Through Name

You can enable the shell within the dockerized app. It permits an interface for the file system as well as script execution. Here, the -it option activates interactive mode and assigns the interface:

docker exec -it mynginx /bin/bash

Image17

The figure enables the bash shell interface within mynginx. But, /bin/bash is not guaranteed to be present in every image of Docker. Therefore, other shells like sh can also be enabled.

Now, input exit and press ENTER to close the interface:

exit

Image15

To launch other shells like sh (which is a symbolic link to bash or another shell), use /bin/sh in the below statement line:

docker exec -it mynginx /bin/sh

Image3

In the figure, the code line launches the shell interface, which is operational. 

Enabling the Shell Through ID

In this particular use case, enable the session through the b51dc8e05c77 container ID inside the Docker app. Furthermore, you have the ability to interact with the interface as though you directly logged in via the -it flag. The -t indicates the assignment of pseudo-TTY, and the -i opens the STDIN. Both are beneficial for analysis, debugging, as well as managerial operations:

docker exec -it b51dc8e05c77 bash

Image5

Furthermore, you can analyse the information of the current folder inside the particular shell (in a detailed format), e.g., file size, owner, group, number of links, modification date, and file permissions:

ls -l

Image12

It gives detailed information on each file as well as the folder that assists you in knowing their attributes and managing them effectively.

Working As a Particular User

You can execute a command as the specific user through the -u option. It is beneficial when you are permitted to work with specific privileges. It runs the command in the operational container through the particular user and group:

docker exec -u <user>:<group> <container_id> <command> 

For instance, the whoami runs as the www-data in the mynginx container:

docker exec -u www-data mynginx whoami

Image6

In the figure, www-data verifies that the particular command is executed successfully with the correct user permissions and within the expected interface. 

Enabling a Non-Interactive Shell

Sometimes, users prefer not to have any interaction. For such circumstances, they can execute the command without any argument:

docker exec mynginx tail /etc/passwd 

Image10

The last 10 lines of the passwd file have been shown. This passwd file is stored in the /etc/passwd folder containing the user information. It helps you monitor the user account information, permitting you to quickly check for troubleshooting or update issues. 

Working With a Single Environment Variable

You may need to pass environment variables to the command that is run in the operational container. To achieve this, use the -e option as below:

docker exec -e MY_VAR=value mynginx printenv MY_VAR

Image2

In the figure, the printenv MY_VAR is successfully executed in mynginx when the MY_VAR is set to value correctly.

Working With Multiple Environment Variables

You can set more than one variable through the -e flag. 

docker exec -e TEST=john -e ENVIRONMENT=prod mynginx env

Image8

The figure confirms that the two variables TEST and ENVIRONMENT have been set to john and prod in the mynginx.

Working With the Detached Mode

You can run commands in the detached mode through the -d flag. Therefore, it runs in the background:

docker exec -d mynginx sleep 500

Image13

The figure confirms that the mynginx is executing the sleep 500 command.

Working With the Privileged Mode

Here, the --privileged flag permits you to execute the command, such as mount, with elevated privileges in the running container:

docker exec --privileged mynginx mount

Image4

In the figure, mount permits the system to create a mount point with the particular permissions in the mynginx.

More Information on docker exec

The --help option shows the manual with a list of available options with concise explanations. 

docker exec --help

Image16

Final Words

docker exec is an effective utility for controlling and interacting with active containers. It is helpful for operations like monitoring, managing, and debugging apps without interfering with their functionality. It permits you to run code, launch shells, customize several configuration aspects, and also set environment variables. Once you become familiar with the usage of this utility, you can manage containers easily. It makes your operations much smoother for creating and deploying apps.

Docker
29.01.2025
Reading time: 8 min

Similar

Docker

Installing Nextcloud with Docker

For those who want full control over their data, Nextcloud provides a powerful open-source solution for building a private cloud storage system. It not only enables secure file synchronization across devices but also allows you to deploy storage on your own server, avoiding reliance on third-party providers. In this guide, we’ll go through the process of installing Nextcloud using isolated Docker containers, which greatly simplifies deployment and management. We’ll also configure automatic traffic encryption with SSL certificates from Let’s Encrypt to ensure secure data transmission. Prerequisites You will need: A Hostman cloud server with Linux Ubuntu 24.04 pre-installed. A domain name. Docker and Docker Compose installed. For the server, choose a configuration with 1 CPU core, 2 GB of RAM, and a public IPv4 address, which you can request when creating the server or later in the “Network” section. The server will be set up within a few minutes. The IPv4 address, login, and password for SSH access will be available in the Dashboard. Installing and Running Nextcloud Nextcloud requires several key components to run: Database: in this case, MariaDB, a high-performance and reliable DBMS. SSL certificate: we’ll use free SSL certificates from the non-profit certificate authority Let’s Encrypt. Reverse proxy: we’ll add Nginx Proxy Manager, which will route and balance incoming HTTP and HTTPS traffic to the appropriate containers. Step 1: Create a Configuration Directory First, we create a directory where we will store configuration files, and navigate to it. mkdir nextcloud && cd nextcloud Step 2: Create an .env File This hidden file will store variables with passwords: nano .env File contents: NEXTCLOUD_ROOT_PASSWORD=secure_root_password_123 NEXTCLOUD_DB_PASSWORD=secure_nextcloud_db_password_456 NPM_ROOT_PASSWORD=secure_npm_root_password_789 NPM_DB_PASSWORD=secure_npm_db_password_012 Don’t forget to replace the values with your own. Step 3: Create the docker-compose.yml File Use nano to create it: nano docker-compose.yml Add the following configuration: volumes: nextcloud-data: nextcloud-db: npm-data: npm-ssl: npm-db: networks: frontend: backend: services: nextcloud-app: image: nextcloud:31.0.8 restart: always volumes: - nextcloud-data:/var/www/html environment: - MYSQL_PASSWORD=${NEXTCLOUD_DB_PASSWORD} - MYSQL_DATABASE=nextcloud - MYSQL_USER=nextcloud - MYSQL_HOST=nextcloud-db - MYSQL_PORT=3306 networks: - frontend - backend nextcloud-db: image: mariadb:12.0.2 restart: always command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW volumes: - nextcloud-db:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=${NEXTCLOUD_ROOT_PASSWORD} - MYSQL_PASSWORD=${NEXTCLOUD_DB_PASSWORD} - MYSQL_DATABASE=nextcloud - MYSQL_USER=nextcloud networks: - backend npm-app: image: jc21/nginx-proxy-manager:2.12.6 restart: always ports: - "80:80" - "81:81" - "443:443" environment: - DB_MYSQL_HOST=npm-db - DB_MYSQL_PORT=3306 - DB_MYSQL_USER=npm - DB_MYSQL_PASSWORD=${NPM_DB_PASSWORD} - DB_MYSQL_NAME=npm volumes: - npm-data:/data - npm-ssl:/etc/letsencrypt networks: - frontend - backend npm-db: image: jc21/mariadb-aria:10.11.5 restart: always environment: - MYSQL_ROOT_PASSWORD=${NPM_ROOT_PASSWORD} - MYSQL_DATABASE=npm - MYSQL_USER=npm - MYSQL_PASSWORD=${NPM_DB_PASSWORD} volumes: - npm-db:/var/lib/mysql networks: - backend Step 4: Start the Containers Run the command: docker compose up -d When running docker compose up -d, you may encounter an error related to Docker Hub pull limits. In that case: Log in to your Docker Hub account, or register a new one on the official website. Go to Account settings → Personal access tokens. Click Generate new token. Enter a description, set an expiration date, and select permissions: Read, Write, Delete. Click Generate. Copy and save the token (it will only be shown once). On the server, log in with: docker login -u dockeruser Replace dockeruser with your Docker Hub username. When prompted for a password, paste the token. Restart the containers:  docker compose up -d Wait until all containers are up and running.  Check with: docker ps All containers should have the status Up. Step 5. Configure HTTPS with Let’s Encrypt Open a browser and go to http://<server-IP>:81 to access the Nginx Proxy Manager interface. Log in with the default credentials: Login: admin@example.com Password: changeme On first login, update the admin user details (Full Name, Nickname, Email). Change the admin password: Current Password: changeme New Password: your new password Confirm Password: repeat the new password Save changes. Step 6: Add a Proxy Host Go to Hosts → Proxy Hosts. Click Add Proxy Host. Fill in the fields: Domain Names: the domain for your Nextcloud instance. Scheme: http. Forward Hostname/IP: nextcloud-app (the service name from docker-compose.yml). Forward Port: 80. Go to the SSL tab: In SSL Certificate, select Request a new SSL Certificate. Enable: Force SSL HTTP/2 Support HSTS Enabled Enter your email for Let’s Encrypt. Agree to Let’s Encrypt terms of service. Click Save. The configured host will appear in the list. Step 7. Log In to NextCloud Now, navigate to your domain name. If everything is set up correctly, the Nextcloud web interface will open, and an SSL certificate will be issued by Let’s Encrypt. Create a new administrator account. Optionally, install recommended apps or skip this step. At this point, Nextcloud installation and basic configuration is complete. Conclusion In this article, we demonstrated how to deploy Nextcloud using Docker and issue a free Let’s Encrypt certificate. This method is one of the most reliable, secure, and easily scalable approaches. Docker ensures application isolation, simplifies updates, and makes migration between systems easier. Using an SSL certificate is not just a recommendation but a necessity for protecting confidential data and ensuring encrypted traffic.
24 September 2025 · 5 min to read
Docker

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
Docker

How to Install Docker on Ubuntu 22.04

Docker is a free, open-source tool for application containerization. Containers are isolated environments similar to virtual machines (VMs), but they are more lightweight and portable across platforms, requiring fewer system resources. Docker uses OS-level virtualization, leveraging features built into the Linux kernel. Apps order after installing Docker on Ubuntu Although it applies to other Ubuntu versions as well, this tutorial explains how to install Docker on Ubuntu 22.04. We'll also download Docker Compose, which is a necessary tool for effectively managing several containers. For this guide, we will use a Hostman cloud server. System Requirements According to Docker's documentation, the following 64-bit Ubuntu versions are supported: Ubuntu Oracular 24.10 Ubuntu Noble 24.04 (LTS) Ubuntu Jammy 22.04 (LTS) Ubuntu Focal 20.04 (LTS) Docker works on most popular architectures. The resource requirements for your device will depend on your intended use and how comfortably you want to work with Docker. The scale of applications you plan to deploy in containers will largely dictate the system needs. Some sources recommend a minimum of 2 GB of RAM. Additionally, a stable internet connection is required. Installing Docker on Ubuntu 22.04 Installing Docker on Ubuntu 22.04 involves executing a series of terminal commands. Below is a step-by-step guide with explanations. The steps are also applicable to server versions of Ubuntu. 1. Update Package Indexes The default repository may not always contain the latest software releases. Therefore, we will download Docker from its official repository to ensure the latest version. First, update the package indexes: sudo apt update 2. Install Additional Packages To install Docker, you’ll need to download four additional packages: curl: Required for interacting with web resources. software-properties-common: Enables software management via scripts. ca-certificates: Contains information about certification authorities. apt-transport-https: Necessary for data transfer over the HTTPS protocol. Download these packages with the following command: sudo apt install curl software-properties-common ca-certificates apt-transport-https -y The -y flag automatically answers "Yes" to all terminal prompts. 3. Import the GPG Key Software signatures must be verified using the GPG key. Docker's repository must be added to the local list. Use the command to import the GPG key: wget -O- https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor | sudo tee /etc/apt/keyrings/docker.gpg > /dev/null During the import process, the terminal may display a warning before confirming the successful execution of the command. 4. Add Docker Repository Add the repository for your version of Ubuntu, named "Jammy." For other versions, use their respective code names listed in the "System Requirements" section. Run the following command: echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null During execution, the terminal will prompt you to confirm the operation. Press Enter. 5. Update Package Indexes Again After making these changes, update the package indexes once more using the familiar command: sudo apt update 6. Verify the Repository Ensure that the installation will proceed from the correct repository by running the following command: apt-cache policy docker-ce Output example: Depending on the most recent Docker releases, the result could change. Verifying that the installation will be carried out from Docker's official repository is crucial. 7. Installing Docker After configuring the repositories, proceed with the Docker installation: sudo apt install docker-ce -y The installation process will begin immediately. To confirm a successful installation, check Docker's status in the system: sudo systemctl status docker Output example: The output should indicate that the Docker service is active and running. And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS. Installing Docker Compose Docker Compose is a Docker tool designed for managing multiple containers. It is commonly used in projects where many containers must work together as a unified system. Managing this process manually can be challenging. Instead, you describe the entire configuration in a single YAML file containing the settings and configurations for all containers and their applications. There are several ways to install Docker Compose. If you need the latest version, make sure to use manual installation and installation via the Git version control system. Installation via apt-get If having the latest version is not critical for you, Docker Compose can be installed directly from the Ubuntu repository. Run the following command: sudo apt-get install docker-compose Installing via Git First, install Git: sudo apt-get install git Verify the installation by checking the Git version: git --version The output should show the Git version. Next, clone the Docker Compose repository. Navigate to the Docker Compose GitHub page and copy the repository URL. Run the following command to clone the repository: git clone https://github.com/docker/compose.git The cloning process will begin, and the repository will be downloaded from GitHub. Manual Installation Go to the Docker Compose GitHub repository and locate the latest release version under the Latest tag. At the time of writing, the Latest version of Docker Compose is v2.31.0. Let's download it: sudo curl -L "https://github.com/docker/compose/releases/download/v2.31.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose In this command, the parameters $(uname -s) and $(uname -m) automatically account for the system characteristics and architecture. After the download finishes, change the file's permissions: sudo chmod +x /usr/local/bin/docker-compose Right order of your infrastructure after installation of Docker on Ubuntu Conclusion In this guide, we covered the installation of Docker on Ubuntu 22.04, along with several ways to install Docker Compose. You can order a cloud server at Hostman for your experiments and practice.
22 August 2025 · 5 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support