docker-compose

Table of Contents

1. Introduction to docker-compose:

1.docker-compose overview:

2. The difference between docker-compose and traditional build:

3. Three major concepts of docker-compose:

2. YAML file format and writing notes:

1. YAML file format:

2. Notes on YAML format:

3. YAML writing format:

3. YAML data structure case:

3. Docker compose fields and commands:

1. Docker Compose configuration common fields:

2. Docker Compose common commands:

4. Docker Compose environment installation:

5. Docker-compose deploys nginx:

1. Prepare dependency files:

2. Write Dockerdile file:

3. Write the configuration file docker-compose.yml file:

4. Generate image container:

5. View the directory structure:

6. Access test:


1. Introduction to docker-compose:

1.docker-compose overview:

  • docker-compose is a tool for orchestrating and managing container clusters on a single machine. Developed using python, any platform that can run docker can also use docker-compose to orchestrate and manage containers.
  • The essence is to define the startup parameters and dependencies of multiple containers in the docker-compose configuration template file in yaml format, and use docker-compose to start and manage the container cluster according to the configuration of this template file.

2. The difference between docker-compose and traditional build:

Dockerfile file –> docker build builds images one by one –> docker run creates and starts containers one by one

Dockerfile file –> docker-compose automatically completes all image building and container creation, startup, and management

3. The three major concepts of docker-compose:

Project, service and container

  • By default, the project directory name is used as the project name. It is supported to use -p or –project-name to specify the project name.
  • A docker-compose configuration template file must be included in the project directory. The default is docker-compose.yml. You can use -f or –file to specify the project configuration template file.
  • The configuration template file must contain one or more services. Each service includes the name, image, mapped port, environment variables, mount point, network mode, dependencies and other configuration parameters for container startup.

2. YAML file format and writing notes:

1. YAML file format:

  • YAML is a markup language that can display data serialization format very intuitively and is highly readable.
  • Similar to json data description language, but the syntax is much simpler than json.
  • YAML data structures are represented by indentation, consecutive items are represented by minus signs, key-value pairs are separated by colons, arrays are enclosed by square brackets [ ], and bash is enclosed by curly braces { }.

2. Notes on YAML format:

  • Tab key indentation is not supported, only spaces can be used for indentation.

  • Usually indent 2 spaces at the beginning

  • Indent 1 space after the character, such as colon [:], comma [,], horizontal bar [-]

  • Use # sign to indicate comments

  • If special characters are included, use single quotes [‘ ‘] to enclose them as ordinary characters. If double quotes [” “] are used to express the meaning of the special characters themselves,

  • Boolean values must be enclosed in [” “]

  • YAML is case sensitive

3. YAML writing format:

Horizontal writing:
ky31: ["qqq", "www", "eee"]

Vertical writing:
ky32:
- "qqq"
- "www"
- "eee"

3. YAML data structure case:

#Key-value pair representation
animal:pets
 
#Array: a set of lists arranged in order
- cat
-dog
-goldfish
 
#Boolean value
debug: "true"
debug: "false"
 
#yamlExample
languages: #Sequence mapping
  -java
  - Golang
  -Python
  
websites: #mapping of mappings
  Baidu: www.baidu.com
  Wangyi: www.163.com
  Souhu: www.souhu.com
 
#or
languages: ["java","Golong","Python"]
websites:
  Baidu:
    www.baidu.com
  Wangyi:
    www.163.com
  Souhu:
    www.souhu.com
 
#Json format
{
  languages: [
    'Java',
    'Golong',
    'Python',
  ],
  websites: [
    Baidu: 'www.baidu.com',
    Wangyi: 'www.163.com',
    Souhu: 'www.souhu.com',
  ]
}

3. Docker compose fields and commands:

1. Docker Compose configuration common fields:

