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

JavaScript determines whether the browser is IE

As a front-end developer, I can’t avoid IE’s pitf...

Mobile development tutorial: Summary of pixel display issues

Preface I believe that in the process of mobile t...

In-depth understanding of the role of Vuex

Table of contents Overview How to share data betw...

Specific use of Docker anonymous mount and named mount

Table of contents Data volume Anonymous and named...

How to separate static and dynamic state by combining Apache with Tomcat

Experimental environment Apache and Tomcat are bo...

SSM implements the mysql database account password ciphertext login function

introduction Our company is engaged in the resear...

Cross-domain issues in front-end and back-end separation of Vue+SpringBoot

In the front-end and back-end separation developm...

Vue uses better-scroll to achieve horizontal scrolling method example

1. Implementation principle of scrolling The scro...

How to configure the My.ini file when installing MySQL5.6.17 database

I recently used the MySql database when developin...

Detailed explanation of Mencached cache configuration based on Nginx

Introduction Memcached is a distributed caching s...

js to achieve sliding carousel effect

This article shares the specific code of js to ac...

What to do if you forget the initial password when installing MySQL on Mac

Forgetting the password is a headache. What shoul...