Docker Compose 使用 YML 文件来配置应用程序所需要的的所有服务,非常适合组合使用多个容器进行开发的场景,且不再需要使用 shell 脚本来启动容器
常用字段 | 描述 |
---|---|
build | 指定Dockerfile文件名,要指定Dockerfile文件需要在build标签的子级标签中使用dockerfile标签指定 |
dockerfile | 构建镜像上下文路径 |
context | 可以是dockerfile的路径,或者是指向git仓库的url地址 |
image | 指定镜像 |
command | 执行命令,覆盖容器启动后默认执行的命令 |
container name | 指定容器名称,由于容器名称是唯一的,如果指定自定义名称,则无法scale |
deploy | 指定部署和运行服务相关配置,只能在Swarm模式使用 |
environment | 添加环境变量 |
networks | 加入网络,引用顶级networks下条目 |
ports | 暴露容器端口,与-p相同,但端口不能低于60 |
volumes | 挂载一个宿主机目录或命令卷到容器,命名卷要在顶级volume定义卷名称 |
volumes_from | 从另一个服务或容器挂载卷,可选参数:ro和:rw |
hostname | 容器主机名 |
sysctls | 在容器内设置内核参数 |
links | 连接到另外一个容器,- 服务名称[:服务别名] |
restart | 重启策略,默认为no,另有always/no-failure/unless-stoped no,默认策略,在容器退出时不重启容器。 no-failure,在容器非正常退出时(退出状态非0),才会重启容器。 on-failure:3,在容器非正常退出时重启容器,最多重启3次。 always,在容器退出时总是重启容器。 unless-stopped,在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器 |
depends_on | 在使用Compose时,最大的好处就是少打启动命令,但一般项目容器启动的顺序是由要求的,如果直接从上到下启动容器,可能会因为容器依赖问题而启动失败。例如在没启动数据库容器的时候启动应用容器,应用容器会因为找不到数据库而退出。depends_on标签用于解决容器的依赖、启动先后的问题 |
字段 | 描述 |
---|---|
build | 重新构建服务 |
ps | 列出容器 |
up | 创建和启动容器 |
exec | 在容器里面执行命令 |
scale | 指定一个服务容器启动数量 |
top | 显示容器进程 |
logs | 查看容器输出 |
down | 删除容器、网络、数据卷和镜像 |
stop/start/restart | 停止/启动/重启服务 |
'命令格式:'
docker-compose [选项] [命令] [参数]
'常用选项:'
-verbose:输出更多调试信息
-version:打印版本并退出
-f:使用特定的compose模板文件,默认为docker-compose.yml
-p:指定项目名称,默认使用目录名称
[root@docker ~]# yum install -y tree
[root@docker ~]# tree /opt/compose_lnmp/
/opt/compose_lnmp/
├── docker-compose.yml
├── mysql
│ ├── Dockerfile
│ ├── my.cnf
│ └── mysql-boost-5.7.20.tar.gz
├── nginx
│ ├── Dockerfile
│ ├── nginx-1.12.0.tar.gz
│ ├── nginx.conf
│ └── wordpress-4.9.4-zh_CN.tar.gz
└── php
├── Dockerfile
├── php-7.1.10.tar.bz2
├── php-fpm.conf
├── php.ini
└── www.conf
Docker-Compose 是 Docker 的独立产品,因此需要安装 Docker 之后再单独安装 Docker-Compose
[root@localhost ~]# cd /usr/local/bin/
[root@localhost bin]# ls
[root@localhost bin]# curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 664 100 664 0 0 323 0 0:00:02 0:00:02 --:--:-- 323
100 10.3M 100 10.3M 0 0 1414k 0 0:00:07 0:00:07 --:--:-- 2607k
[root@localhost bin]# ls
docker-compose
[root@localhost bin]# chmod +x /usr/local/bin/docker-compose
[root@localhost bin]# ls
docker-compose
[root@localhost bin]# docker-compose -v
docker-compose version 1.21.2, build a133471
[root@localhost ~]# mkdir -p /opt/compose_nginx/nginx
[root@localhost ~]# cd /opt/compose_nginx/nginx/
[root@localhost nginx]# rz
[root@localhost nginx]# ls
nginx-1.12.2.tar.gz
[root@localhost nginx]# vim Dockerfile
[root@localhost nginx]# cat Dockerfile
FROM centos:7
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.12.2
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
RUN make -j 4 && make install
RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#ENV PATH /usr/local/nginx/sbin/:PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
CMD ["/usr/local/nginx/sbin/nginx"]
[root@localhost ~]# mkdir -p /opt/compose_nginx/nginx
[root@localhost nginx]# cd ..
[root@localhost compose_nginx]# vim docker-compose.yml
[root@localhost compose_nginx]# cat docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80
- 1216:443
networks:
- xjj
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
xjj:
version: '3' #compose编排版本为3
services: #服务:
nginx: #服务名称
hostname: nginx #容器主机名
build: #创建容器过程:
context: ./nginx #所需素材都在该目录(仓库)下
dockerfile: Dockerfile #指定Dockerfile文件
ports: #提供端口:
- 1216:80 #对外提供(暴露)的端口
- 1217:443
networks: #指定网络
- xjj #网络名称(群集名称需一致)
volumes: #数据卷
- ./wwwroot:/usr/local/nginx/html #将宿主机该仓库内文件与nginx首页文件相关联(容器)
networks: #对外公开需要声明networks群集名称
xjj:
[root@localhost nginx]# docker-compose -f docker-compose.yml up -d
...
...
Successfully built 4e35d880d9ca
Successfully tagged compose_nginx_nginx:latest
————————————————
[root@localhost compose_nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_nginx_nginx latest 4e35d880d9ca 49 seconds ago 729MB
centos 7 eeb6ee3f44bd 2 months ago 204MB
[root@localhost compose_nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
063f07eec655 compose_nginx_nginx "/usr/local/nginx/sb…" 56 seconds ago Created compose_nginx_nginx_1
[root@localhost compose_nginx]# ls
docker-compose.yml nginx wwwroot
[root@localhost compose_nginx]# echo "<h1>hello world</h1>" > index.html
[root@localhost compose_nginx]# curl 192.168.126.12:1216
<h1>hello world</h1>
[root@localhost compose_nginx]# yum -y install tree
[root@localhost compose_nginx]# tree ./
./
├── docker-compose.yml
├── nginx
│ ├── Dockerfile
│ └── nginx-1.12.2.tar.gz
└── wwwroot
└── index.html
2 directories, 4 files
Dockerfile 文件详情见往期博客
[root@localhost ~]#mkdir -p /opt/compose_lnmp/nginx/
[root@localhost ~]#mkdir -p /opt/compose_lnmp/mysql/
[root@localhost ~]#mkdir -p /opt/compose_lnmp/php/
[root@localhost ~]#ls /opt/compose_lnmp/nginx/
Dockerfile nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz
[root@localhost ~]#ls /opt/compose_lnmp/mysql/
Dockerfile my.cnf mysql-boost-5.7.20.tar.gz
[root@localhost ~]#ls /opt/compose_lnmp/php/
Dockerfile php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
[root@localhost ~]#cat docker-compose.yml
version: '2'
services:
nginx:
hostname: nginx
build:
context: /opt/compose_lnmp/nginx/
dockerfile: Dockerfile
ports:
- 1111:80
- 2222:443
container_name: nginx_server
networks:
lnmp:
ipv4_address: 172.111.0.10
mysql:
hostname: mysql
build:
context: /opt/compose_lnmp/mysql/
dockerfile: Dockerfile
ports:
- 3306:3306
container_name: mysql_server
networks:
lnmp:
ipv4_address: 172.111.0.20
php:
hostname: php
build:
context: /opt/compose_lnmp/php/
dockerfile: Dockerfile
ports:
- 9000:9000
container_name: php_server
networks:
lnmp:
ipv4_address: 172.111.0.30
networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.111.0.0/16
[root@localhost /opt/compose_lnmp]#docker-compose -f docker-compose.yml up -d
......
[root@localhost /opt/compose_lnmp]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_lnmp_php latest 86b0447ebabd About a minute ago 1.22GB
compose_lnmp_mysql latest 59579ca01443 5 minutes ago 9.98GB
compose_lnmp_nginx latest 82d760d5d811 17 minutes ago 528MB
centos 7 eeb6ee3f44bd 2 months ago 204MB
[root@localhost /opt/compose_lnmp]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53c87813597f compose_lnmp_php "/bin/sh -c '/usr/lo…" About a minute ago Up About a minute 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php_server
2d671252967b compose_lnmp_nginx "/usr/local/nginx/sb…" About a minute ago Up About a minute 0.0.0.0:1111->80/tcp, :::1111->80/tcp, 0.0.0.0:2222->443/tcp, :::2222->443/tcp nginx_server
9d5428cfdf54 compose_lnmp_mysql "/usr/sbin/init" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql_server
[root@localhost /opt/compose_lnmp]#docker exec -it mysql_server bash
[root@mysql bin]# mysql -u root -p
Enter password:
#初始密码为空
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
访问 http://192.168.126.12:11111/wordpress/index.php
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_53560205/article/details/121732395
内容来源于网络,如有侵权,请联系作者删除!