DOCKER learning _017: Docker-Compose Introduction

dockers Troika

  •  Docker Machine
  •  Docker Swarm
  •  Docker Compose

Docker Compose a presentation

Docker Compose is a container defining and running multiple stand-alone application orchestration tools. By Docker Compose you can use a single YAML file to configure multiple application services, with a single command, you can configure all the services all started up.

1.1 Use of three steps Docker Compose

  • Use Dockerfile defined environment, which ensures that its run anywhere

  • Use docker-compose.yml file defines the service, so that they can run together in a standalone environment

  • Run docker-compose up to start using the docker-compose all applications

1.2 Docker Compose can manage the entire life cycle of the application

  • Start, stop, rebuild service

  • View the running status of the service

  • Streaming service log output

  • Execute commands on a one-time service

Two Docker Compose installation

2.1 binary installation

Download: https: //github.com/docker/compose/releases

 

 

As well as installation, wget can also download the file above, is itself a binary file

 [root@docker-server3 ~]# curl -L https://github.com/docker/compose/releases/download/1.25.0/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    470      0 --:--:--  0:00:01 --:--:--   470
100 16.2M  100 16.2M    0     0   767k      0  0:00:21  0:00:21 --:--:-- 1622k

[root@docker-server3 ~]# chmod +x /usr/local/bin/docker-compose

[root@docker-server3 ~]# docker-compose --version

docker-compose version 1.25.0, build 0a186604

[root@docker-server3 ~]# mkdir /docker-compose

[root@docker-server3 ~]# cd /docker-compose

2.2 simple to use

Write a simple file dockerpose

[root@docker-server3 docker-compose]# vim docker-compose.yml

version: '3'
services:
  httpd-test:
    image: httpd:2.4

[root@docker-server3 docker-compose]# docker-compose up -d

Creating network "docker-compose_default" with the default driver
Pulling httpd-test (httpd:2.4)...
2.4: Pulling from library/httpd
8ec398bc0356: Already exists
354e6904d655: Pull complete
27298e4c749a: Pull complete
10e27104ba69: Pull complete
36412f6b2f6e: Pull complete
Digest: sha256:769018135ba22d3a7a2b91cb89b8de711562cdf51ad6621b2b9b13e95f3798de
Status: Downloaded newer image for httpd:2.4
Creating docker-compose_httpd-test_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
06a7e97739b2        httpd:2.4           "httpd-foreground"   4 minutes ago       Up 4 minutes        80/tcp              docker-compose_httpd-test_1

Delete and then executed, it will got me a different image

[root@docker-server3 docker-compose]# docker rm -f 06a7e97739b2

[root@docker-server3 docker-compose]# docker-compose up -d

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
d184230f2de6        httpd:2.4           "httpd-foreground"   3 seconds ago       Up 2 seconds        80/tcp              docker-compose_httpd-test_1

While creating two containers, modify compose documents

[root@docker-server3 docker-compose]# vi docker-compose.yml

version: '3'
services:
  httpd-test:
    image: httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
    ports:
      - "80:80"

[root@docker-server3 docker-compose]# docker-compose up -d

docker-compose_httpd-test_1 is up-to-date
Creating docker-compose_httpd-test-2_1 ... done

[root@docker-server3 docker-compose]#  cat /data/index.html

just a test

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                NAMES
8b60d28de874        httpd:2.4           "httpd-foreground"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   docker-compose_httpd-test-2_1
d184230f2de6        httpd:2.4           "httpd-foreground"   6 minutes ago       Up 6 minutes        80/tcp               docker-compose_httpd-test_1

[root@docker-server3 docker-compose]# docker exec -it 8b60d28de874 /bin/bash

root@8b60d28de874:/usr/local/apache2# ls /var/www/html/
ca_download  database  index.html  job_logs  psc  redis  registry  secret
root@8b60d28de874:/usr/local/apache2# cat /var/www/html/index.html 
just a test

[root@docker-server3 docker-compose]# vi docker-compose.yml 

version: '3'
services:
  httpd-test:
    image: httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
    ports:
      - "80:80"
  httpd-test-3:
    image: httpd:2.4
    volumes:
      - "/data:/usr/local/apache2/htdocs"
    ports:
      - "8080:80"

[root@docker-server3 docker-compose]# docker-compose up -d

docker-compose_httpd-test_1 is up-to-date
docker-compose_httpd-test-2_1 is up-to-date
Creating docker-compose_httpd-test-3_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
a3d24a4c5d25        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->80/tcp   docker-compose_httpd-test-3_1
8b60d28de874        httpd:2.4           "httpd-foreground"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp     docker-compose_httpd-test-2_1
d184230f2de6        httpd:2.4           "httpd-foreground"   14 minutes ago      Up 14 minutes       80/tcp                 docker-compose_httpd-test_1

[root@docker-server3 docker-compose]# curl http://192.168.132.133:8080

just a test

[root@docker-server3 docker-compose]# echo 111 > /data/111.html

[root@docker-server3 docker-compose]# curl http://192.168.132.133:8080/111.html

111

The short answer is more than a docker operation

2.3 docker-compose the common commands

In a simple example above, we have used docker-compose upto start a docker-compose.yml file defines the service. Just passed docker-compose upthough the service started, when a docker-compose the instruction was executed in the foreground, if it needs to be put in the background, you can use -dparameters:

Compose-up Docker - d background #
Docker -compose -f xx.yaml up - d # yml if the file is not docker-compose.yml need to use the -f Force specified
Docker - Compose Down # delete container
Docker -compose PS # also see the role of the container, but it can only view docker-compose initiated container, use docker run to start the container can not see 
Docker - Compose Start
docker-compose stop
docker-compose restart 
docker-compose logs

[root@docker-server3 docker-compose]# docker-compose down

Stopping docker-compose_httpd-test-3_1 ... done
Stopping docker-compose_httpd-test-2_1 ... done
Stopping docker-compose_httpd-test_1   ... done
Removing docker-compose_httpd-test-3_1 ... done
Removing docker-compose_httpd-test-2_1 ... done
Removing docker-compose_httpd-test_1   ... done
Removing network docker-compose_default

[root@docker-server3 docker-compose]# docker-compose up -d 

Creating network "docker-compose_default" with the default driver
Creating docker-compose_httpd-test-3_1 ... done
Creating docker-compose_httpd-test_1   ... done
Creating docker-compose_httpd-test-2_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
ddf6b2a8f4bb        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        0.0.0.0:80->80/tcp     docker-compose_httpd-test-2_1
a1dabe398019        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        80/tcp                 docker-compose_httpd-test_1
81030fea5437        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->80/tcp   docker-compose_httpd-test-3_1

[root@docker-server3 docker-compose]# docker-compose ps -a

            Name                    Command        State          Ports        
-------------------------------------------------------------------------------
docker-compose_httpd-test-2_1   httpd-foreground   Up      0.0.0.0:80->80/tcp  
docker-compose_httpd-test-3_1   httpd-foreground   Up      0.0.0.0:8080->80/tcp
docker-compose_httpd-test_1     httpd-foreground   Up      80/tcp

Start and stop operation

The front is a brief introduction to the operation of the docker-compose the subsequent careful study docker-compose the syntax and case


博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!

Guess you like

Origin www.cnblogs.com/zyxnhr/p/12158816.html