Article Directory
Preface
One: Detailed explanation of Docker Compose container orchestration
1.1: What is Docker Compose? what's the effect?
The predecessor of Docker Compose is Fig, which is a tool for defining and running multiple containers
Using Docker Compose no longer requires a shell script to start the container
Through Docker Compose, you can use YML files to configure all the services required by the application
Docker Compose is very suitable for scenarios where multiple containers are combined for development
The YML file just mentioned
YAML is a very intuitive data serialization format with a markup language. It is very suitable for expressing or editing data structures, various configuration files, file outlines, etc. For example: many email header formats are very similar to YAML
File format and writing notes
1. Tab key indentation is not supported. Space indentation is required. Use indentation to indicate hierarchical relationship.
2. Usually the beginning of indentation is 2 spaces. The number of indented spaces is not important, as long as the elements of the same level are aligned to the left may be
3, indented one space character, such as colon, comma, a crossbar
4, using a # Note
5, if the special characters in single quotation marks
6, Boolean values must be enclosed in quotes
1.2: Detailed explanation of using Docker Compose: usage steps, environment preparation, detailed format
Three steps used by compose
1. Use Dockerfile to define the application environment
2. Use docker-compose.yml to define the services that make up the application so that they can run together in an isolated environment
3. Finally execute the docker-compose up command to start and run the entire application
Docker compose环境准备
[root@docker ~]# curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose '//在Linux上我们可以从GitHub上下载它的二进制包来使用,此命令是下载Docker Compose的当前稳定版本'
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
[root@docker ~]# docker-compose -v
docker-compose version 1.21.1, build 5a3f1a3
Example of Docker Compose file structure: docker-compose.yml
1. Compose version number and service identifier must be written in the top box
2. Attribute name and attribute value are separated by: (colon plus space)
3. Use two spaces to indicate the level
4. The service attribute is represented by-(space space-space)
version: '2' '//compose版本号'
services: '//服务标识符'
web: '//子服务名'
image: dockercloud/hello-world '//服务依赖镜像属性'
ports: '//服务端口属性'
- 8080
networks: '//网络服务属性'
- front-tier
- back-tier
redis:
image: redis
links: '//容器间的连接设置'
- web
networks:
- back-tier
lb:
image: dockercloud/haproxy
ports:
- 80:80
links:
- web
networks:
- front-tier
- back-tier
volumes: '//挂载一个目录或者一个已存在的数据卷容器'
- /var/run/docker.sock:/var/run/docker.sock
networks:
front-tier:
driver: bridge
back-tier:
driver: bridge
1.3: Docker Compose configuration common fields
Common fields have some explanations in the above yml file format, the following is a detailed explanation
1.4: Docker Compose commonly used commands
基本的命令格式:docker-compose [选项] [命令] [参数]
docker-compose选项
–verbose:输出更多调试信息
–version:打印版本并退出
-f、–file FILE:使用特定的compose末班文件,默认为docker-compose.yml
-p、–project-name NAME:指定项目名称,默认使用目录名称
Two: Experiment: Use Docker-Compose to create a container
Install docker environment
[root@docker2 compose_nginx]# yum install docker-ce -y
下载软件 docker-compose
[root@docker2 opt]# ls
docker-compose
[root@docker2 opt]# chmod +x docker-compose
[root@docker2 opt]# cp -p docker-compose /usr/local/bin/
[root@docker2 compose_nginx]# yum install tree -y
2. Create nginx
nginx 操作
Copy the previously created nginx doekerfile folder to the compose_nginx directory
[root@docker2 compose_nginx]# cd /opt
[root@docker2 opt]# mkdir compose_nginx
[root@docker2 opt]# cd co
compose_nginx/ containerd/
[root@docker2 opt]# cd co
compose_nginx/ containerd/
[root@docker2 opt]# cd compose_nginx/
[root@docker2 compose_nginx]#
将之前造的nginx DOCKFILE文件夹拷贝到此目录下
[root@docker2 opt]# cp -r nginx compose_nginx/
编辑yml 文件
[root@docker2 compose_nginx]# vim docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80
- 1217:443
networks:
- cluster
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
cluster:
[root@docker2 nginx]# ls
Dockerfile nginx-1.12.2.tar.gz run.sh
[root@docker2 opt]# tree compose_nginx/
compose_nginx/
├── docker-compose.yml
└── nginx
├── Dockerfile
├── nginx-1.12.2.tar.gz
└── run.sh
run
[root@docker2 compose_nginx]# docker-compose -f docker-compose.yml up -d
。。。。。。。。。。。。。。。。。。。。。。
Successfully built 1d98e0d8cbcc
Successfully tagged compose_nginx_nginx:latest
WARNING: Image for service nginx was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating compose_nginx_nginx_1 ... done
[root@docker2 compose_nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d330373d57c compose_nginx_nginx "/run.sh" 5 minutes ago Up 5 minutes 0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp compose_nginx_nginx_1
[root@docker2 opt]# tree compose_nginx/
compose_nginx/
├── docker-compose.yml
├── nginx
│ ├── Dockerfile
│ ├── nginx-1.12.2.tar.gz
│ └── run.sh
└── wwwroot
2.2 Create Nginx + tomcat container
[root@docker2 compose_ngcat]# ls
docker-compose.yml nginx tomcat wwwroot wwwroot2
Vim docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80
- 1217:443
networks:
- cluster
volumes:
- ./wwwroot:/usr/local/nginx/html
tomcat:
hostname: tomcat
build:
context: ./tomcat
dockerfile: Dockerfile
ports:
- 1218:8080
networks:
- cluster2
volumes:
- ./wwwroot2:/usr/local/tomcat8/webapps/ROOT/
networks:
cluster:
cluster2:
run
[root@docker2 compose_ngcat]# docker-compose -f docker-compose.yml up -d
[root@docker2 wwwroot2]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7979f0446ca compose_ngcat_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_ngcat_nginx_1
d49ee8d19d23 compose_ngcat_tomcat "/usr/local/tomcat8/…" About a minute ago Up About a minute 0.0.0.0:1218->8080/tcp compose_ngcat_tomcat_1
[root@docker2 wwwroot2]# ls
[root@docker2 compose_ngcat]# ls
docker-compose.yml nginx tomcat wwwroot wwwroot2
Access test