Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
Using Compose is basically a three-step process: 使用的三个步骤
Dockerfile
so it can be reproduced anywhere.Dockerfile 保证我们的项目(app)可以在任何地方运行
docker-compose.yml
so they can be run together in an isolated environment.进行配置服务在YAML配置文件当中
docker compose up
and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up
using the docker-compose binary.启动我们的项目(app)
Compose 是Docker官方的一个开源项目,所以需要安装!
配置文件:docker-compose.yml
version: "3.9" # optional since v1.27.0
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
我们的任务就是编写这样一个配置文件,管理我们的多个服务(容器)
实现通过docker-compose up 同时启动多个服务
1、下载Docker-Compose
#安装在Linux系统中
csudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#安装太慢的 把github源换成get.daocloud.io
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
去到安装目录检查:下载成功!
2、设置文件可执行权限
sudo chmod +x /usr/local/bin/docker-compose
3、查看版本信息
docker-compose -version
4、卸载Docker-Compose
sudo rm /usr/local/bin/docker-compose
1、创建目录,所有操作都在这个目录下进行
mkdir composetest
cd composetest
2、创建一个py应用,(类似统计浏览次数,计数器)
import time
import redis #导入了我们的redis
from flask import Flask #导入了我们的Flask(python的一个框架依赖)
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)
3、创建一个配置文本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、创建docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
6、启动
docker-compose up
hh,一键启动两个应用确实舒服!
1、自动下载配置文件docker-compose.yml中的镜像
2、自动生成服务名字 文件名_服务名 _num
Starting composetest_web_1 ... done #web服务
Starting composetest_redis_1 ... done #redis服务
3、网络规则
默认会生成一个网络,10个服务=>网络(项目中的内容都在同一个网络下,容器名访问)
在同一个网络下可以直接通过容器名(域名)访问
4、停止
1、Docker镜像,run ==> 容器
2、Dockerfile构建镜像(服务打包)
3、docker-compose启动项目(编排,多个微服务/环境)
4、Docker网络!
5、项目需要更新,docker compose up --build
即可
核心:编写 docker-compose.yaml
#3层
version: '' #版本
services: #服务
服务1: web
#服务配置 docker容器的配置
images:
build:
network:
depends_on: #depents_on: web服务依赖redis和mysql,让他们先启动!(这就是编排的原因)
-redis
-mysql
服务2: redis
...
服务2: mysql
...
#其他配置 网络/卷、全局规则
volumes:
network:
configs:
1、版本选择
2、服务相关配置
1、创建目录并进入目录
mkdir my_wordpress/
cd my_wordpress/
2、编写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
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:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
3、我们服务器的8000端口放行
4、启动Compose
docker-compose up -d
5、检测容器是否启动
docker ps
6、访问测试:http://qxsong.top:8000/(自己的ip+8000端口)
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/m0_46571920/article/details/121600136
内容来源于网络,如有侵权,请联系作者删除!