您现在的位置是:首页 >其他 >Docker-Compose介绍网站首页其他
Docker-Compose介绍
文章目录
一、Docker-compose 简介
Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。
Docker-Compose将所管理的容器分为三层,分别是 工程(project),服务(service)以及容器(container)。
Docker-Compose运行目录下的所有文件(docker-compose.yml,extends文件或环境变量文件等)组成一个工程,若无特殊指定工程名即为当前目录名。
一个工程当中可包含多个服务,每个服务中定义了容器运行的镜像、参数、依赖。一个服务当中可包括多个容器实例,Docker-Compose并没有解决负载均衡的问题,因此需要借助其它工具实现服务发现及负载均衡,比如 Consul。
Docker-Compose的工程配置文件默认为docker-compose.yml,可通过环境变量COMPOSE_FILE或-f参数自定义配置文件,其定义了多个有依赖关系的服务及每个服务运行的容器。
使用一个Dockerfile模板文件,可以让用户很方便的定义一个单独的应用容器。
在工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个Web项目,除了Web服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。
Compose允许用户通过一个单独的docker-compose.yml模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。
Docker-Compose项目由Python编写,调用Docker服务提供的API来对容器进行管理。因此,只要所操作的平台支持Docker API, 就可以在其上利用Compose来进行编排管理
二、YAML 文件格式及编写注意事项
YAML 是一种标记语言,它可以很直观的展示数据序列化格式,可读性高。类似于 XML数据描述语言,语法比 XML 简单的很多。
YAML 数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号 [] 括起来, hash 用花括号 {} 括起来
三、Docker Compose配置常用字段
字段 描述
build 指定 Dockerfile 文件名,要指定Dockerfile文件需要在build标签的子级标签中使用dockerfile标签指定
dockerfile 构建镜像上下文路径
context 可以是 dockerfile 的路径,或者是指向 git 仓库的 url 地址
image 指定镜像
command 执行命令,覆盖默认命令
container name 指定容器名称,由于容器名称是唯一的,如果指定自定义名称,则无法 scale
deploy 指定部署和运行服务相关配置,只能在 Swarm 模式使用
environment 添加环境变量
networks 加入网络
ports 暴露容器端口,与 -p 相同,但端口不能低于 60
volumes 挂载宿主机路径或命令卷
hostname 容器主机名
restart 重启策略,默认 no,always,no-failure,unless-stoped
no,默认策略,在容器退出时不重启容器。
on-failure,在容器非正常退出时(退出状态非0),才会重启容器。
on-failure:3,在容器非正常退出时重启容器,最多重启3次。
always,在容器退出时总是重启容器。
unless-stopped,在容器退出时总是重启容器,但是不考虑在 Docker 守护进程启动时就已经停止了的容器。
四、Docker Compose 常用命令
字段 描述
build 重新构建服务
ps 列出容器
up 创建和启动容器
exec 在容器里面执行命令
scale 指定一个服务容器启动数量
top 显示容器进程
logs 查看容器输出
down 删除容器、网络、数据卷和镜像
stop/start/restart 停止/启动/重启服务
五、Docker Compose 文件结构
tree /opt/compose_nginx
/opt/compose_nginx/
├── docker-compose.yml #创建模板脚本
├── nginx
│?? ├── Dockerfile #创建容器脚本
│?? ├── nginx-1.12.0.tar.gz #复制源码包
│?? └── run.sh #启动服务脚本
└── wwwroot
└── index.html #站点网页
六、compose 部署
1、Docker Compose 环境安装
Docker Compose 是 Docker 的独立产品,因此需要安装 Docker 之后在单独安装 Docker Compose
#下载 在Linux上我们可以从GitHub上下载它的二进制包来使用,此命令是下载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
[root@localhost ~]# cd /opt/
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
containerd docker-compose mysql nginx nginx.tar php rh
[root@localhost opt]# ll
总用量 143208
drwx--x--x 4 root root 28 10月 9 22:32 containerd
-rw-r--r-- 1 root root 10867152 10月 15 11:28 docker-compose
drwxr-xr-x 2 root root 71 10月 14 16:46 mysql
drwxr-xr-x 4 root root 129 10月 14 18:20 nginx
-rw-r--r-- 1 root root 135774208 10月 10 21:05 nginx.tar
drwxr-xr-x 2 root root 101 10月 14 18:33 php
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@localhost opt]# chmod +x docker-compose
[root@localhost opt]# ll
总用量 143208
drwx--x--x 4 root root 28 10月 9 22:32 containerd
-rwxr-xr-x 1 root root 10867152 10月 15 11:28 docker-compose
drwxr-xr-x 2 root root 71 10月 14 16:46 mysql
drwxr-xr-x 4 root root 129 10月 14 18:20 nginx
-rw-r--r-- 1 root root 135774208 10月 10 21:05 nginx.tar
drwxr-xr-x 2 root root 101 10月 14 18:33 php
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@localhost opt]# mv docker-compose /usr/local/bin/
[root@localhost opt]# docker-compose --version
docker-compose version 1.21.1, build 5a3f1a3
2、compose部署nginx
(1)准备依赖文件
mkdir -p /opt/compose_nginx/nginx /opt/compose_nginx/wwwroot
cd /opt/compose_nginx/nginx
cp nginx-1.12.0.tar.gz ./
vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
vim Dockerfile
#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is nginx image <wl>
#添加环境包
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#上传nginx软件压缩包,并解压
ADD nginx-1.12.0.tar.gz /usr/local/src/
#指定工作目录
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
#指定http和https端口
EXPOSE 80
EXPOSE 443
//方法一:
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf #关闭 nginx 在后台运行
#添加宿主机中run.sh到容器中
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
//方法二:
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
echo "<h1>this is test web</h1>" > /opt/compose_nginx/wwwroot/index.html
(2)编写配置文件docker-compose.yml
vim /opt/compose_nginx/docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80
- 1217:443
networks:
- lnmp
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
lnmp:
cd /opt/compose_nginx/
docker-compose -f docker-compose.yml up -d
----------------------------------------------------------------------------------------------------------
-f, --file FILE :使用特定的 compose 模板文件,默认为 docker-compose.yml
-p, --project-name NAME :指定项目名称,默认使用目录名称
-d :在后台运行
----------------------------------------------------------------------------------------------------------
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b48dceee248f compose_nginx_nginx "/run.sh" About a minute ago Up About a minute 0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp compose_nginx_nginx_1
cd /opt/compose_nginx/
docker-compose ps #必须在docker-compose.yml所在目录执行此命令
浏览器访问:http://192.168.229.80:1314
3、compose部署lnmp
[root@localhost opt]# tree /opt/compose/ -L 2
/opt/compose/
├── docker-compose.yml
├── mysql
│ ├── Dockerfile
│ ├── my.cnf
│ └── mysql-boost-5.7.20.tar.gz
├── nginx
│ ├── Dockerfile
│ ├── html
│ ├── nginx-1.12.0.tar.gz
│ ├── nginx.conf
│ ├── wordpress-4.9.4-zh_CN.tar.gz
│ └── wwwroot
└── php
├── Dockerfile
├── php-7.1.10.tar.bz2
├── php-fpm.conf
├── php.ini
└── www.conf
5 directories, 13 files
配置nginx
[root@localhost nginx]# ls
Dockerfile html nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz wwwroot
[root@localhost nginx]# cat Dockerfile
FROM centos:7
MAINTAINER this is nginx image <ly>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
#ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
CMD [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
[root@localhost nginx]# cd html/
[root@localhost html]# ls
wordpress
配置MySQL
[root@localhost compose]# cd mysql/
[root@localhost mysql]# ls
Dockerfile my.cnf mysql-boost-5.7.20.tar.gz
[root@localhost mysql]# cat my.cnf
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysql]
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
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
[root@localhost mysql]# cat Dockerfile
FROM centos:7
MAINTAINER this is mysql image <wl>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
RUN useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN 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_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
-DMYSQL_DATADIR=/usr/local/mysql/data
-DWITH_BOOST=boost
-DWITH_SYSTEMD=1 && make -j4 && make install
RUN chown -R mysql:mysql /usr/local/mysql/
ADD my.cnf /etc/my.cnf
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
RUN bin/mysqld
--initialize-insecure
--user=mysql
--basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
#RUN systemctl enable mysqld
#VOLUME [ "/usr/local/mysql" ]
CMD /usr/local/mysql/bin/mysqld
配置PHP
[root@localhost compose]# cd php/
[root@localhost php]# ls
Dockerfile php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
[root@localhost php]# cat Dockerfile
FROM centos:7
MAINTAINER this is php image <wl>
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
RUN useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./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 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
配置Docker-compose.yml文件
[root@localhost compose]# ls
docker-compose.yml mysql nginx php
[root@localhost compose]# vim docker-compose.yml
[root@localhost compose]# cat docker-compose.yml
version: '2' #使用版本2,版本3不包含volumes-from
services:
nginx:
build:
context: ./nginx
dockerfile: Dockerfile
container_name: nginx
ports:
- 1314:80
- 1315:443
networks:
lnmp:
ipv4_address: 172.18.0.10
volumes:
- ./nginx/html/:/usr/local/nginx/html
mysql:
build:
context: ./mysql
dockerfile: Dockerfile
container_name: mysql
ports:
- 3306:3306
networks:
lnmp:
ipv4_address: 172.18.0.20
volumes:
- /usr/local/mysql
php:
build:
context: ./php
dockerfile: Dockerfile
container_name: php
ports:
- 9000:9000
networks:
lnmp:
ipv4_address: 172.18.0.30
volumes_from:
- nginx
- mysql
depends_on:
- nginx
- mysql
networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16
运行docker compose
[root@localhost compose]# ls
docker-compose.yml mysql nginx php
[root@localhost compose]# docker-compose -f docker-compose.yml up -d
运行结果
[root@localhost compose]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------------------------
mysql /bin/sh -c /usr/sbin/init Up 0.0.0.0:3306->3306/tcp,:::3306->3306/tcp
nginx /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:1315->443/tcp,:::1315->443/tcp, 0.0.0.0:1314->80/tcp,:::1314->80/tcp
php /usr/local/php/sbin/php-fpm -F Up 0.0.0.0:9000->9000/tcp,:::9000->9000/tcp
[root@localhost compose]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b7c237ec1ef compose_php "/usr/local/php/sbin…" About an hour ago Up About an hour 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
016b61ad52ad compose_mysql "/bin/sh -c /usr/sbi…" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql
bd2c18c1dff8 compose_nginx "/usr/local/nginx/sb…" About an hour ago Up About an hour 0.0.0.0:1314->80/tcp, :::1314->80/tcp, 0.0.0.0:1315->443/tcp, :::1315->443/tcp nginx
[root@localhost compose]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_php latest 44cd7158ebe4 About an hour ago 1.2GB
compose_mysql latest 3b9913b419cd 2 hours ago 9.98GB
compose_nginx latest 83d1c00059d4 2 hours ago 466MB
centos 7 eeb6ee3f44bd 4 weeks ago 204MB
进入MySQL容器并授予权限
[root@localhost compose]# docker exec -it mysql bash
[root@1011b16fbb59 mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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 privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
浏览器访问测试
http://192.168.229.90:1314/wordpress/index.php