目录
1、compose
1.1、compose的好处
1.2、安装compose
1.3、使用compose
Step 1: Setup
Step 2: Create a Dockerfile
Step 3: Define services in a Compose file
Step 4: Build and run your app with Compose
1.4、用compose启动一个wordpress
1、compose
是docker官方推出的一个用python编写的容器编排工具。可以理解为启动容器的脚本,在脚本里指明启动容器的顺序,启动多少容器
那么有以下问题
- 对容器进行什么编排操作呢?
启动容器,可以指定端口、卷、链接、使用哪个镜像等
docker run -dp 8080:80 -v /web:/usr/local/bginx/html --name sc-nginx-1 nginx:1.12.1
- 对多少容器进行编排操作呢?
>=1
- 对多少台宿主机上的容器进行编排操作呢?
一台
还有两个比较厉害的软件swarm和k8s
这三个都是容器编排的工具,他们的区别是:
- compsose:单台机器上编排容器
- swarm:多台机器上编排容器
- k8s:多台机器上的编排容器,性能和功能比swarm更好
1.1、compose的好处
- 可以快速批量启动容器,效率高
- 不容易出错,可靠性高
1.2、安装compose
[root@centos7-docker nginx1]# uname -s
Linux
[root@centos7-docker nginx1]# uname -m
x86_64
[root@centos7-docker nginx1]# uname -r
3.10.0-1160.el7.x86_64
[root@centos7-docker nginx1]# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 若是上边那个命令安装不了可以使用
[root@centos7-docker nginx1]# curl -L "https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@centos7-docker nginx1]# chmod +x /usr/local/bin/docker-compose
# 授予可执行权限
[结果查看]
[root@centos7-docker nginx1]# docker-compose -v
docker-compose version 1.29.2, build 5becea4c
1.3、使用compose
官方参考资料:Get started with Docker Compose | Docker Documentation
Step 1: Setup
1、Create a directory for the project:
[root@centos7-docker nginx1]# mkdir /composetest
[root@centos7-docker nginx1]# cd /composetest
注意:下面的操作都是在/composetest里边完成的
2、Create a file called app.py
in your project directory and paste this in:
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)
In this example, redis
is the hostname of the redis container on the application’s network. We use the default port for Redis, 6379
.(在这个例子中,redis是应用程序网络中redis容器的主机名。我们使用Redis的默认端口6379。)
3、Create another file called requirements.txt
in your project directory and paste this in:
flask
redis
Step 2: Create a Dockerfile
In your project directory, create a file named Dockerfile
and paste the following:
# 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"]
This tells Docker to:
- Build an image starting with the Python 3.7 image.
- Set the working directory to
/code
. - Set environment variables used by the
flask
command. - Install gcc and other dependencies
- Copy
requirements.txt
and install the Python dependencies. - Add metadata to the image to describe that the container is listening on port 5000
- Copy the current directory
.
in the project to the workdir.
in the image. - Set the default command for the container to
flask run
.
For more information on how to write Dockerfiles, see the Docker user guide and the Dockerfile reference.
Step 3: Define services in a Compose file
Create a file called docker-compose.yml
in your project directory and paste the following:
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
#我们可以在这里加一行这个,若是没有加也没有关系,因为redis的默认端口就是6379
ports:
- "6379:6379"
This Compose file defines two services: web
and redis
.(这个Compose文件定义了两个服务:web和redis。)这里同一个缩进的就是同级的
[Web service]
The web
service uses an image that’s built from the Dockerfile
in the current directory. It then binds the container and the host machine to the exposed port, 8000
. This example service uses the default port for the Flask web server, 5000
.
web服务使用从当前目录中的Dockerfile构建的映像。然后,它将容器和主机绑定到暴露的端口8000。本示例服务使用Flask web服务器的默认端口5000。
[Redis service]
The redis
service uses a public Redis image pulled from the Docker Hub registry. redis服务使用一个从Docker Hub注册表中提取的公共redis镜像。
若是你想对yaml语法更加的了解可以去这里:YAML 入门教程 | 菜鸟教程
[简单提一下yaml的基本语法]
- 大小写敏感
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
Step 4: Build and run your app with Compose
- From your project directory, start up your application by running
docker-compose up
.
# 可以在这个命令后边加一个 "-d",这样的话你的前台就不会被占用了
$ docker-compose up
Creating network "composetest_default" with the default driver
Creating composetest_web_1 ...
Creating composetest_redis_1 ...
Creating composetest_web_1
Creating composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
redis_1 | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
web_1 | * Restarting with stat
redis_1 | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.
redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
web_1 | * Debugger is active!
redis_1 | 1:M 17 Aug 22:11:10.483 # Server initialized
redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
web_1 | * Debugger PIN: 330-787-903
redis_1 | 1:M 17 Aug 22:11:10.483 * Ready to accept connections
Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In this case, the code is statically copied into the image at build time.
[启动效果]
[root@centos7-docker composetest]# docker-compose up -d # "-d",让compose在后台运行就不会占用你的前台
Creating network "composetest_default" with the default driver
Creating composetest_web_1 ... done
Creating composetest_redis_1 ... done
[root@centos7-docker composetest]# ls
app.py docker-compose.yml Dockerfile requirements.txt
[root@centos7-docker composetest]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3baf2ad4957 redis:alpine "docker-entrypoint.s…" 22 seconds ago Up 21 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp composetest_redis_1
ade540a6b78f composetest_web "flask run" 22 seconds ago Up 21 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp composetest_web_1
关闭可以使用"docker-compose down",它会关闭并且删掉容器
[root@centos7-docker composetest]# docker-compose down
Stopping composetest_redis_1 ... done
Stopping composetest_web_1 ... done
Removing composetest_redis_1 ... done
Removing composetest_web_1 ... done
[root@centos7-docker composetest]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@centos7-docker composetest]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
进入容器可以使用"docker-compose run web /bin/sh"
[root@centos7-docker composetest]# docker-compose run web /bin/sh # 进入web容器里边
Creating composetest_web_run ... done
/code # cat /etc/issue # 看版本号
Welcome to Alpine Linux 3.15
Kernel \r on an \m (\l)
/code # exit
docker-compose ps 和 docker-compose top
[root@centos7-docker composetest]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------
composetest_redis_1 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp,:::6379->6379/tcp
composetest_web_1 flask run Up 0.0.0.0:5000->5000/tcp,:::5000->5000/tcp
[root@centos7-docker composetest]# docker-compose top
composetest_redis_1
UID PID PPID C STIME TTY TIME CMD
------------------------------------------------------------------------
polkitd 6953 6907 0 13:17 ? 00:00:00 redis-server *:6379
composetest_web_1
UID PID PPID C STIME TTY TIME CMD
------------------------------------------------------------------------------------------------
root 6947 6906 0 13:17 ? 00:00:00 /usr/local/bin/python /usr/local/bin/flask run
注意:若是你使用的是校园网,且在第四步出现什么下载不了的话,你可以使用手机热点下载试试,校园网是真拉跨!
1.4、用compose启动一个wordpress
参考文件:Quickstart: Compose and WordPress | Docker Documentation
第一步:创建文件夹
# 可以在之前我们那个/composetest里边创建这个文件夹
mkdir my_worldpress
cd my_worldpress
第二步:编写docker-compose.yml文件
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress(这里把"someworldpress"删掉,设置自己的密码)123456
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
第三步:"docker-compose up -d" 在后台启动
[root@centos7-docker my_worldpress]# docker-compose up -d
Creating network "my_worldpress_default" with the default driver
Creating my_worldpress_db_1 ... done
Creating my_worldpress_wordpress_1 ... done
[使用wordpress]
启动之后,我们用"IP地址:端口号"的方式在网站里访问,会得到下面这个界面,选择中文即可
选择之后,输入用户名和密码,要记住用户名和密码待会要用的。登录之后是这个界面
你可以自己修改着玩,我修改之后重新访问"IP地址:端口号"的界面是这样的。
🚀[删除wordpress的数据]🚀
若是我们想要格式化这个wordpress,必须使用"docker-compose down --volumes",使用之后,会删除容器,并且删除容器相关的卷和网络等会 。访问的时候,若是一直是这个画面,那么是因为缓存的原因,你需要多等一会,然后在刷新,然后就能成功了。