Table of contents
4. Simple rules for YAML files
1. Introduction
Run the container docker run -d image
Based on the dockerfile file Yunxin docker build -f dockerfile file path -t container name .
If you need to run multiple containers, start them one by one, which is troublesome to operate. Therefore, Docker Compose is introduced to manage multiple container startups. Write the yaml configuration file through docker-compose, and you can start and stop all services with one click through compose. !
Three steps to operate Docker Compose
1. Use a dockerfile to define the required content of the application.
2. Use docker- compose.yml to define the services that make up the application and ensure that they can run together in an isolated environment.
3. Use the docker-compose up command to start and run the entire application.
2. Installation
# 下载
➜ ~ sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/dockercompose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# 添加运行权限
➜ ~ sudo chmod +x /usr/local/bin/docker-compose
3. Short answer example
1. Application file app.py
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
2.dockerfile file
FROM python:3.6-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["flask", "run"]
3.requirements.txt file
flask
redis
4.docker-compose.yaml file
version: '3.3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: "redis:alpine"
5. docker-compose up start
Among them, the naming rules for starting services----"folder_service name_num (number of starting services)
6. Check whether there is a configured network
# 查看运行
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5f23dbb8dfc5 composetest_web "flask run" 23 seconds ago Up 22 seconds 0.0.0.0:5000->5000/tcp composetest_web_1
a432fab80dc3 redis:alpine "docker-entrypoint.s…" 10 minutes ago Up 22 seconds 6379/tcp composetest_redis_1
#查看网络
➜ ~ docker network ls
NETWORK ID NAME DRIVER SCOPE
b2de486c0a96 bridge bridge local
5a511e60b5ac composetest_default bridge local
4b6ca6c1af1f docker_standalone-fate-130_default bridge local
75c6b565c73c host host local
9329391ab863 mynet bridge local
0196d25ee724 none null local
# 内部查看详细
➜ ~ docker network inspect 5a511e60b5ac
[
{
"Name": "composetest_default",
"Id": "5a511e60b5ace3984851007d73b04b2c6be795aea5df74cb09c518f93fe5e557",
"Created": "2022-04-14T13:36:51.985458301+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"5f23dbb8dfc5ab38883621811b595f2963b35f454f5ba16b442fcd5d94c0e9fc": {
"Name": "composetest_web_1",
"EndpointID": "d21e5aab735acc27b456fd878659067519f6453db4d8407e4254abd9dcb47f68",
"MacAddress": "02:42:ac:13:00:03",
"IPv4Address": "172.19.0.3/16",
"IPv6Address": ""
},
"a432fab80dc36c3606605aa2e8ea1e30262fc04aec709ce348261fe323c9e479": {
"Name": "composetest_redis_1",
"EndpointID": "25372bb5d7eb7280a1a8d6aa697390a2f63da2e03df4f39f1f9063df36e448c2",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "composetest",
"com.docker.compose.version": "1.25.5"
}
}
]
# ping通
➜ ~ docker exec -it composetest_web_1 ping composetest_redis_1
PING composetest_redis_1 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.110 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.079 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.077 ms
64 bytes from 172.19.0.2: seq=3 ttl=64 time=0.082 ms
^Z64 bytes from 172.19.0.2: seq=4 ttl=64 time=0.080 ms
^C
--- composetest_redis_1 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.077/0.085/0.110 ms
4. Simple rules for YAML files
官网: Compose file version 3 reference | Docker Documentation
The core of the main architecture is three layers
# 版本
version: ''
# 服务
services:
服务1: web
# 服务配置
images
build
network
depends_on
deploy
......
服务2: redis
# 其他配置 网络/卷、全局规则
volumes:
networks:
configs: