-
Notifications
You must be signed in to change notification settings - Fork 0
Docker Compose
PotatoScript edited this page Feb 16, 2025
·
1 revision
Docker Compose is a tool used to define and manage multi-container Docker applications. With docker-compose.yml, you can configure all your application's services (e.g., app, database, cache) in a single YAML file and run them together.
- Download Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose - Make it executable:
sudo chmod +x /usr/local/bin/docker-compose
- Verify installation:
docker-compose --version
A typical docker-compose.yml file has the following structure:
version: '3.8' # Define the Compose file format version
services:
<service_name>:
image: <image_name>
build: <build_context>
ports:
- "<host_port>:<container_port>"
volumes:
- "<host_path>:<container_path>"
environment:
<key>: <value>
depends_on:
- <other_service_name>| Command | Description |
|---|---|
docker-compose up |
Build and start containers. |
docker-compose up -d |
Start containers in detached mode (background). |
docker-compose down |
Stop and remove containers, networks, and volumes. |
docker-compose build |
Build or rebuild services. |
docker-compose ps |
List running containers. |
docker-compose logs |
Show logs for containers. |
docker-compose stop |
Stop running containers without removing them. |
docker-compose restart |
Restart services. |
This example creates a Node.js application with a MongoDB database:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- NODE_ENV=development
depends_on:
- db
db:
image: mongo:4.4
container_name: mongodb
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db
volumes:
mongo_data:-
Create a
Dockerfilefor theappservice:FROM node:16 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["npm", "start"]
-
Run
docker-compose upto start both services:docker-compose up
-
Access the application at
http://localhost:3000.
-
Version Pinning: Always specify the Compose version (e.g.,
version: '3.8') for compatibility. -
Use
.envFiles: Store environment variables in a.envfile for easier management:- Example
.envfile:NODE_ENV=production DB_HOST=mongodb
- Reference in
docker-compose.yml:environment: - NODE_ENV=${NODE_ENV} - DB_HOST=${DB_HOST}
- Example
- Leverage Volumes: Use named volumes for persistent data.
-
Use Networks for Isolation: Specify networks to isolate services when needed.
networks: default: name: custom_network
-
Keep
docker-compose.ymlClean: Use separate Compose files for development and production:docker-compose -f docker-compose.dev.yml up
version: '3.8'
services:
wordpress:
image: wordpress:latest
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: user
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpress
depends_on:
- db
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: wordpress
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:- Save the above configuration as
docker-compose.yml. - Run the setup:
docker-compose up
- Access WordPress at
http://localhost:8080.
- Check logs of a specific service:
docker-compose logs <service_name>
- Execute a command inside a running container:
Example:
docker-compose exec <service_name> <command>
docker-compose exec app bash