一、Dockerfile的构建
1、指定镜像
采用FROM指令:
FROM 指定镜像名称
2、设置标签信息
对镜像进行备注说明:
LABEL manager="admin" \
label1="docker build1" \
lable2="docker build2"
3、运行命令
容器运行后,执行的指令
RUN echo "docker build run."
4、容器启动指令
CMD sleep 6;echo start success
5、Dockerfile 的编写示例
# Dockerfile使用的镜像
FROM alpine
#镜像标签配置
LABEL manager="admin" \
label1="docker build1" \
lable2="docker build2"
#运行的指令
RUN echo "docker build run."
#容器启动后的执行命令
CMD sleep 6;echo start success
Dockerfile 主要分为四部分: 镜像信息 、维护信息、操作指令、启动时执行指令。
CMD与ENTRYPOINT的差异:
1. CMD指令指定的容器启动时命令可以被docker run指定的命令覆盖,而ENTRYPOINT指令指定的命令不能被覆盖,而是将docker run指定的参数当做ENTRYPOINT指定命令的参数;
例:
CMD ["ls","-a"]
docker run xxx -l 会替换为 -l 执行, 会报错。
2. CMD指令可以为ENTRYPOINT指令设置默认参数,而且可以被docker run指定的参数覆盖;
例:
ENTRYPOINT ["ls","-a"]
docker run xxx -l 会拼接为 ls -al执行。
如果运行JAVA程序, 增加ENTRYPOINT:
ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom
$JAVA_OPTS -jar /app.jar $PARAMS" ]
# 运行命令 docker run -e JAVA_OPTS="-Xmx512m -Xms33 -" -e PARAMS="--
spring.profiles=dev --server.port=8080" -jar /app/app.jar
6、运行容器
docker run --name test1 -it dockerfile:test1
7、镜像的存储
查看镜像信息:
docker image inspect my-wizard:0.1
存储目录结构:
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/b7e19d896235074801e65678900f1eb9e3d42a15961e1c5776e821a48c508602/diff:/var/lib/docker/overlay2/94c244b15f5d5290dc48ea41fe90e30292169c1ad1316b4b4508a26043a03889/diff:/var/lib/docker/overlay2/51e4f5054f8035f78df429174458d3511c75086bacd9bc22727ba3b4bdf1ee17/diff:/var/lib/docker/overlay2/fe000be05464f9548d95b24c0773ec5fbefd9f18bc6b393d2fe2731e9bd5b929/diff:/var/lib/docker/overlay2/d257f693e8aa47470bb2a0ea82fcc833d5560e3b471cb008b8d00ff29979e90c/diff:/var/lib/docker/overlay2/3648999483d13c18d34f00d90d843b3aca2abfdc946180730593fecb8585c673/diff:/var/lib/docker/overlay2/3f469b0f602b3f8bf2d85a867652cb045c86bb44da93599a440c6af66c8c4702/diff:/var/lib/docker/overlay2/74114f4a8911f8bee5a793d16b9e66417033aaa3b2763920fd4e7203f2a83aeb/diff:/var/lib/docker/overlay2/7fe7e08fc852d57d1cdf3d8ffce3969da42af3f7c82f01b06501364f86cd615c/diff:/var/lib/docker/overlay2/b59d29226dd54f956383976ee17c2098e351fe73ec27794e35740a4af0467a19/diff:/var/lib/docker/overlay2/83054ea9dfefb4e0f666a640f82476be7b6ee2f0e050b9afc486a7eef755a0f1/diff:/var/lib/docker/overlay2/298e882043803bbafefbbcdd98cdc78df14f4bfa789867be918c60291d1b2ba4/diff:/var/lib/docker/overlay2/2eed01572ca6f1212afceff327c71b4d6bb92033b17ce7ec1476d72afed7b194/diff:/var/lib/docker/overlay2/3284164b93f7945defcbb61bb7eb0d78989b69acd0f20acb2a04b3ac9f6478f1/diff:/var/lib/docker/overlay2/2927eeb30fe0e11cb65dd08a196b9f63e9fd5f6e689aa2e71c58be47d043bef9/diff:/var/lib/docker/overlay2/92a64758e128809c3d6a5ce4bda56f231d969c9915cdc6891031dfaa13c5bf40/diff:/var/lib/docker/overlay2/ad0138bb13e3a6549a967a03be1338680fa79d65f77df0a24e3e3905fa31bb49/diff:/var/lib/docker/overlay2/3dd833c5349e77df38958ab59080311227c84dffd9e85d476d137ad5404a8417/diff:/var/lib/docker/overlay2/19b0730a578cbdf1bde44a7c0296c6dcff39d46154435370231e0b090f2989f8/diff:/var/lib/docker/overlay2/a4b2e0843b03de43dec536f958cb46072c8b5f33bc4cc2429eb92e098d6c3869/diff:/var/lib/docker/overlay2/e659df56cceb80589bfd6d68df3ef703773450158f4f682f5c2b3ac05308264d/diff:/var/lib/docker/overlay2/2e950590bd3aff9d24c436e45a8f53afbc6a5dec7c3cf86223473fa6953dffda/diff:/var/lib/docker/overlay2/f82ab6a6e0611a78a95611d281245b2c2edfaa273d29d88de989b081f79651bb/diff:/var/lib/docker/overlay2/c0c628c8cdf5641f1642eff0f42a5f12ab178cb596e6bce7a86efe725a7da333/diff:/var/lib/docker/overlay2/1041b58a07cfbf956c4a2a2ff2adc93ef1e3f85813dab0bac069452837c5f9fa/diff:/var/lib/docker/overlay2/0247958590c10ef1c20789c2131413b668357348af09955ac8f118b8723d7f5f/diff:/var/lib/docker/overlay2/a8896150c3e359092e2e6d62b755c440e13ee4f8db990135800e0ef46d7894c1/diff",
"MergedDir": "/var/lib/docker/overlay2/66e9b11cf699cbdc26eac8dfd363274ac0cf601804ae79999c31265899204d4a/merged",
"UpperDir": "/var/lib/docker/overlay2/66e9b11cf699cbdc26eac8dfd363274ac0cf601804ae79999c31265899204d4a/diff",
"WorkDir": "/var/lib/docker/overlay2/66e9b11cf699cbdc26eac8dfd363274ac0cf601804ae79999c31265899204d4a/work"
},
"Name": "overlay2"
},
二、Docker Compose的使用
1、安装Docker Compose
进入
https://github.com/docker/compose/releases检索docker引擎所对应的版本:
2、执行安装:
curl -SL https://github.com/docker/compose/releases/download/1.29.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
3、创建一个python应用脚本
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)
创建依赖信息文件:
requirements.txt
flask
redis
4、创建Dockerfile文件
# syntax=docker/dockerfile:1
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"]
5、执行构建
创建名称为web的镜像
docker build -t web .
6、创建docker-compose配置文件
version: "3.9" #指定版本号;查看文档https://docs.docker.com/compose/compose-file/
services: #所有需要启动的服务
web: #第一个服务的名字
build: # 可手动执行创建镜像: docker build -t xxx -f Dockerfile .
dockerfile: Dockerfile
context: .
image: 'hello:py'
ports: #指定启动容器暴露的端口
- "5000:5000"
redis: #第二个服务的名字
image: "redis:alpine"
7、启动执行:
通过命令启动: docker-compose up
加上-d参数, 以后台方式启动。
[root@localhost composetest]# docker-compose up
Starting composetest_web_1 ... done
Starting composetest_redis_1 ... done
Attaching to composetest_redis_1, composetest_web_1
redis_1 | 1:C 04 Nov 2022 17:19:23.281 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 04 Nov 2022 17:19:23.281 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 04 Nov 2022 17:19:23.281 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 04 Nov 2022 17:19:23.281 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 04 Nov 2022 17:19:23.282 * Running mode=standalone, port=6379.
redis_1 | 1:M 04 Nov 2022 17:19:23.282 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 04 Nov 2022 17:19:23.282 # Server initialized
redis_1 | 1:M 04 Nov 2022 17:19:23.282 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 04 Nov 2022 17:19:23.282 * Loading RDB produced by version 7.0.5
redis_1 | 1:M 04 Nov 2022 17:19:23.282 * RDB age 605 seconds
redis_1 | 1:M 04 Nov 2022 17:19:23.282 * RDB memory usage when created 0.82 Mb
redis_1 | 1:M 04 Nov 2022 17:19:23.282 * Done loading RDB, keys loaded: 1, keys expired: 0.
redis_1 | 1:M 04 Nov 2022 17:19:23.282 * DB loaded from disk: 0.000 seconds
redis_1 | 1:M 04 Nov 2022 17:19:23.282 * Ready to accept connections
web_1 | * Serving Flask app 'app.py'
web_1 | * Debug mode: off
web_1 | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web_1 | * Running on all addresses (0.0.0.0)
web_1 | * Running on http://127.0.0.1:5000
web_1 | * Running on http://172.18.0.3:5000
web_1 | Press CTRL+C to quit
8、验证访问:
[root@localhost bin]# curl http://127.0.0.1:5000
Hello World! I have been seen 4 times.
[root@localhost bin]# curl http://172.18.0.3:5000
Hello World! I have been seen 5 times.
9、停止服务
[root@localhost composetest]# docker-compose stop redis
Stopping composetest_redis_1 ... done
[root@localhost composetest]# docker-compose stop web
Stopping composetest_web_1 ... done
这里的服务名字与docker-compose配置文件中的名称对应。
三、Docker Swarm集群模式
1、创建Dockerfile
创建一个需要采用集群模式部署的Dockerfile:
version: "3.7"
services:
app: #定义应用程序的容器
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
networks: # 加入application和databases两个网络
- application
- databases
deploy: #安装docker swarm
replicas: 6 #指定副本:部署到加入swarm集群的不同的节点(负载均衡+高可用)
mysql: # 定义mysql数据库的容器
image: mysql:5.7
volumes:
- mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
networks: #这个服务加入hello自定义网络
- databases
deploy: #安装docker swarm
replicas: 6 #指定副本:处于不同的服务器(负载均衡+高可用)
redis: # 定义redis缓存的容器
image: redis
networks:
- application
# 定义全局的配置信息,包括volumes与networks
volumes:
mysql-data:
networks:
application:
databases:
2、集群初始化
主节点进行初始化:
[root@localhost ~]# docker swarm init
Swarm initialized: current node (oyje6kmyqnot8fop89msrs1wf) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1iq2u7f21fty2wyih2xh6j8jzb87uiwi2gpqclg77ykii642kz-bupmi5rixs4a7zaho7i8o8kjf 10.10.20.15:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[root@localhost ~]#
其他节点加入执行:
docker swarm join --token SWMTKN-1-1iq2u7f21fty2wyih2xh6j8jzb87uiwi2gpqclg77ykii642kz-bupmi5rixs4a7zaho7i8o8kjf 10.10.20.15:2377
3、多个主节点的加入配置(高可用)
在集群主节点执行:
[root@localhost ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1iq2u7f21fty2wyih2xh6j8jzb87uiwi2gpqclg77ykii642kz-1dturcr1g93eak84d2jlvfbop 10.10.20.15:2377
在其他集群主节点执行
docker swarm join --token SWMTKN-1-1iq2u7f21fty2wyih2xh6j8jzb87uiwi2gpqclg77ykii642kz-1dturcr1g93eak84d2jlvfbop 10.10.20.15:2377