Here's a detailed docker-compose.yml
file with all possible options and their variations. I'll also provide a README.md
explaining its structure and usage.
Here's a corresponding README.md
:
This docker-compose.yml
file defines a multi-container application setup including Nginx, PostgreSQL, and Redis.
Service | Purpose | Key Configurations |
---|---|---|
web |
Nginx reverse proxy | Exposes port 80 , uses nginx.conf as config, depends on db |
db |
PostgreSQL database | Uses named volume pg_data , environment variables for DB setup |
redis |
Redis caching | Runs on port 6379 , part of the my_network network |
Option | Purpose | Example Value(s) |
---|---|---|
image |
Specifies the base image | nginx:latest , postgres:latest |
container_name |
Custom name for container | my_nginx_container |
restart |
Defines restart behavior | always , unless-stopped , on-failure |
ports |
Maps host ports to container ports | "80:80" , "5432:5432" |
environment |
Sets environment variables | POSTGRES_USER=admin , NGINX_PORT=80 |
volumes |
Persistent storage | pg_data:/var/lib/postgresql/data |
networks |
Custom networking | driver: bridge |
depends_on |
Ensures startup order | depends_on: db |
healthcheck |
Monitors service health | test: ["CMD", "curl", "-f", "http://localhost"] |
docker-compose up -d
docker-compose stop
docker-compose start
docker-compose down
docker-compose logs -f
docker-compose -f docker_compose_file_name.yaml up
for down command
docker-compose -f docker_compose_file_name.yaml down
for stopping all the containers
docker-compose -f docker_compose_file_name.yaml stop
for resuming the execution
docker-compose -f docker_compose_file_name.yaml start
Note: in the above command, replace docker_compose_file_name
with actual file name in which you have written docker-compose.yaml contant.
If each service has its own Dockerfile
, instead of using pre-built images like nginx:latest
, you should specify the build
context in your docker-compose.yml
. This tells Docker Compose to build the image from the given Dockerfile
rather than pulling from Docker Hub.
version: '3.9'
services:
web:
build:
context: ./web # Path where the Dockerfile is located
dockerfile: Dockerfile # Optional, default is "Dockerfile"
container_name: my_nginx_container
restart: always
ports:
- "80:80"
environment:
- NGINX_HOST=localhost
- NGINX_PORT=80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
networks:
- my_network
depends_on:
- db
db:
build:
context: ./db # Path to the DB service's Dockerfile
container_name: my_postgres_container
restart: unless-stopped
ports:
- "5432:5432"
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
POSTGRES_DB: my_database
volumes:
- pg_data:/var/lib/postgresql/data
networks:
- my_network
redis:
build:
context: ./redis # Path to the Redis service's Dockerfile
container_name: my_redis_container
restart: on-failure
ports:
- "6379:6379"
networks:
- my_network
volumes:
pg_data:
networks:
my_network:
driver: bridge
🔹 With this setup, when you run docker-compose up --build
, it will:
- Build the images for
web
,db
, andredis
from their respectiveDockerfile
s. - Use those images to create containers.
No, EXPOSE
is not mandatory if you define ports
in docker-compose.yml
. However, their purposes are different:
Option | Purpose |
---|---|
EXPOSE (in Dockerfile ) |
Tells Docker that the container listens on a specific port. It does not publish the port automatically. It's mainly used for documentation purposes and inter-container communication. |
ports (in docker-compose.yml ) |
Maps a port from the host machine to the container. This is necessary to make a service accessible from outside Docker. |
Dockerfile
FROM node:latest
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 2000 # This does NOT publish the port automatically
CMD ["node", "server.js"]
docker-compose.yml
services:
app:
build: .
ports:
- "2000:2000" # This publishes the port, making it accessible from outside
- If your service only needs inter-container communication,
EXPOSE
is enough. - If you want the service to be accessible externally, you must define
ports
indocker-compose.yml
.
Would you like me to update your docker-compose.yml
to use build
instead of image
for each service? 🚀
Read more with example, Link [click me]