Docker Compose is a tool for defining and running multi-container Docker applications. Here's a checklist of common Docker Compose commands with detailed examples and expected outputs:
Docker Compose is used to run multiple containers as a single service. For example, suppose you had an application which required NGNIX and MySQL, you could create one file which would start both the containers as a service without the need to start each one separately.
1. Create a Docker Compose File:
Create a docker-compose.yml file defining your services, networks, and volumes.
Example (docker-compose.yml):
version: '3'
services:
web:
image: nginx:alpine
ports:
- "8080:80"
db:
image: postgres:latest
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
2. Start Services Defined in Compose File:
docker-compose up -d
Expected Output:
Creating network "myproject_default" with the default driver
Creating myproject_web_1 ... done
Creating myproject_db_1 ... done
3. List Running Services:
docker-compose ps
Expected Output:
4. View Logs of Services:
docker-compose logs
Expected Output:
5. Stop Services:
docker-compose down
Expected Output:
6. Build and Start Services:
docker-compose up --build -d
Expected Output:
Building web
Step 1/2 : FROM nginx:alpine
---> abcdef123456
...
Successfully built abcdef123456
Creating myproject_web_1 ... done
Creating myproject_db_1 ... done
7. Scale Services:
docker-compose up -d --scale web=3
Expected Output:
Creating network "myproject_default" with the default driver
Creating myproject_db_1 ... done
Creating myproject_web_1 ... done
Creating myproject_web_2 ... done
Creating myproject_web_3 ... done
8. List Docker Compose Version:
docker-compose version
Expected Output:
docker-compose version 1.29.2, build 1110ad01
docker-py version: 5.0.2
CPython version: 3.7.10
9. Pause and Unpause Services:
docker-compose pause
docker-compose unpause
10. Execute Command in a Service Container:
docker-compose exec web ls /usr/share/nginx/html
Expected Output:
index.html
11. Display Compose Configuration:
docker-compose config
Expected Output:
services:
db:
environment:
POSTGRES_DB: mydatabase
POSTGRES_PASSWORD: mypassword
POSTGRES_USER: myuser
image: postgres:latest
networks:
myproject_default:
volumes: null
web:
image: nginx:alpine
networks:
myproject_default:
aliases:
myproject_web_1:web
ports:
- 8080:80
volumes: null
networks:
myproject_default:
external: true
version: '3.0'
12. Build and Push Services Images:
docker-compose build
docker-compose push
These Docker Compose commands provide essential functionality for defining, building, and managing multi-container applications. Adjust the examples based on your specific use case and project structure.
Top comments (0)