Fast orchestration of docker compose container cluster

Posted by rigy73 on Sun, 02 Jan 2022 14:47:42 +0100

1, Docker compose overview

Docker compose project is the official open source project of docker, which is responsible for the rapid arrangement of docker container clusters.

Docker compose divides the managed containers into three layers: project, service and container.

2, YAML file format and preparation considerations

YAML is a markup language, which can intuitively display the data serialization format with high readability. Similar to XML data description language, the syntax is much simpler than XML. YAML data structure is represented by indentation, continuous items are represented by minus sign, key value pairs are separated by colon, arrays are enclosed by brackets [] and hash is enclosed by curly brackets {}.

When using YAML, you should pay attention to the following:

  1. tab indentation is not supported. Space indentation is required
  2. Usually the beginning is indented by 2 spaces
  3. Indent 1 space after characters, such as colon:, comma,, horizontal bar-
  4. Note with # number
  5. If it contains special characters, enclose them with single quotation marks' '
  6. Boolean values must be enclosed in quotation marks' '

3, Common fields for Docker Compose configuration

Common fieldeffect
buildSpecify the Dockerfile file name (to specify the Dockerfile file, you need to use the Dockerfile tag in the child tag of the build tag)
dockerfileBuild mirror context path
contextIt can be the path to the dockerfile or the url to the git repository
imageSpecify mirror
commandExecute the command to override the default command
container nameSpecify the container name. Because the container name is unique, if you specify a custom name, you cannot scale
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 lower than 60
volumesMount the host path or command volume
hostnameContainer host name
restartRestart policy, default no, always, no failure, unless stopped

no, the default policy is not to restart the container when it exits.
On failure: when the container exits abnormally (the exit status is not 0), the container will be restarted.
On failure: 3. Restart the container when the container exits abnormally, up to 3 times.
Always, always restart the container when it exits.
The container is always restarted when the container exits, but the container that has been stopped when the Docker daemon starts is not considered

4, Docker Compose command

docker-compose -f docker-compose.yml up -d

-f, --file FILE : Use specific compose Template file, default to docker-compose.yml
-p, --project-name NAME : Specify the project name, and the directory name is used by default
-d : Run in the background

Common commands

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

5, Docker Compose file structure

yum install -y tree
tree /opt/compose_nginx
├── docker-compose.yml				#Create template script
├── nginx
│?? ├── Dockerfile					#Create container script
│?? ├── nginx-1.12.0.tar.gz			#Copy source package
│?? └──						#Start service script
└── wwwroot
    └── index.html					#Site page

6, Deploy compose

1.Docker Compose environment installation

#The first method is to download directly
curl -L`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

#The second method has an installation package
cd /opt
 Upload compressed package
chmod +x docker-compose
mv docker-compose /usr/local/bin

#View version
docker-compose --version

2. Prepare dependent documents

mkdir -p /opt/compose_nginx/nginx
cd /opt/compose_nginx/
mkdir wwwroot
echo "this is test web" > /opt/compose_nginx/wwwroot/index.html

3. Create dockerfile file

vim Dockerfile
#Base mirror
FROM centos:7
#User information
MAINTAINER this is nginx image <zz>
#Add environment package
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#Upload the nginx software package and unzip it
ADD nginx-1.12.0.tar.gz /usr/local/src/
#assign work directory
WORKDIR /usr/local/src/nginx-1.12.0
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
#Specify http and https ports

//Method 1:
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf			#Turn off nginx to run in the background
#Add run. In the host SH into container
RUN chmod 755 /
CMD ["/"]

To create

//Method 2:
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]

4. Write the configuration file docker compose yml

vim /opt/compose_nginx/docker-compose.yml
version: '3'
    hostname: nginx
      context: ./nginx
      dockerfile: Dockerfile
      - 1216:80
      - 1217:443
      - lnmp
      - ./wwwroot:/usr/local/nginx/html

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

docker ps -a

cd /opt/compose_nginx/
docker-compose ps				#Must be in docker compose Execute this command in the directory where YML is located

5. Verification

Browser access:

Topics: Docker