Please enable Javascript to view the contents

 ·  ☕ 3 分钟 · 👀... 阅读

Docker Compose基础指令

快速入门

准备工作

1
2
yum install python-pip
yum -y install epel-release

来个练习吧

1.1为项目创建目录

1
2
mkdir composetest
cd composetest

1.2在项目目录中创建一个app.py的文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)

1.3在项目的目录中创建一个requirements.txt文件:

1
2
flask
redis

1.4在项目目录中创建一个Dockerfile文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]

1.5在项目目录中创建一个名为docker-compose.yml文件:

1
2
3
4
5
6
7
8
version: "3.8"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

1.6启动

1
2
3
4
docker-compose build
docker-compose up
-d 后台启动
docker-compose up --build #重新构建

1.7查看网络配置

1
docker network inspect composetest_default

yaml规则

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#3层
version: '3.8'
services:
	服务1:web
	#服务配置
	images
	build
	network
	...
	服务2:redis
	...
	服务3:redis
	...
#其他配置 网络/卷、全局规则
volumes:
networks:
configs:

Docker-Compose常用命令详解

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
docker-compose --help     
Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             文件件文件指定另一个合成文件(default: docker-compose.yml)
  -p, --project-name NAME     项目名称指定备用项目名称(default: directory name)
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert keys
                              in v3 files to their non-Swarm equivalent
  --env-file PATH             Specify an alternate environment file

Commands:
  build      	#生成或重建服务       
  bundle       	#从Compose文件生成Docker bundle     
  config       	#验证并查看撰写文件    
  create        #创建服务   
  down          #停止并删除容器、网络、映像和卷  
  events        #从容器接收实时事件 
  exec          #在正在运行的容器中执行命令 
  help          #获取命令帮助
  images        #列出图像  
  kill          #杀死容器
  logs        	#查看容器的输出
  pause         #暂停服务   
  port       	#打印端口绑定的公共端口 
  ps           	#列出容器  
  pull         	#拉取服务映像  
  push         	#推送服务映像   
  restart      	#重新启动服务   
  rm          	#移除停止的容器
  run         	#运行一次性命令
  scale       	#服务集装箱数量
  start     	#启动服务
  stop          #停止服务 
  top           #显示正在运行的进程
  unpause      	#取消暂停服务
  up         	#创建并启动容器
  version       #显示Docker Compose版本信息

Docker swarm

3.1初始化

1
docker swarm init --advertise-addr 192.168.58.104

3.2获取令牌

1
2
3
4
docker swarm join-token manager #生成管理令牌
docker swarm join-token worker 	#生成一个命令,然后给其他的机器使用。
docker node ls					#查看这个集群下面的节点
docker swarm 

Docker Start

Docker Secret

Docker Config

分享

幽梦
作者
幽梦
傻猪男孩

目录