A, Docker-Compose Profile
Compose is a tool for defining and operating container docker application. By Compose, you can use YAML file with all the services required by the application. Then, use a command, you can create and start all services from a YAML file with the. Then you need to understand the basic syntax of YAML files.
YAML file basic syntax:
- Case Sensitive;
- Use indention hierarchy;
- Indent does not allow tab, allowing only spaces;
- The number of spaces to indent does not matter, as long as the left-aligned elements of the same level can be;
- "#" Indicates a comment;
Docker-Compose tool arrangement is a container. By a .yml or .yaml file, the deployment method for all containers, file mapping, port mapping and other containers written in a configuration file, execute the command docker-compose up like a script execution, one by one, to install and deploy container.
Dockerfile allows users to manage a single application container; Compose the application allows the user to define a set of associated containers in a template (YAML form);
Docker Compose will manage the container is divided into three layers:
- Engineering (project);
- Service (service);
- The container (Container);
Yml all files in the directory docker compose run up a project, a project contains multiple services, each defined container running mirror, parameters, depend. Services may include a plurality of container instance.
docker-compose that docker container layout tool, mainly to address each other have to manage multiple containers dependencies.
Second, the installation docker-compose the use of tools
To use this container docker-comppose layout tool, it must be based on the host environment docker, reference may docker detailed installation guide . After docker environment to solve, is to download docker-compose this command, you can on GitHub official website to download, as shown:
When you download compose tool, you must first see docker version of this machine!
[root@docker ~]# docker -v //查看docker的版本信息
Docker version 18.09.0, build 4d60db4
//本次采用18.9.0版本
If docker version is too low, you can find other versions of themselves docker-compose tool. After selecting the appropriate version, execute commands found on github website.
[root@docker ~]# curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
If the speed is poor you can use the following command:
[root@docker ~]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
//使用道云的加速器进行下载
[root@docker ~]# docker-compose -v //查看工具的版本信息
docker-compose version 1.25.0, build 0a186604
Third, write .yml file
(1) Build a simple file Nginx service .yml
[root@docker ~]# vim /root/.vimrc
set tabstop=2
[root@docker ~]# source /root/.vimrc
//由于tab键使用的较多,所以事先设置了一个tab键代表的空格数
[root@docker ~]# mkdir compose_test
[root@docker ~]# cd compose_test/
//创建一个测试目录,用于存放docker-compose.yml文件
//建议一个目录下只有一个docker-compose.yml文件
[root@docker compose_test]# vim docker-compose.yml //编写一个docker-compose.yml文件
version: "3" //指定语法的版本
services: //定义服务
nginx:
container_name: web_nginx //运行的容器名
image: nginx //使用的镜像
restart: always //随docker服务的启动而启动
ports:
- 90:80 //映射的端口
volumes:
- /root/compose_test/webserver:/usr/share/nginx/html //本地与容器挂载的目录
//编写文件注意缩进
[root@docker compose_test]# docker-compose up -d
//使用当前目录下的docker-compose.yml文件生成相应的容器
//“-d”选项,表示后台运行,如果不指定,默认则在前台运行,会占用终端
[root@docker compose_test]# docker ps //查看运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c674041cc65b nginx "nginx -g 'daemon of…" 8 minutes ago Up 8 minutes 0.0.0.0:90->80/tcp web_nginx
[root@docker compose_test]# echo "hello world" > webserver/index.html
//创建测试网页
[root@docker compose_test]# curl 127.0.0.1:90
hello world
//访问测试
[root@docker compose_test]# docker-compose stop
//通过.yml文件停止文件中指定的容器
[root@docker compose_test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//查看效果
[root@docker ~]# docker-compose -f /root/compose_test/docker-compose.yml up -d
//可以使用“-f”选项来指定yml文件启动文件中定义的容器
(2) compose + dockerfile mirror Construction
[root@localhost ~]# mkdir compose && cd compose //创建一个测试目录并进入
[root@localhost compose]# vim Dockerfile //创建dockerfile
FROM nginx:latest
ADD html /usr/share/nginx/html
[root@localhost compose]# vim docker-compose.yml //编写yml文件
version: "3"
services:
nginx:
build: . //这里指定dockerfile的路径,可以写相对路径或绝对路径
container_name: mynginx //生成的容器名称
image: mynginx //使用dockerfile生成的镜像名称
restart: always
ports:
- 70:80
[root@localhost compose]# mkdir html
[root@localhost compose]# echo "hello world" > html/index.html //创建网页目录
[root@localhost compose]# docker-compose build //就是将dockerfile文件生成镜像
[root@localhost compose]# docker-compose up -d //直接生成容器,上一条命令可以忽略
[root@localhost compose]# curl 127.0.0.1:70
hello world
//测试效果
(3) build a blog platform file using .yml
[root@localhost ~]# mkdir wordpress && cd wordpress //创建测试目录
[root@localhost wordpress]# vim docker-compose.yml //编写yml文件
version: "3.1"
services:
wordprss:
image: wordpress //指定使用的镜像
restart: always
ports:
- 8080:80 //指定映射的端口
environment: //修改容器内部的环境变量
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: 123.com
WORDPRESS_DB_NAME: wordpress
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: 123.com
MYSQL_ROOT_PASSWORD: 123.com
[root@localhost wordpress]# docker-compose up -d //生成相应的容器,并在后台运行
[root@localhost wordpress]# netstat -anpt | grep 8080 //确定端口在监听
tcp6 0 0 :::8080 :::* LISTEN 5546/docker-proxy
[root@localhost wordpress]# docker ps | grep word //确定容器在运行
81dd5fe032a6 wordpress "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 0.0.0.0:8080->80/tcp wordpress_wordprss_1
702b530d7679 mysql:5.7 "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 3306/tcp, 33060/tcp wordpress_db_1
[root@localhost wordpress]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
[root@localhost wordpress]# sysctl -p
net.ipv4.ip_forward = 1
Now you can access the test page. Figure:
visit the blog to build your own success!
Three, Docker monitoring
(1) docker comes with the monitor command
[root@localhost ~]# docker top wordpress_wordprss_1 //查看容器的使用状态
UID PID PPID C STIME TTY TIME CMD
root 5601 5569 0 20:53 ? 00:00:00 apache2 -DFOREGROUND
33 6073 5601 0 20:54 ? 00:00:00 apache2 -DFOREGROUND
33 6074 5601 0 20:54 ? 00:00:00 apache2 -DFOREGROUND
33 6075 5601 0 20:54 ? 00:00:00 apache2 -DFOREGROUND
33 6076 5601 0 20:54 ? 00:00:00 apache2 -DFOREGROUND
33 6077 5601 0 20:54 ? 00:00:00 apache2 -DFOREGROUND
33 6096 5601 0 20:54 ? 00:00:00 apache2 -DFOREGROUND
33 6098 5601 0 20:54 ? 00:00:00 apache2 -DFOREGROUND
33 6099 5601 0 20:54 ? 00:00:00 apache2 -DFOREGROUND
33 6100 5601 0 20:54 ? 00:00:00 apache2 -DFOREGROUND
33 6155 5601 0 20:57 ? 00:00:00 apache2 -DFOREGROUND
[root@localhost ~]# docker stats wordpress_wordprss_1
[root@localhost ~]# docker logs wordpress_wordprss_1
//这三条都是容器本身自带的监控命令
(2) sysdig
[root@localhost ~]# docker run -it --rm --name sysdig --privileged=true --volume=/var/run/docker.sock:/host/var/run/docker.sock --volume=/dev:/host/dev --volume=/proc:/host/proc:ro --volume=/boot:/host/boot:ro --volume=/lib/modules:/host/lib/modules:ro --volume=/usr:/host/usr:ro sysdig/sysdig
//创建一个容器并自动进入容器中
//--rm:随着退出容器而被删除;
//--privileged=true:赋予特殊权限;
root@711dbeb59fdd:/# csysdig //执行这条命令
As shown:
figure may be used to operate the keyboard and mouse!
(3)scope
[root@localhost ~]# curl -L git.io/scope -o /usr/local/bin/scope
[root@localhost ~]# chmod +x /usr/local/bin/scope //下载安装脚本
[root@localhost ~]# scope launch //以容器方式启动
……………………
Weave Scope is listening at the following URL(s):
* http://172.21.0.1:4040/
* http://192.168.122.1:4040/
* http://172.22.0.1:4040/
* http://172.20.0.1:4040/
* http://172.18.0.1:4040/
* http://172.19.0.1:4040/
* http://192.168.1.1:4040/
//根据末尾的提示信息进行访问
Figure:
According to the figures suggest, you can click on their own to test!
If you want to monitor two words: host name must be distinguished as follows:
[root@dockerA ~]# curl -L git.io/scope -o /usr/local/bin/scope
[root@dockerA ~]# chmod +x /usr/local/bin/scope
[root@dockerA ~]# scope launch 192.168.1.1 192.168.1.2 //首选指定本地的IP,再指定对方的IP
[root@dockerA ~]# docker run -itd --name http httpd //运行一个容器进行测试
[root@dockerB ~]# curl -L git.io/scope -o /usr/local/bin/scope
[root@dockerB ~]# chmod +x /usr/local/bin/scope
[root@dockerB ~]# scope launch 192.168.1.2 192.168.1.1
[root@dockerB ~]# docker run -itd --name nginx nginx
Access (dockerA, dockerB Renyiyitai to) test:
---------- article. Thank watch ----------