Field Description
build Specify the Dockerfile file name (the Dockerfile file to be specified needs to be specified with the dockefile tag in the sub-label of the build tag)
dockerfile Build image context path
context Can be the dockerfile path, or the URL address of the git warehouse when executing
context Can be the dockerfile path, or the URL address of the git warehouse when executing
images Specify the image (already exists)
td>
command Executing the command will overwrite the command executed by default after the container is started (it will overwrite the CMD instruction in the dockefile)
container_name Specify the container name. Since the container name is unique, if you specify a custom name, you cannot scale the specified number of containers.
deploy Specifies configuration related to deployment and running services, which can only be used in swarm mode
environment Add environment variables
networks Join the network and reference the entries under top-level networks
network-mode Set the network mode of the container
ports Expose the container port, the same as -p , but the port cannot be lower than 60
volumes Mount a host directory or command volume to the container. The command volume must define the volume name in the top-level volumes
volumes_from Mount volumes from another service or container, optional parameters: ro and rw (only supported in version ‘2’)
hostname Set kernel parameters in the container
links Connect to Another container, – service name [: ]
privileged is used to give root permissions to the container. Note that it is unsafe, true
restart

Restart strategy, defining whether to restart the container
1. no, the default policy, does not restart the container when the container exits

2. On-failure, the container will only be restarted when the container exits abnormally (exit status is not 0)
3. on-failure: 3 When the container exits abnormally, restart the container up to 3 times.
4. always, always restart the container when the container exits.
5. unless-stopped, always restart the container when the container exits, but does not consider containers that have been stopped when the Docker daemon is started.

depends_on This tag is used to solve container dependencies and startup issues. If you want to start the application container, you need to start the database container first. php:depends_on:- apache- mysql

2. Docker Compose common commands:

Running these commands needs to be used in conjunction with docker-compose.

Command Description
build Rebuild service
ps List containers
up Create and start containers
exec Execute commands in the container
scale Specify the number of service containers to start
top Display running container processes
logs View the output of the service container
down Delete the container, network, data volume and image
stop/start/restart Stop/start/restart service

4. Docker Compose environment installation:

Docker Compose is an independent product of Docker, so you need to install Docker and then install Docker Compose separately.

#Download
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#Install
chmod +x /usr/local/bin/docker-compose
#View version
docker-compose --version

5. Docker-compose deploys nginx:

1. Prepare dependency files:

mkdir -p /opt/compose_nginx/nginx /opt/compose_nginx/wwwroot
cd /opt/compose_nginx/nginx
cp nginx-1.12.0.tar.gz ./

2. Write Dockerdile file:

cd /opt/compose_nginx/nginx
vimDockerfile
#Based on base image
FROM centos:7
#User Info
MAINTAINER this is nginx image <wl>
#Add environment package
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#Upload the nginx software compressed package and decompress it
ADD nginx-1.12.0.tar.gz /usr/local/src/
#Specify working directory
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module & amp; & amp; make & amp; & amp; make install
ENV PATH /usr/local/nginx/sbin:$PATH
#Specify http and https ports
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]

3. Write the configuration file docker-compose.yml file:

vim /opt/compose_nginx/docker-compose.yml
version: '3'
  #Define the orchestration version, there are 3 versions in total (1, 2, 3), 1 is eliminated, 2 can only be arranged on a single machine, and 3 can realize single machine orchestration or multi-machine orchestration.
services:
  nginx:
    container_name: web1
    hostname: nginx #The host name of the container is nginx
    build: #Define the process of creating a container. If you use an existing image, write images directly:
      context: ./nginx #Specify the directory location where the container is created (the directory where dockerfile is stored) nginx/ of the current path
      dockerfile: Dockerfile
    ports: #Define ports
      - 1216:80
      - 1217:443
    networks: #Specify network
      lnmp:
        ipv4_address: 172.18.0.10
    volumes: #Data volume, mounting directory
      - ./wwwroot:/usr/local/nginx/html
networks: #Custom network
  lnmp:
    driver:bridge
    ipam:
      config:
        - subnet: 172.18.0.0/16


echo "this is wzw" >/opt/compose_nginx/wwwroot/index.html

4. Generate image container:

cd /opt/compose_nginx/
docker-compose -f docker-compose.yml up -d

————————————————– ————————————————– ——
-f, –file FILE: Use a specific compose template file, the default is docker-compose.yml
-p, –project-name NAME: Specify the project name, the directory name is used by default
-d: Run in the background
————————————————– ————————————————– ——

5. View directory structure:

cd /opt/compose_nginx
tree

6. Access test:

curl 20.0.0.55:1234
or
Web page access: http:20.0.0.55:1234

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skill treeContainer (docker)Install docker16854 people are learning the system