Docker Compose container orchestration theory introduction and detailed experimental steps

1. Introduction to Docker Compose

1.1 Introduction to Docker Compose

  • The predecessor of Docker Compose was Fig. After Fig was acquired by Docker, it was officially renamed Compose. Compose is downward compatible with Fig;
  • The use of Docker Compose no longer requires the use of shell scripts to start the container;
  • Docker Compose is very suitable for scenarios where multiple containers are combined for development.

1.2 Docker Compose file structure

vim docker-compose.yml
  • YAML is a very intuitive data serialization format with a markup language. The file format and writing notes are as follows:
1.不支持制表符Tab 建缩进,需要使用空格缩进
2.通常开头缩进2个空格
3.字符后缩进1个空格,如:冒号,逗号,横杆
4.用井号注释
5.如果包含特殊字符用单引号引起来
6.布尔值必须用单引号括起来

1.3 Three steps used by Docker compose

1.使用Dokcerfile 定义应用程序的环境
2.使用docker-compose.yml 定义构成应用程序的服务,这样它们就可以再隔离环境中一起运行
3.最后执行 docker-compose up 命令来启动并运行整个应用程序

Important concepts of Compose:

  • Project: A complete business unit composed of a set of associated application containers, defined in the docker-compose.yml file.
  • Service: An application container can actually include several container instances running the same image.

Two, Docker compose configuration information

2.1 Dokcer compose common commands

Common commands description
build Rebuild the service
ps List containers
up Create and start the container
exec Execute commands in the container
scale Specify the number of service containers to start
top Show container process
logs View container output
down Delete containers, networks, data volumes and mirrors
stop/start/restart Stop/start/restart service

2.2 Docker compose configuration common fields

Field description
build dockerfile context Specify the Dockerfile file name to build the image context path
image Specified mirror
command Execute the command, overwrite the default command
container name Specify the container name, because the container name is unique, if you specify a custom name, you cannot scale
deploy Specify the deployment and operation service related configuration, which can only be used in Swarm mode
environment Add environment variables
networks If the network (name space)
posts Open container port, same as -p, but the port cannot be lower than 60
volumes Mount host path or command volume (similar to data volume)
restart Restart policy, default no, always, no-failure, unless-stoped
hostname Container hostname

2.3 Compose command description

  • Basic usage format
docker-compose [options] [command] [ARGS...]
  • docker-compose command options
--verbose 输出更多调试信息
--version 打印版本并退出
-f --file FILE 使用特定的 compose 模板文件,默认为docker-compose.yml
-p --project-name NAME 指定项目名称,默认使用目录名称

Three, Dokcer orchestration experiment

3.1 Project environment

centos7 虚拟机一台  IP:192.168.140.22
docker-ce 社区版已安装
本次项目以构建Nginx为主

3.2 Project steps

3.2.1 docker-compose configuration

  • Download compose
[root@docker ~]# curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  • Set environment variables
[root@docker ~]# chmod 755 docker-compose
[root@docker ~]# cp -p docker-compose /usr/local/bin/
[root@docker ~]# docker-compose --version
docker-compose version 1.21.1, build 5a3f1a3

3.2.2 Build Nginx

  • Create a working directory, add packages
[root@docker ~]# mkdir compose_nginx
[root@docker ~]# cd compose_nginx/
[root@docker compose_nginx]# mkdir nginx
[root@docker compose_nginx]# cd nginx/
[root@docker nginx]# ls
[root@docker nginx]# rz -E
rz waiting to receive.
[root@docker nginx]# ls
nginx-1.12.2.tar.gz
  • Set up the startup script
[root@docker nginx]# vim run.sh
#启动脚本
#!/bin/bash
/usr/local/nginx/sbin/nginx
  • Write Dockerfile
[root@docker nginx]# vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image
RUN yum -y update
RUN yum -y install gcc gcc-c++ pcre-devel zlib-devel make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.12.2
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
  • Set up website page
[root@docker ~]# cd compose_nginx/
[root@docker compose_nginx]# mkdir wwwroot
[root@docker compose_nginx]# cd wwwroot/
[root@docker wwwroot]# vim index.html
<h1>This is my web!</h1>
  • Write yml file
[root@docker ~]# cd compose_nginx/
[root@docker compose_nginx]# vim docker-compose.yml
version: '3'
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 1106:80
      - 1107:443
    networks:
      - cluster
    volumes:
      - ./wwwroot:/usr/local/nginx/html
networks:
  cluster:
  • Build nginx image
先确认文件位置
[root@docker compose_nginx]# ls
docker-compose.yml  nginx  wwwroot

[root@docker compose_nginx]# ls nginx/
Dockerfile  nginx-1.12.2.tar.gz  run.sh

构建镜像并启动容器
[root@docker compose_nginx]# docker-compose -f docker-compose.yml up -d	
											'//指定以docker-compose.yml文件来启动'

[root@docker compose_nginx]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                         NAMES
68093658e0bb   compose_nginx_nginx   "/run.sh"                13 seconds ago   Up 12 seconds   0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp   compose_nginx_nginx_1

Possible errors when starting the container
Insert picture description here

解决方法:
查看yml文件中 volumes挂载目录文件是否存在;查看脚本run.sh环境是否正确

3.2.3 Access verification

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42449832/article/details/114818310