好物优选点击查看详情 京东购买

暂无内容

Docker容器之Docker Compose详解

目录

Docker Compose是什么

安装docker-compose

使用docker-compose

常用命令

Compose and WordPress


Docker Compose是什么

Docker Compose是一款容器编排工具,Compose是在一台机器上管理多个容器。

Compose 是 Docker 容器进行编排的工具,定义和运行多容器的应用,可以一条命令启动多个容器,使用Docker Compose不再需要使用shell脚本来启动容器。

Compose 通过一个配置文件来管理多个Docker容器,在配置文件中,所有的容器通过services来定义,然后使用docker-compose脚本来启动、停止和重启应用,和应用中的服务以及所有依赖服务的容器,非常适合组合使用多个容器进行开发的场景。

官网地址:Overview of Docker Compose | Docker Documentation

安装docker-compose

1、从daocloud上下载docker-compose二进制文件安装

[root@docker ~]# 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、添加可执行权限

[root@docker ~]# chmod +x /usr/local/bin/docker-compose

3、测试安装结果

[root@docker ~]# docker-compose --version (-v也可以查看版本) docker-compose version 1.29.2, build 5becea4c

使用docker-compose

1、为docker-compose项目创建一个目录

mkdir compose-test cd compose-test

2、在该目录下创建一个名为 app.py 的文件,并粘贴以下内容

vim app.py(粘贴之前得在命令模式下输入":set paste",然后按i进入输入模式粘贴) 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)

3、在该目录下创建一个名为 requirements.txt 的文件,并粘贴以下内容

flask redis

4、创建一个Dockerfile,并粘贴以下内容

vim 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 的文件,并粘贴以下内容

vim docker-compose.yml version: "3.9" services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine" ports: - "6379:6379" 

这个Compose文件定义了web和redis这两个服务。

6、使用Compose构建并运行程序

[root@manager compose-test]# docker-compose up WARNING: The Docker Engine you're using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`. Creating network "compose-test_default" with the default driver Creating compose-test_web_1 ... done Creating compose-test_redis_1 ... done Attaching to compose-test_web_1, compose-test_redis_1 redis_1 | 1:C 05 Sep 2021 08:28:48.263 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo redis_1 | 1:C 05 Sep 2021 08:28:48.263 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started redis_1 | 1:C 05 Sep 2021 08:28:48.263 # 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 05 Sep 2021 08:28:48.264 * monotonic clock: POSIX clock_gettime redis_1 | 1:M 05 Sep 2021 08:28:48.265 * Running mode=standalone, port=6379. redis_1 | 1:M 05 Sep 2021 08:28:48.265 # 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 05 Sep 2021 08:28:48.265 # Server initialized redis_1 | 1:M 05 Sep 2021 08:28:48.265 # 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 05 Sep 2021 08:28:48.268 * Ready to accept connections web_1 | * Serving Flask app 'app.py' (lazy loading) web_1 | * Environment: development web_1 | * Debug mode: on web_1 | * Running on all addresses. web_1 | WARNING: This is a development server. Do not use it in a production deployment. web_1 | * Running on http://172.19.0.2:5000/ (Press CTRL+C to quit) web_1 | * Restarting with stat web_1 | * Debugger is active! web_1 | * Debugger PIN: 910-519-966 [root@manager compose-test]# docker-compose up -d Compose启动的容器以后台方式运行 Starting compose-test_redis_1 ... done Starting compose-test_web_1 ... done

web端口访问测试

Docker容器之Docker Compose详解插图

常用命令

docker-compose up # 创建并启动容器 docker-compose ps # 显示所有容器 docker-compose down # 停止和删除容器、网络、卷、镜像,这些内容是通过docker-compose up命令创建的. 默认值删除容器、网络,可以通过指定rmi、volumes参数删除镜像和卷 docker-compose stop # 停止所有服务(容器) docker-compose top # 显示正在运行的进程

Compose and WordPress

了解如何使用Compose来设置和运行WordPress

WordPress:可以快速搭建一个自己的网站的框架

快速入门指南:Quickstart: Compose and WordPress | Docker Documentation

1、新建一个目录 # cd my_wordpress 2、在该目录下创建一个名为docker-compose.yml的文件 # vim docker-compose.yml version: "3.9" services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: 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:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: db_data: {} wordpress_data: {} 3、创建并启动WordPress容器 [root@manager my_wordpress]# docker-compose up -d WARNING: The Docker Engine you're using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`. Creating network "my_wordpress_default" with the default driver Creating volume "my_wordpress_db_data" with default driver Creating volume "my_wordpress_wordpress_data" with default driver Creating my_wordpress_db_1 ... done Creating my_wordpress_wordpress_1 ... done 4.web端口访问WordPress http://localhost:8000

Docker容器之Docker Compose详解插图1

Docker容器之Docker Compose详解插图2

访问成功,说明已成功运行WordPress。

原文链接:https://blog.csdn.net/weixin_44411385/article/details/123993812?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168623078216800226541510%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168623078216800226541510&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-16-123993812-null-null.268%5Ev1%5Ekoosearch&utm_term=docker%E3%80%81wordpress%E3%80%81wordpress%E5%BB%BA%E7%AB%99%E3%80%81wordpress%E4%B8%BB%E9%A2%98%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享