![[docker]九、compose,第1张 [docker]九、compose,第1张](/aiimages/%5Bdocker%5D%E4%B9%9D%E3%80%81compose.png)
是docker官方推出的一个用python编写的容器编排工具。可以理解为启动容器的脚本,在脚本里指明启动容器的顺序,启动多少容器
那么有以下问题
- 对容器进行什么编排 *** 作呢?
启动容器,可以指定端口、卷、链接、使用哪个镜像等
docker run -dp 8080:80 -v /web:/usr/local/bginx/html --name sc-nginx-1 nginx:1.12.1
- 对多少容器进行编排 *** 作呢?
>=1
- 对多少台宿主机上的容器进行编排 *** 作呢?
一台
还有两个比较厉害的软件swarm和k8s
1.1、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: Setup1、Create a directory for the project:
[root@centos7-docker nginx1]# mkdir /composetest
[root@centos7-docker nginx1]# cd /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
flaskcommand. - Install gcc and other dependencies
- Copy
requirements.txtand 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.
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)