Docker Compose

Docker Compose

Docker Compose

Docker Compose is a tool that simplifies the management of multi-container Docker applications. With Compose, you define a multi-container environment in a YAML file (docker-compose.yml) and then use simple commands to build, start, and manage the entire stack. This is particularly useful for applications with multiple components, like a web server, database, cache, and more.

Key Concepts of Docker Compose

  1. Service: A service is a container that you define in your docker-compose.yml file. Each service represents a part of your application, such as a web server or a database.

  2. Network: Docker Compose automatically creates a network for all the services, enabling them to communicate with each other by service names.

  3. Volume: Volumes in Docker Compose allow data persistence across container restarts.

Basic Structure of a docker-compose.yml File

A the docker-compose.yml file is used to define all the services and their configurations. Here’s a simple structure:

version: '3' services: web: image: nginx ports: - "8080:80" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpassword volumes: - db-data:/var/lib/mysql volumes: db-data:

Explanation:

  • version: Specifies the Compose file format version.

  • services: Defines the different containers (services) that make up the application.

    • web: An Nginx container exposed on port 8080.

    • db: A MySQL container with an environment variable for the root password and a volume for persistent data storage.

  • volumes: Defines named volumes (like db-data) for persistent storage.

Docker Compose for a Web App

Let’s create a simple multi-container application with Docker Compose. This application will consist of:

  1. A web service running on an Nginx server.

  2. A database service running MySQL.

1. Create a Docker Compose File (docker-compose.yml)

Create a directory for your project, and inside it create a docker-compose.yml file:

mkdir my-docker-compose-app cd my-docker-compose-app touch docker-compose.yml

Add the following content to docker-compose.yml:

version: '3' services: web: image: nginx container_name: my-web-container ports: - "8080:80" networks: - my-network db: image: mysql:5.7 container_name: my-db-container environment: MYSQL_ROOT_PASSWORD: rootpassword networks: - my-network volumes: - db-data:/var/lib/mysql volumes: db-data: networks: my-network: driver: bridge

Explanation of the Compose File:

  • web:

    • Runs the official Nginx image.

    • Exposes port 8080 on the host to port 80 on the container (Nginx default port).

    • Connects to the my-network network.

  • db:

    • Runs the official MySQL 5.7 image.

    • Sets the root password for MySQL.

    • Attaches to the same my-network network.

    • Uses a named volume db-data for persistent MySQL data.

  • volumes:

    • Defines db-data for persistent MySQL data storage.

  • networks:

    • Defines a custom bridge network (my-network) to allow containers to communicate with each other.

2. Start the Application with Docker Compose

In the same directory as the docker-compose.yml file, run the following command to start the containers:

docker-compose up -d
  • The -d flag runs the containers in detached mode (in the background).

This will download the required images (nginx and mysql:5.7), create the services, and start the containers.

You can check the running containers with:

docker-compose ps

3. Access the Web Service

Once the containers are running, you can access the Nginx web server by opening your browser and navigating to:

http://localhost:8080

This will show the default Nginx welcome page.

4. Access the MySQL Database

If you want to access the MySQL container to check its logs or manage the database, you can use:

docker exec -it my-db-container bash

This opens a bash shell inside the MySQL container. You can then log into MySQL:

mysql -u root -p

Enter the password (rootpassword), and you’ll be inside the MySQL command-line interface.

5. Stopping the Services

To stop the services, you can use:

docker-compose down

This will stop and remove all the containers, networks, and volumes created by docker-compose up.

6. Rebuilding the Services

If you make changes to the docker-compose.yml file (e.g., update the service configuration or add a new service), you can rebuild the services with:

docker-compose up -d --build

This will rebuild the services and apply any changes.

Additional Docker Compose Commands

  • List Running Containers:

    docker-compose ps
  • View Logs for Services:

    docker-compose logs

    To view logs for a specific service:

    docker-compose logs web
  • Scale Services: You can scale services (e.g., running multiple instances of a service):

    docker-compose up -d --scale web=3
  • Run a One-Off Command in a Service: For example, running a shell inside the web service:

    docker-compose exec web bash

Use Cases for Docker Compose

  1. Multi-Container Applications: Compose simplifies defining and running applications with multiple services (e.g., a web server, database, caching, etc.).

  2. Development Environment: Developers use Docker Compose to quickly set up and tear down environments without needing to manually configure each container.

  3. Testing & Continuous Integration: Compose is ideal for spinning up testing environments for unit tests, integration tests, and CI/CD pipelines.

Example: Adding a Simple Web App to Docker Compose

Let’s extend the current docker-compose.yml file by adding a simple web application to the web service. For example, using a simple Node.js app with Express.

  1. Create a Dockerfile for the Node.js App:

    In the same directory, create a Dockerfile For the Node.js app:

    FROM node:14 WORKDIR /app COPY . . RUN npm install CMD ["node", "app.js"]
  2. Create app.js (a simple Express app):

    const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello, Docker Compose!'); }); app.listen(3000, () => { console.log('Server is running on port 3000'); });
  3. Update docker-compose.yml to build the app service from the Dockerfile:

    version: '3' services: web: build: . ports: - "8080:3000" networks: - my-network db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: rootpassword networks: - my-network volumes: - db-data:/var/lib/mysql volumes: db-data: networks: my-network: driver: bridge
  4. Start the Application:

    Now, run the application with:

    docker-compose up -d --build

    Visit http://localhost:8080 In your browser, and you should see Hello, Docker Compose!.

Summary

  • Docker Compose makes managing multi-container applications easy by defining all services, networks, and volumes in a single docker-compose.yml file.

  • You can start, stop, and manage the entire stack with simple commands like docker-compose up and docker-compose down.

  • It's highly useful for development, testing, and CI/CD setups.

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close