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:
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:
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. 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:
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:
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]. 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:
|
<<: Introduction to the common API usage of Vue3
>>: Detailed explanation of the four transaction isolation levels in MySQL
MySQL database too many connections This error ob...
What is keepalive? In normal development, some co...
Concept introduction : 1. px (pixel): It is a vir...
<!DOCTYPE html> <html lang="en"...
Prerequisites 1. Docker has been installed on the...
Here's a solution to the problem where margin...
The <abbr> and <acronym> tags represen...
This article introduces how to install the system...
Open the cpanel management backend, under the &qu...
To do MySQL performance adjustment and service st...
When we write pages, we sometimes find that the C...
1. Download Navicat for MySQL 15 https://www.navi...
one. First of all, you have to package it in idea...
In the course of work, you will encounter many ca...
background Basic Concepts CSS filter property app...