Day 9 - The Docker Compose File

A Docker Compose file is a YAML file that defines the configuration of your Docker application. It specifies the services, networks, volumes, secrets, and other components that make up your application. You can use a Docker Compose file to run multiple containers with a single command: docker compose up.

A Docker Compose file has several advantages, such as:

•  It simplifies the creation and management of multi-container applications.

•  It allows you to define your application stack in a declarative and human-readable way.

•  It supports environment variables, secrets, and dependencies between services.

•  It enables you to scale, update, and restart your application easily.

For Syntax and all important updates of docker-compose please check official doc file as the Docker Compose file

A Docker Compose file has a specific structure and syntax that follows the Compose Specification. The specification defines the following top-level elements:

•  version: The version of the Compose file format. The latest and recommended version is 3.9.

•  name: An optional name for the application.

•  services: A list of services (or containers) that make up the application. Each service has its own attributes, such as image, ports, volumes, networks, etc.

•  networks: A list of networks that connect the services. Each network has its own attributes, such as driver, ipam, etc.

•  volumes: A list of volumes that persist data for the services. Each volume has its own attributes, such as driver, driver_opts, etc.

•  configs: A list of configuration files that can be mounted into the services. Each config has its own attributes, such as file, external, etc.

•  secrets: A list of secrets that can be accessed by the services. Each secret has its own attributes, such as file, external, etc.

Here is a sample docker-compose.yaml file for a 3 tier application that consists of a web server, an application server, and a database server.

version: '3.9'  #The version of the Docker Compose document"
services:       #The services in your project#
web:
image: nginx
ports:
  80:80
 
volumes:       #Named volumes that are shared among multiple services#
  ./web:/usr/share/nginx/html

depends_on:
   app

app:
image: node:18-alpine
working_dir: /app
volumes:
  ./app:/app

environment:
 DB_HOST=db
 
 DB_USER=root
 
 DB_PASSWORD=secret
 
 DB_NAME=todos

command: sh -c "yarn install && yarn run start"
depends_on:
 db

db:
image: mysql:8.0
environment:
 MYSQL_ROOT_PASSWORD=secret
 
 MYSQL_DATABASE=todos

volumes:
 ./db:/docker-entrypoint-initdb.d
Details about this Sample Docker-Compose.yaml file as below.

This file defines three services: web, app, and db.
  • The web service uses the nginx image and exposes port 80 to the host. It also mounts the web directory as a volume to serve static files.
  • The app service uses the node:18-alpine image and runs a Node.js application from the app directory. It also sets some environment variables to connect to the db service.
  • The db service uses the mysql:8.0 image and creates a database called todos. It also mounts the db directory as a volume to initialize the database schema.
Docker Compose Practice Project

This tutorial is designed to introduce the key concepts of Docker Compose whilst building a simple Python web application. The application uses the Flask framework and maintains a hit counter in Redis.

Download attached file for this test docker-compose practice.Docker_Compose_Practice

Details about attached Dockerfile.

Build an image starting with the Python 3.7 image.

Set the working directory to /code.

Set environment variables used by the flask command.

Install gcc and other dependencies

Copy requirements.txt and install the Python dependencies.

Add metadata to the image to describe that the container is listening on port 5000

Copy the current directory . in the project to the workdir . in the image.

Set the default command for the container to flask run.

This Compose file defines two services: web and redis.

The Docker-Compose File details as below

This Compose file defines two services: web and redis.

The web service uses an image that’s built from the Dockerfile in the current directory. It then binds the container and the host machine to the exposed port, 8000. This example service uses the default port for the Flask web server, 5000.

The redis service uses a public Redis image pulled from the Docker Hub registry.

Let's run this Docker-Compose file as below.

PS D:\Docker> docker compose up
[+] Running 7/7
 ✔ redis 6 layers [⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                            17.2s 
   ✔ 31e352740f53 Pull complete                                                                               3.4s 
   ✔ 029a81f05585 Pull complete                                                                               3.4s 
   ✔ 7aaf69037d81 Pull complete                                                                               4.1s 
   ✔ 2bfe6b931134 Pull complete                                                                               4.8s 
   ✔ 9528a9e21ebd Pull complete                                                                               4.8s 
...
...
docker-web-1    | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
docker-web-1    |  * Running on all addresses (0.0.0.0)
docker-web-1    |  * Running on http://127.0.0.1:5000
docker-web-1    |  * Running on http://172.18.0.3:5000
docker-web-1    | Press CTRL+C to quit
docker-web-1    | 172.18.0.1 - - [25/Jul/2023 08:16:26] "GET / HTTP/1.1" 200 -
docker-web-1    | 172.18.0.1 - - [25/Jul/2023 08:16:27] "GET /favicon.ico HTTP/1.1" 404 -
docker-web-1    | 172.18.0.1 - - [25/Jul/2023 08:16:31] "GET / HTTP/1.1" 200 -
docker-web-1    | 172.18.0.1 - - [25/Jul/2023 08:16:33] "GET / HTTP/1.1" 200 -
Gracefully stopping... (press Ctrl+C again to force)
Aborting on container exit...
[+] Stopping 2/2
 ✔ Container docker-redis-1  Stopped                                                                          0.5s 
 ✔ Container docker-web-1    Stopped                                                                         10.6s 
canceled
PS D:\Docker> 
Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In this case, the code is statically copied into the image at build time.

Open below URL and click on refresh 2 times , you will see numbers are increasing.

So in above example we saw that we dont need to create multiples files for every component and this got taken care by Docker-Compose.yaml file.

Basic Commands in Docker Compose

Start all services: Docker Compose up

Stop all services: Docker Compose down

Install Docker Compose using pip: pip install -U Docker-compose

Check the version of Docker Compose: Docker-compose-v

Run Docker Compose file: Docker-compose up -d

List the entire process: Docker ps

Scale a service - Docker Compose up -d -scale

Use YAML files to configure application services - Docker Compose.yml





No comments:

Post a Comment

Total Pageviews