Container technology |docker The docker-compose of the Three Musketeers

Source: Internet
Author: User
Tags control characters nginx server docker ps

About Three Musketeers Docker-machine

Docker technology is based on the Cgroup technology of the Linux kernel, so the question is, is it not possible to use Docker technology on non-Linux platforms? The answer is yes, but obviously you need to use a virtual machine to simulate a Linux environment.
Docker-machine is the official Docker company that is used to quickly create a technology for virtual machines with Docker services on various platforms, even by specifying driver to customize the implementation of virtual machines (typically VirtualBox).

Docker-compose

After the Docker image is created, it is often necessary to manually pull the mirror to get the image and run it by executing the Run command. The manual operation of deploying a service is annoying when the service needs to use multiple containers and the containers generate dependencies and connections.
Dcoker-compose technology, is through a. YML configuration file, all the container deployment method, file mapping, container connection, and so on a series of configurations written in a configuration file, and finally only need to execute Docker-compose The up command, like executing a script, installs containers and deploys them automatically, greatly facilitating the deployment of complex services.

Docker-swarm

Swarm is a clustering technology based on the Docker platform, where he can quickly create a docker cluster with a few simple instructions, then deploy the application on the shared network of the cluster and eventually implement the distributed service.

Workflow of Docker-compose

Using compose requires only three steps:

1. Use a context that defines your application, dockerfile so it can be reproduced anywhere.
2. Define the services that make up 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 Deployment
[[email protected] ~]# 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  Speed100   617    0   617    0     0    396      0 --:--:--  0:00:01 --:--:--   397100 10.3M  100 10.3M    0     0   678k      0  0:00:15  0:00:15 --:--:-- 1876k[[email protected] ~]# chmod +x /usr/local/bin/docker-compose [[email protected] ~]# docker-compose --versiondocker-compose version 1.21.2, build a133471
The Docker-compose Management Command describes the compose command that manages the entire lifecycle of an application:
  • Start, stop, and rebuild services
    • To view the status of a running service
    • Log output for streaming run service
    • Run a one-time command on a service
[[email protected] ~]# docker-compose--helpdefine and run Multi-container applications with docker.usage:docker-c Ompose [-f <arg> ...] [Options] [COMMAND]  [ARGS ...]                              Docker-compose-h|--helpoptions:-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 (DEB               UG, 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 t O 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 W  Orking directory (default:the path of the Compose file)--compatibility If set, Compose would attempt to convert deploy keys on V3 files to their non-swarm Equivalentcommand S:build Build or Rebuild Services bundle Generate a Docker bundle from the Compose file confi G Validate and view the Compose file create create services down Stop and remove C               Ontainers, 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 publ               IC 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 sto                 P Stop Services Top Display The running processes Unpause Unpause services up Create and Start containers version Show the Docker-compose version information

The docker-compose Runtime is required to specify a service name that can be specified in multiple or unspecified. When not specified, the default is for all service execution commands in the configuration file.

-F #用于指定配置文件
-P #用于指定项目名称

Docker-compose Build
Used to create or recreate the image used by the service
Docker-compose Build Service_a
Create an image called Service_a

Docker-compose Kill
Used to send Sigkill signals via containers Force stop service

Docker-compose logs
Display log information for service

Docker-compose Pause/unpause
Docker-compose Pause #暂停服务
Docker-compose Unpause #恢复被暂停的服务

Docker-compose Port
Used to view the mappings between the ports in the service and the physical machines
Docker-compose Port Nginx_web 80
View 80 ports in the service mapped to that port on the physical machine

Dokcer-compose PS
Used to display containers under the current item
Note that this command differs from Docker PS, and this command displays a stopped container (with a status of exited), only for an item.

Docker-compose Pull
Mirrors used for pull service dependencies

Docker-compose restart
Used to restart all containers in a service
Docker-compose Restart Service_Name
Only the running service can use the restart command, the stopped service is not available to restart

Docker-compose RM
Delete Stopped services (containers in the service)

-F #强制删除
-V #删除与容器相关的卷 (volumes)

Docker-compose Run
Used to run a one-time command in the service. This command creates a new container with the same configuration as the Srvice configuration.
But there are still two differences between the two.

