docker-compose 是一个可以实现在单机上对容器集群编排管理的工具。使用python开发的,能运行docker的平台也都能用 docker-compose 编排管理容器。
本质就是在yaml格式的docker-compose配置模版文件里定义多个服务容器的启动参数和依赖关系,并使用docker-compose命令指定配置模版文件的配置来启动和管理容器集群。
Dockerfile 文件 → 手动执行 docker build 一个个构建镜像 → 手动执行 docker run 创建启动一个个容器
Dockerfile 文件 → 在docker compose 配置文件里定义好容器启动参数和依赖关系 → 执行 docker-compose命令指定配置文件一次性的自动完成所有镜像的构建和容器的创建启动
Docker-compose 项目是 Docker 官方的开源项目,负责实际对 Docker 容器集群的快速编排。Docker-compose 项目由 Python 编写,调用 Dokcer 服务提供的 API 来对容器进行管理。因此,只要操作的平台支持 Docker-AIP,就可以在其上利用 Compose 来进行编排管理
Docker-compose是一个用于定义和运行多容器 Docker 应用程序的工具。它允许用户使用 YAML,文件来定义应用程序的配置,包括容器的数量、容器之间的依赖关系、环境变量、端口映射以及其他设置。然后,用户可以使用Docker-compose 命令来启动和管理这些容器。
使用 Docker-compose 可以方便管理多个容器,例如,可以同时启动一个 Web放服务器容器和一个数据库容器,并在他们之间建立网络连接。Docker-compose 还支持自定义网络,使得容器可以在不同的网络中进行通信。
Docker-compose 文件通常包含一个或多个服务器,每个服务器都由一个或多个容器组成。服务定义了容器应该执行的任务,以及容器之间的依赖关系。例如,一个 Web 服务器服务可以包含一个或多个 Web 服务器容器,而这些容器又依赖于一个数据库容器。
Docker-compose 将所管理的容器分为三层,分别是项目(project),服务(service)以及容器(container)。Docker-compose 运行目录下的所有文件组成一个项目,若无特殊指定项目名则即为当前目录名。一个项目当中可以包含多个服务,每个服务都包含一个名称、镜像、端口映射、环境变量、挂载点等信息。
Docker-compose 的项目配置文件默认为 docker-compose.yml,可以通过环境变量 COMPOSE_FILE 或 -f 参数自定义配置文件,compose 允许用户通过一个单独的 docker-compose.yml 模版文件(YAML格式)来定义一组相关联的应用容器为一个项目(project)。
项目project → 包含一个或多个服务service → 包含一个或多个容器container的镜像、端口映射、环境变量、依赖关系等启动参数
默认使用项目的目录作为project的项目名,支持使用 -p 或 --project-name 来制定项目名称
项目目录里要包含一个 docker-compose 配置模版文件,默认为 docker-compose.yml,支持使用 -f 或 --file 来指定项目的配置模版文件
在配置模版文件里要包含一个或者多个服务器的配置,每个服务要包含容器的名称、镜像、映射端口、挂载点、网络模式、依赖关系等容器启动的配置参数
Docker Compose 是 Docker 的独立产品,因此需要安装 Docker 之后在单独安装 Docker Compose #下载 curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose #安装 chmod +x /usr/local/bin/docker-compose #查看版本 docker-compose --version
YAML 是一种标记语言,它可以很直观的展示数据序列化格式,可读性高。类似于 json 数据描述语言,语法比 json 简单的很多。YAML 数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号 [] 括起来, hash 用花括号 {} 括起来。
debug:true
debug:false
name:tom
name:'tom'
name:"tom"
animal: - 飞行动物 - 爬行动物 - 水生动物 - 两栖动物
animal: ["飞行动物","爬行动物","水生动物","两栖动物"]
一级字段: 二级字段1: #yaml格式用 空格 表示字段的层级关系,同一层字段要靠左对齐 二级字段2: 三级字段:
{ "human": { #json格式用大括号 {} 表示字段的层级关系,每层字段除了最后一个字段都 要用逗号,结尾。 "name": { "姓": ["王"] "名": ["文宇","思聪"] }, "age": ["24"] "sex": ["男"] "height": ["180"] } }
value: -> value: "helloword" hello world value: | -> value: "hello\world" #key: | 表示保留文本块里的换行符 hello world value: |- -> value: 相当于 echo -n "helloworld" hello world value: |+ -> value: "hello\world\n" hello world name: &a yaml 相当于 a="yaml" #锚点 book: *a 相当于 echo $a book: yaml #引用
字段 | 描述 |
---|---|
build | 指定 Dockerfile 文件名,要指定Dockerfile文件需要在build标签的子级标签中使用dockerfile标签指定 |
dockerfile | 构建镜像上下文路径 |
context | 可以是 dockerfile 的路径,或者是指向 git 仓库的 url 地址 |
image | 指定镜像 |
command | 执行命令,覆盖容器启动后默认执行的命令 |
container_name | 指定容器名称,由于容器名称是唯一的,如果指定自定义名称,则无法scale指定容器数量 |
deploy | 指定部署和运行服务相关配置,只能在 Swarm 模式使用 |
environment | 添加环境变量 |
networks | 加入网络,引用顶级networks下条目 |
network_mode | 设置容器的网络模式,如 host,bridge,... |
ports | 暴露容器端口,与 -p 相同,但端口不能低于 60 |
volumes | 挂载一个宿主机目录或命令卷到容器,命名卷要在顶级 volumes 定义卷名称 |
volumes_from | 从另一个服务或容器挂载卷,可选参数 :ro 和 :rw,仅版本 '2' 支持,版本3不支持 |
hostname | 容器主机名 |
sysctls | 在容器内设置内核参数 |
links | 连接到另外一个容器,- 服务名称[:服务别名] |
privileged | 用来给容器root权限,注意是不安全的,true | false |
restart | 设置重启策略,no,always,no-failure,unless-stopped no,默认策略,在容器退出时不重启容器。 on-failure,在容器非正常退出时(退出状态非0),才会重启容器。 on-failure:3,在容器非正常退出时重启容器,最多重启3次。 always,在容器退出时总是重启容器。 unless-stopped,在容器退出时总是重启容器,但是不考虑在 Docker 守护进程启动时就已经停止了的容器。 |
depends_on | 在使用Compose时,最大的好处就是少打启动命令,但一般项目容器启动的顺序是有要求的,如果直接从上到下启动容器,可能会因为容器依赖问题而启动失败。例如在没启动数据库容器的时候启动应用容器,应用容器会因为找不到数据库而退出。depends_on标签用于解决容器的依赖、启动先后的问题。 php: depends_on: - apache - mysql |
字段 | 描述 |
---|---|
build | 重新构建服务 |
ps | 列出容器 |
up | 创建和启动容器 |
exec | 在容器里面执行命令 |
scale | 指定一个服务容器启动数量 |
top | 显示容器进程 |
logs | 查看容器输出 |
down | 删除容器、网络、数据卷和镜像 |
stop/start/restart | 停止/启动/重启服务 |
mkdir -p /opt/docker-compose/nginx /opt/docker-compose/html cd /opt/compose_nginx/nginx
ROM centos:7 MAINTAINER this is nginx of LNMP ADD nginx-1.24.0.tar.gz /opt/ RUN yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make && \ yum clean all && \ useradd -M -s /sbin/nologin nginx && \ cd /opt/nginx-1.24.0/ && \ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && \ make && make install ENV PATH $PATH:/usr/local/nginx/sbin COPY nginx.conf /usr/local/nginx/conf/nginx.conf EXPOSE 80 #EXPOSE 443 CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
docker build -t lnmp:nginx . [+] Building 3.4s (9/9) FINISHED docker:default => [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 661B 0.0s => [internal] load metadata for docker.io/library/centos:7 2.9s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/4] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515 0.0s => [internal] load build context 0.0s => => transferring context: 2.87kB 0.0s => CACHED [2/4] ADD nginx-1.24.0.tar.gz /opt/ 0.0s => CACHED [3/4] RUN yum -y install pcre-devel zlib-devel openssl-devel gcc gc 0.0s => CACHED [4/4] COPY nginx.conf /usr/local/nginx/conf/nginx.conf 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:03adabbbe917650347f1223b0aaa1c21ff1b1ecc9a3be1ee94 0.0s => => naming to docker.io/library/lnmp:nginx 0.0s docker images REPOSITORY TAG IMAGE ID CREATED SIZE lnmp nginx 03adabbbe917 5 hours ago 358MB
vim /opt/docker-compose/nginx/nginx.conf worker_processes auto; events { use epoll; worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.ky33.com; charset utf-8; location / { root html; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root html; fastcgi_pass 172.18.0.30:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } } }
vim html/wwy.html wwy like dancing with xiaomeimei
mkdir -p /opt/docker-compose/mysql cd /opt/docker-compose/mysql
vim Dockerfile FROM centos:7 MAINTAINER this is mysql image for LNMP ADD mysql-boost-5.7.44.tar.gz /opt COPY my.cnf /etc/my.cnf yum clean all && \ cd /opt/mysql-5.7.44/ && \ cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ -DSYSCONFDIR=/etc \ -DSYSTEMD_PID_DIR=/usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_EXTRA_CHARSETS=all \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1 && \ make -j 2 && make install && \ useradd -M -s /sbin/nologin mysql && \ chown -R mysql:mysql /usr/local/mysql/ && \ chown mysql:mysql /etc/my.cnf && \ /usr/local/mysql/bin/mysqld \ --initialize-insecure \ --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data ENV PATH $PATH:/usr/local/mysql/bin:/usr/local/mysql/lib EXPOSE 3306 CMD ["/usr/local/mysql/bin/mysqld"]
docker build -t lnmp:mysql . [+] Building 3.7s (9/9) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 1.13kB 0.0s => [internal] load metadata for docker.io/library/centos:7 3.4s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/4] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c4 0.0s => [internal] load build context 0.0s => => transferring context: 742B 0.0s => CACHED [2/4] ADD mysql-boost-5.7.44.tar.gz /opt 0.0s => CACHED [3/4] COPY my.cnf /etc/my.cnf 0.0s => CACHED [4/4] RUN yum -y install ncurses ncurses-devel bison cmake openssl-devel gcc gcc-c 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:77caee91403ca8609ab4d6f26096f62e46b6e7e4f51d33b3635057acd7f0f9f0 0.0s => => naming to docker.io/library/lnmp:mysql 0.0s docker images REPOSITORY TAG IMAGE ID CREATED SIZE php ky33 50a90ffed0ed 4 hours ago 965MB lnmp mysql 77caee91403c 5 hours ago 7.96GB docker-compose-nginx latest b1664f7fcaa8 5 hours ago 358MB lnmp nginx 03adabbbe917 5 hours ago 358MB
vim my.cnf [client] port = 3306 socket=/usr/local/mysql/mysql.sock [mysqld] user = mysql basedir=/usr/local/mysql datadir=/usr/local/mysql/data port = 3306 character-set-server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket=/usr/local/mysql/mysql.sock bind-address = 0.0.0.0 skip-name-resolve max_connections=2048 default-storage-engine=INNODB max_allowed_packet=16M server-id = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
mkdir -p /opt/docker-compose/php cd /opt/docker-compose/php
vim Dockerfile FROM centos:7 MAINTAINER this is php image for LNMP (wl 20240126) ADD php-7.1.10.tar.bz2 /opt/ RUN yum -y install gd \ libjpeg libjpeg-devel \ libpng libpng-devel \ freetype freetype-devel \ libxml2 libxml2-devel \ zlib zlib-devel \ curl curl-devel \ openssl openssl-devel \ gcc gcc-c++ make pcre-devel && \ yum clean all && \ cd /opt/php-7.1.10/ && \ ./configure \ --prefix=/usr/local/php \ --with-mysql-sock=/usr/local/mysql/mysql.sock \ --with-mysqli \ --with-zlib \ --with-curl \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-openssl \ --enable-fpm \ --enable-mbstring \ --enable-xml \ --enable-session \ --enable-ftp \ --enable-pdo \ --enable-tokenizer \ --enable-zip && make -j 2 && make install && \ useradd -M -s /sbin/nologin nginx COPY php.ini /usr/local/php/lib/php.ini COPY php-fpm.conf /usr/local/php/etc/php-fpm.conf COPY www.conf /usr/local/php/etc/php-fpm.d/www.conf ENV PATH $PATH:/usr/local/php/bin:/usr/local/php/sbin EXPOSE 9000 CMD ["/usr/local/php/sbin/php-fpm", "-F"]
docker build -t php:ky33 . [+] Building 3.7s (11/11) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 1.15kB 0.0s => [internal] load metadata for docker.io/library/centos:7 3.5s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/6] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c4 0.0s => [internal] load build context 0.0s => => transferring context: 75.98kB 0.0s => CACHED [2/6] ADD php-7.1.10.tar.bz2 /opt/ 0.0s => CACHED [3/6] RUN yum -y install gd libjpeg libjpeg-devel libpng libpng-devel freetype fre 0.0s => CACHED [4/6] COPY php.ini /usr/local/php/lib/php.ini 0.0s => CACHED [5/6] COPY php-fpm.conf /usr/local/php/etc/php-fpm.conf 0.0s => CACHED [6/6] COPY www.conf /usr/local/php/etc/php-fpm.d/www.conf 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:50a90ffed0ede517116f52a2dab7bcbdc8ff5f440b5eab477b922bbef9881493 0.0s => => naming to docker.io/library/php:ky33 0.0s docker images REPOSITORY TAG IMAGE ID CREATED SIZE php ky33 50a90ffed0ed 4 hours ago 965MB lnmp mysql 77caee91403c 5 hours ago 7.96GB lnmp nginx 03adabbbe917 5 hours ago 358MB docker-compose-nginx latest b1664f7fcaa8 5 hours ago 358MB
cd /opt/docker-compose vim docker-compose.yml
vim docker-compose.yml #使用dockerfile现场构建镜像 build: #指定dockerfile文件所在目录 context: ./nginx #指定dockerfile文件名 dockerfile: Dockerfile #指定容器名 container_name: nginx #映射端口 ports: - 80:80 - 443:443 #数据卷挂载 volumes: - ./nginx/html:/usr/local/nginx/html #加入网络 networks: lnmp: ipv4_address: 172.18.0.10 mysql: image: lnmp:mysql container_name: mysql privileged: true volumes: - db-data:/usr/local/mysql networks: lnmp: ipv4_address: 172.18.0.20 php: image: php:ky33 container_name: php ports: - 9000:9000 volumes: - db-data:/usr/local/mysql - ./nginx/html:/usr/local/nginx/html networks: lnmp: ipv4_address: 172.18.0.30 depends_on: - nginx - mysql networks: lnmp: driver: bridge ipam: config: - subnet: 172.18.0.0/16 volumes: db-data:
docker-compose -f docker-compose.yml up -d [+] Running 3/4 ⠧ Network docker-compose_lnmp Created 2.6s ✔ Container nginx Started 1.6s ✔ Container mysql Started 1.4s ✔ Container php Started 2.4s docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9e5e0a0b5fe7 php:ky33 "/usr/local/php/sbin…" 10 seconds ago Up 7 seconds 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php 8ade50c7246a docker-compose-nginx "/usr/local/nginx/sb…" 10 seconds ago Up 8 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp nginx 432d3dae4da2 lnmp:mysql "/usr/local/mysql/bi…" 10 seconds ago Up 8 seconds 3306/tcp mysql
docker exec -it mysql bash mysqladmin -u root -p password "abc123" Enter password: mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. mysql -uroot -pabc123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 5.7.44 Source distribution Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database wordpress; Query OK, 1 row affected (0.00 sec) mysql>grant all on wordpress.* to 'wordpress'@'%' identified by 'wordpress@123'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye