Docker Compose is a tool for defining and running applications that use multiple Docker containers. Instead of starting and configuring each container separately, you can describe your application in a YAML file and manage its services with a small set of commands.
For example, a web application might need a web server, an application backend, and a database. Docker Compose lets you define how these services run, how they communicate, and which storage volumes they use.
This guide explains how to install Docker Compose on Ubuntu, verify the installation, create a simple example application, and troubleshoot common problems.
What Is Docker Compose?
Docker Compose is part of the Docker ecosystem. It helps you manage multi-container applications using a configuration file commonly named compose.yaml or docker-compose.yml.
With Docker Compose, you can:
- Define multiple application services in one file.
- Configure container networking.
- Manage persistent storage using volumes.
- Set environment variables and service options.
- Start and stop an application stack with simple commands.
- Recreate a development or deployment environment more consistently.
Docker Compose is useful for developers, system administrators, and website owners who run containerized applications on local computers or Linux servers.
Official documentation: Docker Compose overview.
Prerequisites
Before installing Docker Compose on Ubuntu, make sure you have:
- A supported Ubuntu installation.
- A user account with
sudoprivileges. - Internet access to download packages.
- Docker Engine and the Docker CLI installed, or a plan to install them first.
Important: Docker Compose does not replace Docker Engine. On a Linux server, Compose normally operates through the Docker CLI and requires a working Docker Engine to run containers.
Step 1: Check Your Ubuntu Version
Open a terminal or connect to your server using SSH.
Run:
lsb_release -a
Alternatively, use:
cat /etc/os-release
These commands display your Ubuntu release information.
Check that your Ubuntu version is supported by the Docker installation method you intend to use. For production servers, prefer a supported release and consult the relevant official installation instructions.
Step 2: Check Whether Docker Is Already Installed
Before installing additional packages, check your existing Docker setup.
Run:
docker --version
Then check whether the Docker service is running:
sudo systemctl status docker
If Docker is installed and running, continue to Step 4.
If the docker command is missing, or the Docker service is not installed, you need to install and configure Docker Engine first.
Step 3: Install Docker Engine if Needed
If you are starting with a clean Ubuntu server, follow Docker’s official Ubuntu installation guide:
Install Docker Engine on Ubuntu.
Docker’s repository-based installation method lets you install Docker Engine, the Docker CLI, container runtime components, Buildx, and the Compose plugin as appropriate for your system.
After installing Docker, verify the installation:
sudo docker run hello-world
If the test succeeds, Docker can download and run a test container.
If the command fails, resolve the Docker Engine installation or service issue before continuing.
Step 4: Install Docker Compose V2
For a Linux server that already has Docker Engine and the Docker CLI installed from Docker’s official repository, install the Compose plugin using APT:
sudo apt-get update
sudo apt-get install docker-compose-plugin
Review any package manager errors before proceeding.
The package name above is intended for Docker’s supported repository-based installation. If your system uses Ubuntu’s own Docker packages or another packaging method, check which Compose package is available for that installation rather than assuming the same package name works everywhere.
Docker’s official instructions are available at Install the Docker Compose plugin.
Step 5: Verify Docker Compose
After installation, run:
docker compose version
You should see output identifying the installed Docker Compose version.
Notice the command format:
docker compose
There is a space between docker and compose.
This is the current Compose V2 command style. Older tutorials may use docker-compose with a hyphen, which refers to the legacy standalone installation method.
For new installations, prefer the supported Compose plugin method unless you have a specific compatibility requirement.
Step 6: Create a Simple Docker Compose Project
Now that Docker Compose is installed, create a small project that runs an Nginx web server.
First, create a project directory:
mkdir -p ~/compose-demo
cd ~/compose-demo
Create a file named compose.yaml:
nano compose.yaml
Add the following configuration:
services:
web:
image: nginx:alpine
ports:
- "8080:80"
restart: unless-stopped
Save the file and exit the editor.
This configuration defines one service named web. Docker Compose downloads the Nginx image if necessary and maps port 8080 on the host to port 80 inside the container.
The restart: unless-stopped setting asks Docker to restart the container automatically under the documented restart-policy conditions, unless it has been explicitly stopped.
Step 7: Start the Application
From the directory containing compose.yaml, run:
docker compose up -d
The -d option starts the application in the background.
Docker Compose will create the required container and start the service.
To check its status, run:
docker compose ps
To view application logs, use:
docker compose logs
To follow the logs as they arrive, run:
docker compose logs -f
Press Ctrl+C to stop following the logs. This does not normally stop the containers.
Step 8: Test the Nginx Web Server
If the project is running on your own computer, open this address in a browser:
http://localhost:8080
If the project is running on a remote Ubuntu server, open:
http://YOUR_SERVER_IP:8080
Replace YOUR_SERVER_IP with your server’s actual public IP address.
The Nginx welcome page should appear if the container is running and the port is reachable.
If you are using a remote server, make sure your firewall and hosting provider’s network rules allow the required traffic. Do not expose unnecessary ports to the public internet.
For production websites, consider placing Nginx behind a properly configured reverse proxy or load balancer and enabling HTTPS.
Step 9: Manage Your Application with Docker Compose
Once your application is running, you can manage it using several useful commands.
Start the services
docker compose up -d
Check running services
docker compose ps
View logs
docker compose logs
Restart the services
docker compose restart
Stop the services
docker compose stop
Stop and remove the Compose-managed containers and networks
docker compose down
The last command removes the project’s containers and Compose-managed networks. Named volumes are not removed by default, but you should still understand your storage configuration before performing cleanup.
Warning: Avoid adding the -v option to docker compose down unless you intentionally want to remove the associated Compose-managed named volumes. Removing volumes can permanently delete application data.
Step 10: Understand Persistent Storage
Containers can be recreated, so important data should not be stored only in a container’s writable layer.
For databases and applications that need persistent files, use appropriately configured volumes or bind mounts.
For example, a database service might use a named volume to retain its data when the container is replaced.
Before deploying a database in production:
- Configure persistent storage.
- Create and test backups.
- Protect database credentials.
- Limit network exposure.
- Document the recovery procedure.
Do not assume that removing a container also removes all of its data, or that keeping a container guarantees that its data is safe. Storage behavior depends on the configuration.
Common Docker Compose Installation Problems
The docker compose Command Is Not Found
This usually means the Compose plugin is missing, installed in a location Docker does not recognize, or incompatible with the current Docker setup.
Check the installed packages:
apt list --installed | grep -E 'docker|containerd'
If you installed Docker from its official repository, verify that docker-compose-plugin is installed. Then check the official Compose installation instructions.
Docker Is Not Running
Check the service status:
sudo systemctl status docker
If Docker is installed but stopped, start it:
sudo systemctl start docker
To configure Docker to start automatically on boot:
sudo systemctl enable docker
If the service fails to start, inspect its logs:
sudo journalctl -u docker --no-pager -n 100
Investigate the reported error rather than repeatedly restarting the service without understanding the cause.
Permission Denied When Running Docker Commands
If Docker is installed and the user lacks permission to access the Docker daemon, try running a command with sudo:
sudo docker compose ps
Be cautious about adding users to the docker group. Membership in this group can grant root-level control over the host. On shared or sensitive systems, consider the security implications and follow Docker’s official Linux post-installation guidance.
A Port Is Already in Use
If another service is using port 8080, Docker may fail to publish that port.
Check which process is listening on the port:
sudo ss -ltnp | grep ':8080'
If necessary, change the host-side port mapping in compose.yaml. For example, you could map port 8081 on the host to port 80 in the container:
ports:
- "8081:80"
Then apply the updated configuration:
docker compose up -d
The Container Starts but the Website Is Not Accessible
Check the service status and logs:
docker compose ps
docker compose logs web
Confirm that the port mapping is correct and that the host firewall and external network rules permit the connection.
On a remote server, remember that a working container does not automatically mean the service is publicly reachable.
The Configuration File Contains an Error
Validate the Compose configuration:
docker compose config
This helps identify syntax and configuration problems before you deploy the application.
Check indentation carefully because YAML uses whitespace to represent structure.
Docker Compose Security Best Practices
Installing Docker Compose is only the first step. Secure deployment requires additional care.
- Download Docker packages and images from trusted sources.
- Keep Docker Engine, Compose, and application images maintained.
- Avoid exposing databases or internal services unnecessarily.
- Store credentials securely rather than hardcoding secrets into public configuration files.
- Use persistent volumes and reliable backups for important data.
- Review image permissions and configuration before deploying containers.
- Apply firewall rules appropriate to the services you need to expose.
- Monitor application logs and system resources.
- Test major changes before applying them to production.
A Compose file describes how services should run, but it does not automatically make an application secure. Application-level authentication, network configuration, secret management, and update procedures remain important.
Frequently Asked Questions
Is Docker Compose free?
Docker Compose is available as an open-source tool. Your overall deployment may still involve costs for server hosting, storage, networking, domain registration, or other services.
Do I need Docker Engine before installing Docker Compose?
For a typical Ubuntu server installation, yes. Docker Compose relies on Docker components to create and manage containers. Install and verify Docker Engine and the Docker CLI first.
What is the difference between docker compose and docker-compose?
docker compose is the command used by the modern Compose CLI plugin. docker-compose is the legacy standalone command associated with older installations. For new Linux server installations, Docker recommends the Compose plugin approach.
Can I use Docker Compose on an Ubuntu VPS?
Yes. Docker Compose can be used on a compatible Ubuntu VPS that has Docker Engine installed and sufficient resources for the application. The server must also be configured securely, with appropriate network access and persistent storage.
How do I update Docker Compose?
If you installed the Compose plugin from Docker’s repository, refresh package information and install the updated package:
sudo apt-get update
sudo apt-get install docker-compose-plugin
Follow the official installation instructions for your distribution and package source.
How do I remove Docker Compose?
The removal procedure depends on how Compose was installed. If you installed the plugin through APT, Docker documents the package removal command:
sudo apt-get remove docker-compose-plugin
Do not remove Docker Engine or delete application volumes unless you intend to remove those components and understand the consequences.
Conclusion
Docker Compose simplifies the process of defining and running applications that use multiple Docker containers. On an Ubuntu server, the recommended approach for a typical Docker Engine installation is to install the Compose CLI plugin, verify it with docker compose version, and test it using a simple Compose project.
Once the installation works, you can use Compose to manage web servers, databases, application backends, and other containerized services.
Follow the official Docker documentation for your environment, protect persistent data with backups, and configure network access carefully before deploying applications to production.
No Comment! Be the first one.