1, the command specified by run will override the command specified in the service configuration directly
2. The container started by the Run command does not create the port specified in the service configuration, if you need to specify using--service-ports

Docker-compose Start/stop
Docker-compose start starts all containers that run a service
Docker-compose stop stops all containers that run a service

Docker-compose Scale
Specify the number of containers that a service starts

[[email protected] ~]# docker-compose scale --helpNumbers are specified in the form `service=num` as arguments.For example:    $ docker-compose scale web=2 worker=3This command is deprecated. Use the up command with the `--scale` flaginstead.Usage: scale [options] [SERVICE=NUM...]Options:  -t, --timeout TIMEOUT      Specify a shutdown timeout in seconds.                             (default: 10)
Docker-compose configuration File Instance

A docker-compose.yml instance file 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

Here is a simple explanation of the configuration file

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

[[email protected] docker]# tree ././├── docker-compose.yml├── nginx│?? └── nginx.conf└── webserver    └── index.html2 directories, 3 files

The configuration file is as follows

  [[email protected] docker]# cat webserver/index.html Welcome to Nginx Server!!!!!!!!! [[email protected] 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;}  

To start a container with a command

[[email protected] docker]# docker-compose up -dPulling nginx (nginx:1.14)...Trying to pull repository docker.io/library/nginx ... 1.14: Pulling from docker.io/library/nginxf2aa67a397c4: Already exists6160d1ac49e9: Pull complete046b67408776: Pull completeDigest: sha256:85ab7c44474df01422fe8fdbf9c28e497df427e8a67ce6d47ba027c49be4bdc6Status: Downloaded newer image for docker.io/nginx:1.14Creating nginx-server ... done[[email protected] docker]# lsof -i :80COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAMEdocker-pr 891 root    4u  IPv6 1187080      0t0  TCP *:http (LISTEN)[[email protected] docker]# docker ps |grep nginx07ca899cc44b   nginx:1.14   "nginx -g ‘daemon ..."   29 seconds ago  Up 28 seconds  0.0.0.0:80->80/tcp  nginx-server#如果启动时不指定里面的服务名称,就是直接启动配置文件里所有的服务

Browser testing for normal access

Then we modify the corresponding homepage file as follows

[[email protected] docker]# cat webserver/index.html <!DOCTYPE html>

Open the browser again to see the effect

#是不是非常的方便

Deploy Nginx proxy tomcat cluster with Docker-compose for load balancing

The overall step is divided into the following four steps

1. Download the required files Tomcat,jdk
2, write dockerfile to the deployment of Tomcat and Java environment, generate image files
3, write docker-compose.yml configuration file, start all container services
4. Test Load Balancing

The specific configuration file is as follows

#整个目录结构[[email protected] 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.jsp6 directories, 8 files

Two Test home files

[[email protected] java]# cat webserver/tomcatA/index.jsp welcome to tomcat-A server[[email protected] java]# cat webserver/tomcatB/index.jsp welcome to tomcat-B server

Configuration file

