Skip to content

Latest commit

 

History

History
192 lines (153 loc) · 5.6 KB

File metadata and controls

192 lines (153 loc) · 5.6 KB

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:


Docker Compose Setup

Overview

This docker-compose.yml file defines a multi-container application setup including Nginx, PostgreSQL, and Redis.

Services & Configuration

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

Key Options Explained

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"]

Usage

Start the Services

docker-compose up -d

Stop the Services

docker-compose stop 

to restart the services

docker-compose start

to stop and remove the containers

docker-compose down

View Logs

docker-compose logs -f

If you have made docker-compose.yaml file with some other name, run it with following command

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.

Docker-compose using dockerfile

1️⃣ What if we have a Dockerfile for each service?

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.

🔹 Example of Using Dockerfiles for Each Service

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, and redis from their respective Dockerfiles.
  • Use those images to create containers.

2️⃣ Is it necessary to use EXPOSE in a Dockerfile if we already define ports in docker-compose.yml?

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.

🔹 Example: EXPOSE vs. ports

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

Conclusion

  • If your service only needs inter-container communication, EXPOSE is enough.
  • If you want the service to be accessible externally, you must define ports in docker-compose.yml.

Would you like me to update your docker-compose.yml to use build instead of image for each service? 🚀


Maintaining secretes with secrets section and .txt file for storing secrets

Read more with example, Link [click me]