Docker Compose container orchestration

Posted by jskywalker on Wed, 02 Feb 2022 00:35:53 +0100

catalogue

Docker Compose overview

compose features

Multiple isolated environments on a single host

Preserve volume data when creating containers

Recreate only changed containers

Variables and moving combinations between environments

yml file format

Common fields of docker compose configuration

docker compose common commands

Docker compose deploy Nginx service

summary

Docker Compose overview

According to the official documents, there are probably some translations

Compose is a tool for defining and running multi container Docker applications. With compose, you can use YAML files to configure the services of your application. Then, with one command, you can create and start all services from your configuration

Compose applies to all environments: production, staging, development, testing, and CI workflow.

Using Compose is basically a three-step process:

  1. Use a to define the environment of your application, Dockerfile, so that it can be copied 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 the Docker compose command starts and runs the entire application. You can also use the Docker compose binary file to run docker compose up.

Compose has commands for managing the entire application lifecycle:

  • Start, stop and rebuild services
  • View the status of running services
  • Streaming log output of running services
  • Run a one-time command on the service

compose features

The features that Compose makes effective are:

  • Multiple isolated environments on a single host
  • Preserve data volume data when creating containers
  • Recreate only changed containers
  • Move combination between variable and environment

Multiple isolated environments on a single host

Compose uses project names to isolate environments from each other. You can use this project name in several different contexts:

  • On the development host, create multiple copies of a single environment, for example, when you want to run a stable copy for each functional branch of the project
  • On the CI server, to prevent build interference, you can set the project name to a unique build number
  • On shared or development hosts to prevent interference between different items that may use the same service name

The default project name is the base name of the project directory. You can use the - p command line option or {combine_ PROJECT_ The name environment variable sets the custom project name.

The default project directory is the base directory for Compose files. You can use the -- project directory command line option to define its custom values.

Preserve volume data when creating containers

Compose preserves all volumes used by your service. At runtime, docker compose up, if it finds any container that was previously run, it will copy the volume from the old container to the new container. This process ensures that any data you create in the volume is not lost.

Recreate only changed containers

Compose caches the configuration used to create containers. When you restart an unchanged service, compose reuses the existing container. Reusing containers means you can change your environment very quickly.

Variables and moving combinations between environments

Compose supports variables in compose files. You can use these variables to customize your own combinations for different environments or different users.

You can use this field or extend the Compose file by creating multiple Compose files

yml file format

YAML is a kind of markup language, which is a very intuitive data serialization format, file format and writing precautions

● tab indentation is not supported, and space indentation is required·

● usually indent 2 spaces at the beginning

● indent a space after a character, such as colon, comma, bar

● note with # number

● if special characters are included, enclose them in single quotation marks

● Boolean values must be enclosed in quotation marks

Common fields of docker compose configuration

fielddescribe
build dockerfile contextSpecify the Dockerfile file name to build the image context path
imageSpecify mirror
commandExecute the command to override the default command
container nameSpecify the container name. Since the container name is unique, you cannot scale (replica set) if you specify a custom name
deploySpecify the configuration related to deploying and running services, which can only be used in Swarm mode
environmentAdd environment variable
networksJoin the network
portsExpose the container port, the same as - p, but the port cannot be less than 60
volumesMount the host path or command volume
restartRestart policy, default no, always, no failure, unless stopped
hostnameContainer host name

docker compose common commands

fielddescribe
buildRebuild service
psList containers
upCreate and launch containers
execExecute commands in the container
scaleSpecify the number of starts of a service container
topShow container processes
logsView container output
downDelete containers, networks, data volumes, and mirrors
stop/start/restartStop / start / restart service

Docker compose deploy Nginx service

Download compose

[root@localhost ~]# curl -L "https://github.com/docker/compose/releases/download/1.21.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Create a test directory

[root@localhost ~]# mkdir test

The structure is as follows

[root@localhost ~/test]# tree
.
└── compose_nginx
    ├── docker-compose.yml
    ├── nginx
    │   ├── Dockerfile
    │   └── nginx-1.15.9.tar.gz
    └── wwwroot
        └── index.html

3 directories, 4 files

Writing yml files

[root@localhost ~/test/compose_nginx]# cat docker-compose.yml
version: '3'
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 1314:80
      - 2580:443
    networks:
      - cluster
    volumes:
      - ./wwwroot:/usr/local/nginx/html
networks:
  cluster:

Dockerfile file

[root@localhost ~/test/compose_nginx/nginx]# cat Dockerfile
FROM centos:7 as build
ADD nginx-1.15.9.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.9
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &>/dev/null && \
      yum clean all && \
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \
       ./configure --prefix=/usr/local/nginx &>/dev/nuil && \
        make &>/dev/null && \
        make install &>/dev/null && \
        rm -rf /mnt/nginx-1.15.9
FROM centos:7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY --from=build /usr/local/nginx /usr/local/nginx
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;"]

index.html file

[root@localhost ~/test/compose_nginx/wwwroot]# cat index.html
this is xiaobin first docker compose

function

[root@localhost ~/test/compose_nginx]# docker-compose -f docker-compose.yml up -d
Building nginx
Step 1/9 : FROM centos:7 as build
 ---> eeb6ee3f44bd
Step 2/9 : ADD nginx-1.15.9.tar.gz /mnt
 ---> 250ae0c6dec2
Step 3/9 : WORKDIR /mnt/nginx-1.15.9
 ---> Running in 111652508c7f
Removing intermediate container 111652508c7f
 ---> 50c455161330
Step 4/9 : RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &>/dev/null &&       yum clean all                            && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc &&        ./configure --prefix=/usr/lo                           cal/nginx &>/dev/nuil &&         make &>/dev/null &&         make install &>/dev/null &&         rm -rf /mn                           t/nginx-1.15.9
 ---> Running in c78e0cd749c3
Loaded plugins: fastestmirror, ovl
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors
Removing intermediate container c78e0cd749c3
 ---> a7d34a938fe2
Step 5/9 : FROM centos:7
 ---> eeb6ee3f44bd
Step 6/9 : EXPOSE 80
 ---> Running in 452a82532ac7
Removing intermediate container 452a82532ac7
 ---> 58351b0aaeb6
Step 7/9 : VOLUME ["/usr/local/nginx/html"]
 ---> Running in 43ec47184966
Removing intermediate container 43ec47184966
 ---> 81786e63ec57
Step 8/9 : COPY --from=build /usr/local/nginx /usr/local/nginx
 ---> 5d31587f9dad
Step 9/9 : CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;"]
 ---> Running in f10c92bb8449
Removing intermediate container f10c92bb8449
 ---> 5cb6aef432c2
Successfully built 5cb6aef432c2
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

View container

Testing

[root@localhost ~/test/compose_nginx/wwwroot]# curl 192.168.37.107:1314
this is xiaobin first docker compose

So how do we know which containers are created by docker compose

Using the docker compose PS command

[root@localhost ~/test/compose_nginx]# docker-compose ps
        Name                       Command               State                                   Ports
--------------------------------------------------------------------------------------------------------------------------------------
compose_nginx_nginx_1   /usr/local/nginx/sbin/ngin ...   Up      0.0.0.0:2580->443/tcp,:::2580->443/tcp,
                                                                 0.0.0.0:1314->80/tcp,:::1314->80/tcp

It should be noted that it can only be used under the yml directory, otherwise you will be prompted that you can't find

[root@localhost ~]# docker-compose ps
ERROR:
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml

summary

docker compose is an application tool of docker, which configures application services through YAML files

Note the yml writing format, and the scale replica set is very useful in Kubernetes later.

Topics: Operation & Maintenance Docker Container