[[email protected] 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

Installing the Java Environment

[[email protected] java]# cat tomcat/Dockerfile FROM centosADD jdk-8u144-linux-x64.tar.gz /usr/localENV JAVA_HOME /usr/local/jdk1.8.0_144ADD apache-tomcat-8.5.31.tar.gz /usr/localEXPOSE 8080ENTRYPOINT ["/usr/local/apache-tomcat-8.5.31/bin/catalina.sh", "run"]

Start All Container Services

[[email protected] java]# docker-compose upbuilding tomcat1step 1/6: from centostrying to pull repository docker.io/ Library/centos latest:pulling from Docker.io/library/centos7dc0dca2b151:pull completedigest:sha256: b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322status:downloaded newer image for Docker.io/centos : Latest---> 49f7960eb7e4step 2/6: ADD jdk-8u144-linux-x64.tar.gz/usr/local---> 8c9e14062a24removing Intermediate container a499940235acstep 3/6: ENV java_home/usr/local/jdk1.8.0_144---> Running in cefedfd97f61--- ; 12528cd5a517removing Intermediate Container cefedfd97f61step 4/6: ADD apache-tomcat-8.5.31.tar.gz/usr/local---> 246fa08bea1cremoving Intermediate Container a1aaaa2bf0b8step 5/6: EXPOSE 8080---> Running in 87c4b41f3c1e---> fd2 07f27b830removing Intermediate Container 87c4b41f3c1estep 6/6: entrypoint/usr/local/apache-tomcat-8.5.31/bin/ catalina.sh Run---> Running in 9adaed8e3ab9---> B6fc6d3925f7remoVing Tsun Intermediate Container 9adaed8e3ab9successfully built b6fc6d3925f7warning:image for service TOMCAT1 was built Becaus E It did not already exist. To rebuild this image you must use ' docker-compose build ' or ' docker-compose up--build '. Building tomcat2step 1/6: from CentOS---> 49f7960eb7e4step 2/6: ADD jdk-8u144-linux-x64.tar.gz/usr/local---> Us ING cache---> 8c9e14062a24step 3/6: ENV java_home/usr/local/jdk1.8.0_144---> Using cache---> 12528cd5a517ste P 4/6: ADD apache-tomcat-8.5.31.tar.gz/usr/local---> Using cache---> 246fa08bea1cstep 5/6: EXPOSE 8080---> Using Cache---> Fd207f27b830step 6/6: entrypoint/usr/local/apache-tomcat-8.5.31/bin/catalina.sh Run---> Using c Ache---> b6fc6d3925f7successfully built b6fc6d3925f7warning:image for service TOMCAT2 is built because it didn't not AL Ready 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/nginxf2aa67a397c4:already exists6160d1ac49e9:pull complete046b67408776:pull completedigest:sha256:85ab7c44474df01422fe8fdbf9c28e497df427e8a67ce6d47ba027c49be4bdc6status:downloaded newer Image for Docker.io/nginx:1.14creating java_tomcat2_1 ... donecreating java_tomcat1_1 ... donecreating java_nginx_1 ... Done

View boot status

[[email protected] java]# docker-compose ps     Name                   Command               State         Ports       ----------------------------------------------------------------------------java_nginx_1     nginx -g daemon off;             Up      0.0.0.0:80->80/tcpjava_tomcat1_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp          java_tomcat2_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp  
Detecting Load Balancing
[[email protected] java]# curl http://localhostwelcome to tomcat-A server[[email protected] java]# curl http://localhostwelcome to tomcat-B server[[email protected] java]# curl http://localhostwelcome to tomcat-A server[[email protected] java]# curl http://localhostwelcome to tomcat-B server
Browser Access Test Load Balancing

View Log Output Information
nginx_1 | 192.168.22.170--[08/jun/2018:02:14:33 +0000] "get/http/1.1"-"" 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:34 +0000] "get/http/1.1" 499 0 "-" "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:34 +0000] "get/http/1.1"-"" 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" 499 0 "-" "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"-"" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/65.0.3325.181safari/537.36 ""-"nginx_1 | 192.168.22.170--[08/jun/2018:02:14:36 +0000] "get/http/1.1"-"" 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"-"" 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"-"" 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"-"" 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"-"" 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"-"" 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"-"" 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"-"" 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"-"" 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"-"" 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"-"" 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"-"" curl/7.29.0 ""-"nginx_1 | 172.19.0.1--[08/jun/2018:02:18:31 +0000] "get/http/1.1"-"" curl/7.29.0 ""-"nginx_1 | 172.19.0.1--[08/jun/2018:02:18:32 +0000] "get/http/1.1"-"" curl/7.29.0 ""-"nginx_1 | 172.19.0.1--[08/jun/2018:02:18:33 +0000] "get/http/1.1"-"" curl/7.29.0 ""-"nginx_1 | 192.168.22.170--[08/jun/2018:02:19:32 +0000] "get/http/1.1"-"" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/65.0.3325.181 safari/537.36 ""-"

Copyright belongs to the author: from the author of the original works of migrant workers, if required to reprint, please indicate the source, or will be held accountable for legal responsibility

Container technology |docker The docker-compose of the Three Musketeers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.