This method is tested on Debian 12 (Stable). It should work the same way on all Debian derivatives. On distributions that are not using the APT package manager, the commands for installing Docker and Docker Compose will be different.
Install Docker and Docker Compose
Install Docker and Docker Compose:
sudo apt install docker.io docker-compose -y
Create Your Docker Compose File
Create your Compose file (using nano in this example):
nano docker-compose.yml
Paste the contents of your Compose file (CTRL + Shift + V). Save with CTRL + O, press Enter, and exit nano with CTRL + X.
Note: YAML is indentation sensitive!
Start Your Containers
Start your container(s) from the directory where your
docker-compose.yml
is located:
sudo docker-compose up
This will show the logs of the container. To stop the container, press CTRL + C.
If you want to start the container in the background (detached mode),
use the -d
flag:
sudo docker-compose up -d
Stopping and Removing Containers
To stop the container:
sudo docker-compose stop
This does not remove the containers; you can restart them later.
To stop and remove all containers, networks, and volumes:
sudo docker-compose down
To stop, remove everything, and also clean up orphan containers:
sudo docker-compose down --remove-orphans
What's the difference?
- stop: Temporarily pauses your containers without removing them.
- down: Completely stops and cleans up your environment.
- down --remove-orphans: Additionally removes orphan containers
(those created by previous
docker-compose up
commands but no longer present in thedocker-compose.yml
).
Warning: Using down
or down --remove-orphans
will also remove application settings/data unless volumes are persisted properly.
Example docker-compose.yml
Here is an example docker-compose file for an Nginx container with comments explaining the basics:
services:
nginx:
image: nginx:latest # Use the latest official Nginx image from Docker Hub.
container_name: nginx # Name the container "nginx".
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d # Local folder mapped to Nginx’s config directory.
- ./nginx/html:/usr/share/nginx/html # Local folder mapped to Nginx’s web root.
ports:
- "80:80" # Expose port 80 on host and map to port 80 in container.
- "8080:8080" # Expose port 8080.
- "443:443" # Expose port 443.
restart: unless-stopped # Restart automatically unless stopped manually.
More Compose Examples
You can find a collection of working docker-compose files here.