目录

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. 不容易出错,可靠性高

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 timeimport redis
from flask import Flaskapp = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)def get_hit_count():retries = 5while True:try:return cache.incr('hits')except redis.exceptions.ConnectionError as exc:if retries == 0:raise excretries -= 1time.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的默认端口就是6379ports:- "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

  1. 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 psName                      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_1UID     PID    PPID   C   STIME   TTY     TIME             CMD
------------------------------------------------------------------------
polkitd   6953   6907   0   13:17   ?     00:00:00   redis-server *:6379composetest_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.7volumes:- db_data:/var/lib/mysqlrestart: alwaysenvironment:MYSQL_ROOT_PASSWORD: somewordpress(这里把"someworldpress"删掉,设置自己的密码)123456MYSQL_DATABASE: wordpressMYSQL_USER: wordpressMYSQL_PASSWORD: wordpresswordpress:depends_on:- dbimage: wordpress:latestvolumes:- wordpress_data:/var/www/htmlports:- "8000:80"restart: alwaysenvironment:WORDPRESS_DB_HOST: dbWORDPRESS_DB_USER: wordpressWORDPRESS_DB_PASSWORD: wordpressWORDPRESS_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地址:端口号"的界面是这样的。

[docker]九、compose是什么?有什么用?以及用compose启动web、redis和wordpress相关推荐

  1. Docker技术入门与实战 第二版-学习笔记-9-Docker Compose 项目-2-Compose 命令说明

    Compose 命令说明 1)命令对象与格式 对于 Compose 来说,大部分命令的对象既可以是项目本身,也可以指定为项目中 的服务或者容器.如果没有特别的说明,命令对象将是项目,这意味着项目中所有 ...

  2. 《docker基础篇:8.Docker常规安装简介》包括:docker常规安装总体步骤、安装tomcat、安装mysql、安装redis

    文章目录 8.Docker常规安装简介 8.1 docker常规安装总体步骤 8.2安装tomcat 8.3 安装mysql 8.3.1 docker hub上面查找mysql镜像 8.3.2 从do ...

  3. Docker技术入门与实战 第二版-学习笔记-9-Docker Compose 项目-1-举例说明

    Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速在集群中部署分布式应用 Compose 通过一个配置文件来管理多个Docker容器,在配置文件中 ...

  4. Docker(九):Docker实战 安装 Ubuntu

    Docker 安装 Ubuntu Ubuntu 是基于 Debian 的 Linux 操作系统. 1.查看可用的 Ubuntu 版本 访问 Ubuntu 镜像库地址: https://hub.dock ...

  5. git 为什么不能断点_跟我一起学docker(九)--持续系统集成了解下git

    什么是持续集成? 持续集成(Continuous integration,简称CI). 根据敏捷大师Martin Fowler的定义,"持续集成是一种软件开发实践.在持续集成中,团队成员频繁 ...

  6. [转]Docker部署Django由浅入深系列(下): 八步部署Django+Uwsgi+Nginx+MySQL+Redis

    在上篇教程中,我们手动构建了两个容器,一个容器放Django + Uwsgi,另一个容器放Nginx,成功部署了一个简单的Django项目.然而在实际的生产环境中,我们往往需要定义数量庞大的 dock ...

  7. ubuntu下docker的lnmp(二) 安装php-fpm之 下载镜像启动容器

    这里要说一下为什么非要自己去弄一个Ubuntu重新编译安装 php-fpm 我也知道docker官方源有 php-fpm 使用命令可以查很多 root@ccyy-virtual-machine:~# ...

  8. 为什么说 Compose 的声明式代码最简洁 ?Compose/React/Flutter/SwiftUI 语法对比

    前言 Comopse 与 React.Flutter.SwiftUI 同属声明式 UI 框架,有着相同的设计理念和相似的实现原理,但是 Compose 的 API 设计要更加简洁.本文就这几个框架在代 ...

  9. 最新金九银十JAVA面试总结:Java+并发+Spring+MySQL+分布式+Redis+算法+JVM等

    ** 金九银十的面试旺季即将到来,大家准备的怎么样了?** 今年的处境大家都挺难的,但是也不要因此而堕落,太闲了,就多刷题.坚持学习总归是对自己有好处的. 在此,整理了下金九银十的面试知识点(附答案) ...

  10. redis 哨兵_docker里创建redis哨兵

    参考:https://www.jianshu.com/p/ba4667e242f3 https://www.jocat.cn/archives/51908 https://www.runoob.com ...

最新文章

  1. 最后两天-微生物组-宏基因组分析(线上/线下同时开课,2020最后一期)
  2. QIIME 2用户文档. 20命令行界面q2cli(2019.7)
  3. 车道检测源码分析系列
  4. 用python计算pi的值_如何使用python中的series计算pi的值?
  5. Vue 中的 v-if 和 v-show 修饰符
  6. ES6入门之let、cont
  7. (对比PDF)Adobe Acrobat DC 离线对比PDF、draftable.com/compare 在线对比PDF
  8. 美国公司报告称2020年中国会非常穷 我学者驳斥[转]
  9. ghost mysql_Ghost - 博客搭建
  10. 2017美国数学建模MCM C题(大数据)翻译 “合作和导航”
  11. Firemonkey
  12. 【小林课堂】【光学】透镜成像应用
  13. 大话西游2人数最多服务器,大话2大话各个服务器狂人榜排名 看大话如今服务器...
  14. 公司企业邮箱开通使用,收不到邮件怎么办?
  15. 用计算机怎么管理小米路由器,小米路由器3G怎么设置?(电脑)
  16. Sass系统技术选型笔记(2)JBPM
  17. Android 获取手机中的图片信息的两种方法
  18. zabbix配置步骤、操作及使用个人邮箱、企业微信、钉钉报警的配置
  19. 国外问卷调查到底能不能赚钱?
  20. Tensorflow书推荐,深度学习书推荐

热门文章

  1. OPENWRT-LUCI开发总结-LUCI添加新页面总结
  2. 计算机音乐桃源恋歌,GARNiDELiA《桃源恋歌》[FLAC/MP3-320K]
  3. 真实的拼多多:我们统计了56款爆品 发现一个真相……
  4. Android--DES加密解密
  5. 荣耀20青春版装鸿蒙系统,三种颜色三种工艺 荣耀20青春版解锁你的美
  6. 2021-08-05 得帆技术培训Linux作业
  7. logstash: Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread “
  8. gstarcad 2021|gstarcad pro2021(浩辰CAD) 64位附安装教程
  9. Resend messages one by one
  10. 嘉兴 机器人仓库 菜鸟_菜鸟在嘉兴推出全新智能仓 在“双11”启用超级机器人仓群...