Container Technology|Compose of Docker Three Musketeers
收录于话题
#Docker 专辑
9个
Click on the "Migrant Workers Brother Linux Operation and Maintenance" above, and select the "Top Official Account".
Interesting and meaningful articles will be delivered as soon as possible!
Introduction to Three Musketeers
docker-machine
The docker technology is implemented based on the cgroup technology of the Linux kernel. So the question is, is it impossible to use docker technology on non-Linux platforms? The answer is yes, but obviously a virtual machine is needed to simulate the Linux environment.
Docker-machine is officially proposed by the docker company. It is used to quickly create virtual machines with docker services on various platforms. You can even specify the driver to customize the implementation principle of the virtual machine (usually virtualbox).
docker-compose
After the docker image is created, it is often necessary to manually pull to obtain the image, and then execute the run command to run it. When a service needs to use multiple containers, and various dependencies and connections are generated between the containers, the manual operation of deploying a service is very annoying.
The docker-compose technology is to write all the container deployment methods, file mappings, container connections and other configurations in a configuration file through a .yml configuration file, and finally only need to execute the docker-compose up command. Installing containers one by one and automatically deploying them like executing scripts greatly facilitates the deployment of complex services.
docker-swarm
Swarm is a cluster technology based on the docker platform. It can quickly create a docker cluster with a few simple instructions, and then deploy applications on the shared network of the cluster, and finally realize distributed services.
docker-compose workflow
Using Compose only requires three steps:
1. Use an environment that defines your application, Dockerfile so that it can be reproduced anywhere.
2. Define the services that compose your application, docker-compose.yml, so that they can run together in an isolated environment.
3. Run docker-compose up and compose to start and run your entire application.
Reference article: https://docs.docker.com/compose/overview/
Docker-compose installation and deployment
[root@docker ~]# 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 617 0 617 0 0 396 0 --:--:-- 0:00:01 --:--:-- 397
100 10.3M 100 10.3M 0 0 678k 0 0:00:15 0:00:15 --:--:-- 1876k
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
[root@docker ~]# docker-compose --version
docker-compose version 1.21.2, build a133471
Introduction to docker-compose management commands
Compose has commands to manage the entire life cycle of an application:
Start, stop and rebuild the service
View the status of the
running service Stream the log output
of the running service Run a one-time command on the service
[root@docker ~]# docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
When docker-compose is running, you need to specify the service name, you can specify multiple at the same time, or you don't need to specify it. When not specified, the default is to execute commands for all services in the configuration file.
-f #Used to specify the configuration file
-p #Used to specify the project name
#docker-compose build
用来创建或重新创建服务使用的镜像
docker-compose build service_a
创建一个镜像名叫service_a
#docker-compose kill
用于通过容器发送SIGKILL信号强行停止服务
#docker-compose logs
显示service的日志信息
#docker-compose pause/unpause
docker-compose pause #暂停服务
docker-compose unpause #恢复被暂停的服务
#docker-compose port
用于查看服务中的端口与物理机的映射关系
docker-compose port nginx_web 80
查看服务中80端口映射到物理机上的那个端口
#dokcer-compose ps
用于显示当前项目下的容器
注意,此命令与docker ps不同作用,此命令会显示停止后的容器(状态为Exited),只征对某个项目。
#docker-compose pull
用于拉取服务依赖的镜像
#docker-compose restart
用于重启某个服务中的所有容器
docker-compose restart service_name
只有正在运行的服务可以使用重启命令,停止的服务是不可以重启
#docker-compose rm
删除停止的服务(服务里的容器)
-f #强制删除
-v #删除与容器相关的卷(volumes)
#docker-compose run
用于在服务中运行一个一次性的命令。这个命令会新建一个容器,它的配置和srvice的配置相同。
但两者之间还是有两点不同之处
1、run指定的命令会直接覆盖掉service配置中指定的命令
2、run命令启动的容器不会创建在service配置中指定的端口,如果需要指定使用--service-ports指定
#docker-compose start/stop
docker-compose start 启动运行某个服务的所有容器
docker-compose stop 启动运行某个服务的所有容器
#docker-compose scale
指定某个服务启动的容器个数
[root@docker ~]# docker-compose scale --help
Numbers are specified in the form `service=num` as arguments.
For example:
$ docker-compose scale web=2 worker=3
This command is deprecated. Use the up command with the `--scale` flag
instead.
Usage: scale [options] [SERVICE=NUM...]
Options:
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)
Docker-compose configuration file example
An example file of docker-compose.yml is as follows
version: "3"
services:
nginx:
container_name: web-nginx
image: nginx:latest
restart: always
ports:
- 80:80
volumes:
- ./webserver:/webserver
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
#下面将配置文件做个简单的解释说明
docker-compose的配置文件是一个.yml格式的文件
第一部分
version: "3" #指定语法的版本
第二部分
services: #定义服务
nginx: #服务的名称,-p参数后接服务名称
container_name: web-nginx #容器的名称
image: nginx:latest #镜像
restart: always
ports: #端口映射
- 80:80
第三部分
volumes: #物理机与容器的磁盘映射关系
- ./webserver:/webserver
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
The overall directory structure is as follows
[root@docker docker]# tree ./
./
├── docker-compose.yml
├── nginx
│?? └── nginx.conf
└── webserver
└── index.html
2 directories, 3 files
The configuration file is as follows
[root@docker docker]# cat webserver/index.html
welcome to nginx server!!!!!!!!!
[root@docker docker]# cat nginx/nginx.conf
#user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
client_max_body_size 10m;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root /webserver;
index index.html index.htm;
}
}
include /etc/nginx/conf.d/*.conf;
}
Start the container with the command
[root@docker docker]# docker-compose up -d
Pulling nginx (nginx:1.14)...
Trying to pull repository docker.io/library/nginx ...
1.14: Pulling from docker.io/library/nginx
f2aa67a397c4: Already exists
6160d1ac49e9: Pull complete
046b67408776: Pull complete
Digest: sha256:85ab7c44474df01422fe8fdbf9c28e497df427e8a67ce6d47ba027c49be4bdc6
Status: Downloaded newer image for docker.io/nginx:1.14
Creating nginx-server ... done
[root@docker docker]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 891 root 4u IPv6 1187080 0t0 TCP *:http (LISTEN)
[root@docker docker]# docker ps |grep nginx
07ca899cc44b nginx:1.14 "nginx -g 'daemon ..." 29 seconds ago Up 28 seconds 0.0.0.0:80->80/tcp nginx-server
#如果启动时不指定里面的服务名称,就是直接启动配置文件里所有的服务
Browser test is normal access
Then we modify the corresponding home page file as follows
[root@docker docker]# cat webserver/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>welcome to nginx web stie</title>
</head>
<body>
<h2>欢迎来nginx站点</h2>
</body>
</html>
Open the browser again to see the effect
Is it very convenient
Use Docker-Compose to deploy nginx proxy Tomcat cluster to achieve load balancing
The general steps are divided into the following four steps
1. Download the required files tomcat, jdk
2. Write dockerfile to deploy tomcat and java environment, generate image file
3. Write docker-compose.yml configuration file, start all container services
4. Test load balancing
specific configuration files are as follows
#整个目录结构
[root@master java]# tree ./
./
├── docker-compose.yml
├── etc
│ └── localtime
├── nginx
│ └── nginx.conf
├── tomcat
│ ├── apache-tomcat-8.5.31.tar.gz
│ ├── Dockerfile
│ └── jdk-8u144-linux-x64.tar.gz
└── webserver
├── tomcatA
│ └── index.jsp
└── tomcatB
└── index.jsp
6 directories, 8 files
#两个测试首页文件
[root@master java]# cat webserver/tomcatA/index.jsp
welcome to tomcat-A server
[root@master java]# cat webserver/tomcatB/index.jsp
welcome to tomcat-B server
#配置文件
[root@master java]# cat docker-compose.yml
version: "3"
services:
nginx:
image: nginx:1.14
restart: always
ports:
- 80:80
links:
- tomcat1:tomcat1
- tomcat2:tomcat2
volumes:
- ./webserver:/webserver
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./etc/localtime:/etc/localtime
depends_on:
- tomcat1
- tomcat2
tomcat1:
hostname: tomcat1
build: ./tomcat
volumes:
- ./webserver/tomcatA:/usr/local/apache-tomcat-8.5.31/webapps/ROOT
- ./etc/localtime:/etc/localtime
tomcat2:
hostname: tomcat2
build: ./tomcat
volumes:
- ./webserver/tomcatB:/usr/local/apache-tomcat-8.5.31/webapps/ROOT
- ./etc/localtime:/etc/localtime
#安装JAVA环境
[root@master java]# cat tomcat/Dockerfile
FROM centos
ADD jdk-8u144-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_144
ADD apache-tomcat-8.5.31.tar.gz /usr/local
EXPOSE 8080
ENTRYPOINT ["/usr/local/apache-tomcat-8.5.31/bin/catalina.sh", "run"]
Start all container services
[root@master java]# docker-compose up
Building tomcat1
Step 1/6 : FROM centos
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
7dc0dca2b151: Pull complete
Digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322
Status: Downloaded newer image for docker.io/centos:latest
---> 49f7960eb7e4
Step 2/6 : ADD jdk-8u144-linux-x64.tar.gz /usr/local
---> 8c9e14062a24
Removing intermediate container a499940235ac
Step 3/6 : ENV JAVA_HOME /usr/local/jdk1.8.0_144
---> Running in cefedfd97f61
---> 12528cd5a517
Removing intermediate container cefedfd97f61
Step 4/6 : ADD apache-tomcat-8.5.31.tar.gz /usr/local
---> 246fa08bea1c
Removing intermediate container a1aaaa2bf0b8
Step 5/6 : EXPOSE 8080
---> Running in 87c4b41f3c1e
---> fd207f27b830
Removing intermediate container 87c4b41f3c1e
Step 6/6 : ENTRYPOINT /usr/local/apache-tomcat-8.5.31/bin/catalina.sh run
---> Running in 9adaed8e3ab9
---> b6fc6d3925f7
Removing intermediate container 9adaed8e3ab9
Successfully built b6fc6d3925f7
WARNING: Image for service tomcat1 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building tomcat2
Step 1/6 : FROM centos
---> 49f7960eb7e4
Step 2/6 : ADD jdk-8u144-linux-x64.tar.gz /usr/local
---> Using cache
---> 8c9e14062a24
Step 3/6 : ENV JAVA_HOME /usr/local/jdk1.8.0_144
---> Using cache
---> 12528cd5a517
Step 4/6 : ADD apache-tomcat-8.5.31.tar.gz /usr/local
---> Using cache
---> 246fa08bea1c
Step 5/6 : EXPOSE 8080
---> Using cache
---> fd207f27b830
Step 6/6 : ENTRYPOINT /usr/local/apache-tomcat-8.5.31/bin/catalina.sh run
---> Using cache
---> b6fc6d3925f7
Successfully built b6fc6d3925f7
WARNING: Image for service tomcat2 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling nginx (nginx:1.14)...
Trying to pull repository docker.io/library/nginx ...
1.14: Pulling from docker.io/library/nginx
f2aa67a397c4: Already exists
6160d1ac49e9: Pull complete
046b67408776: Pull complete
Digest: sha256:85ab7c44474df01422fe8fdbf9c28e497df427e8a67ce6d47ba027c49be4bdc6
Status: Downloaded newer image for docker.io/nginx:1.14
Creating java_tomcat2_1 ... done
Creating java_tomcat1_1 ... done
Creating java_nginx_1 ... done
Check the startup situation
[root@master java]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------
java_nginx_1 nginx -g daemon off; Up 0.0.0.0:80->80/tcp
java_tomcat1_1 /usr/local/apache-tomcat-8 ... Up 8080/tcp
java_tomcat2_1 /usr/local/apache-tomcat-8 ... Up 8080/tcp
Check load balancing
[root@master java]# curl http://localhost
welcome to tomcat-A server
[root@master java]# curl http://localhost
welcome to tomcat-B server
[root@master java]# curl http://localhost
welcome to tomcat-A server
[root@master java]# curl http://localhost
welcome to tomcat-B server
Browser access test load balancing
View log output information
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:36 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:36 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:39 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:30 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:31 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:32 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:33 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:19:32 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
The previous docker series articles are as follows:
[Container Technology] Introduction to Docker Container Technology (1)
[Container Technology] Introduction to Docker Container Technology (2)
If this article is helpful to you, please forward it and share it with more people in need People; at the same time, friends are welcome to leave a message, praise, make bricks, forward and support!