Detailed explanation of Docker Compose deployment and basic usage

Detailed explanation of Docker Compose deployment and basic usage

1. Docker Compose Overview

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use YAML files to configure your application's services. Then, with a single command, you can create and start all services from the configuration.

Compose works in all environments: production, staging, development, testing, and CI workflows.

Using Compose is basically a three-step process:

  1. Define your application environment in a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your application in docker-compose.yml so that they can run together in isolated environments.
  3. Run docker-compose up and Compose start and run the whole application.

An example of the docker-compose.yml format is as follows:

version: '3'
services:
 web:
  build: .
  ports:
  - "5000:5000"
  volumes:
  - .:/code
  -logvolume01:/var/log
  links:
  - redis
 redis:
  image: redis
volumes:
 logvolume01: {}

Compose has commands to manage the entire lifecycle of your application:

  • Starting, stopping and rebuilding services
  • View the status of running services
  • Streaming log output from running services
  • Run a one-time command on a service

2. Docker Compose Installation

2.1 Binary download and installation

 root@docker01:~# sudo curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
root@docker01:~# sudo chmod +x /usr/local/bin/docker-compose

2.2 Installation with pip (recommended)

root@docker01:~# apt-get -y install python
root@docker01:~# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
root@docker01:~# python get-pip.py #Install PIP
root@docker01:~# pip install docker-compose #Install docker compose
 root@docker01:~# docker-compose version #Verify installation

Three Docker Compose Examples

3.1 Building the Application

root@docker01:~# mkdir composetest #Create Docker Compose directoryroot@docker01:~# cd composetest/
root@docker01:~/composetest# vi app.py

Tip: Use Python to build a simple application. For specific application content, refer to the official example.
https://docs.docker.com/compose/gettingstarted/#step-1-setup

3.2 Create Dockerfile

root@docker01:~/composetest# vi Dockerfile #Use Dockerfile to build the image FROM python:3.4-alpine
RUN mkdir /root/.pip #Create pip source configuration directory ADD pip.conf /root/.pip/pip.conf #Add domestic pip source to the image to be built ADD ./code
WORKDIR /code
RUN pip install -r requirements.txt #Use pip to install according to the file list CMD ["python", "app.py"]

Tip: For the above Dockerfile related commands, refer to "004.Docker Image Management".

root@docker01:~/composetest# vi requirements.txt #Create installation software list file flask
redis
root@docker01:~/composetest# vi pip.conf #Create a file based on domestic pip source [global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

Dockerfile explained:

  1. Build the image starting from the Python 3.4 image.
  2. Create pip configuration directory.
  3. Add the domestic pip source configuration file to the path in /root/.pip/ in your image.
  4. Add the current directory . to the path in the /code image.
  5. Set the working directory to /code.
  6. Install Python related packages.
  7. Set the container’s default command to python app.py.

3.3 Building Services with Docker Compose

root@docker01:~/composetest# vi docker-compose.yml
version: '3'
services:
 web:
  build: .
  ports:
   - "5000:5000"
 redis:
  image: "redis:alpine"

Docker Compose explained:

This Compose file defines two services, web and redis.

Web Services:

  • Use the image built from the Dockerfile in the current directory.
  • Forward the exposed port 5000 on the container to port 5000 on the host. That is, using the default port 5000 of the Flask web server.

redis service:

Use the public Redis image pulled from Docker Hub.

root@docker01:~/composetest# docker-compose up -d #Start building

Four verification confirmation

Browser access: http://172.24.8.111:5000/

root@docker01:~/composetest# docker-compose ps
root@docker01:~/composetest# docker ps 

 root@docker01:~/composetest# docker image ls 


hint:

The container name rule built using Docker Compose is: [directory where the build is located]_[service name defined in the yml build file]_[container startup sequence number].
The naming rule of the image built with Docker Compose is: [directory where the build is located]_[service name defined by the yml build file], and its tag is latest.

Five mount volume construction

root@docker01:~/composetest# vi docker-compose.yml
version: '3'
services:
 web:
  build: .
  ports:
   - "5000:5000"
  volumes:
   - .:/code
 redis:
  image: "redis:alpine"
root@docker01:~/composetest# docker-compose up -d #Build againroot@docker01:~/composetest# vi app.py
…
return 'Hello Docker! I have been seen {} times.\n'.format(count)
…

Browser access: http://172.24.8.111:5000/


Tip: After mounting a local volume to a container, you can quickly modify local files, thereby dynamically modifying the container without rebuilding the image.

6. Other common commands of Docker Compose

docker-compose up -d: runs the service in the background;
docker-compose ps: view the currently running containers;
docker-compose run: Runs a one-time command, such as docker-compose run web env. 

docker-compose stop: stop the service, such as docker-compose stop web

Tip: docker-compose takes the service name in yaml as a parameter, not the container name or ID.

docker-compose down --volumes: Completely delete the container and delete the data volumes used by the container.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of docker-compose deployment php project example
  • Docker-compose one-click deployment of gitlab Chinese version method steps
  • Detailed explanation of software configuration using docker-compose in linux
  • How to deploy gitlab using Docker-compose
  • A brief analysis of the problem of mysql being inaccessible when deployed with docker-compose
  • Detailed tutorial on docker-compose deployment and configuration of Jenkins

<<:  Introduction to the common API usage of Vue3

>>:  Detailed explanation of the four transaction isolation levels in MySQL

Recommend

Mysql error: Too many connections solution

MySQL database too many connections This error ob...

How to clear the cache after using keep-alive in vue

What is keepalive? In normal development, some co...

Detailed explanation of jQuery's copy object

<!DOCTYPE html> <html lang="en"...

Docker nginx example method to deploy multiple projects

Prerequisites 1. Docker has been installed on the...

Solution to the ineffective margin of div nested in HTML

Here's a solution to the problem where margin...

abbr mark and acronym mark

The <abbr> and <acronym> tags represen...

Installing Win10 system on VMware workstation 14 pro

This article introduces how to install the system...

How to use Cron Jobs to execute PHP regularly under Cpanel

Open the cpanel management backend, under the &qu...

Use of MySQL SHOW STATUS statement

To do MySQL performance adjustment and service st...

CSS style does not work (the most complete solution summary in history)

When we write pages, we sometimes find that the C...

Navicat for MySQL 15 Registration and Activation Detailed Tutorial

1. Download Navicat for MySQL 15 https://www.navi...

Analysis of the project process in idea packaging and uploading to cloud service

one. First of all, you have to package it in idea...

Talk about implicit conversion in MySQL

In the course of work, you will encounter many ca...

What magical uses does CSS filter have

background Basic Concepts CSS filter property app...