Directory
1. Theory
1.Docker-Compose
2. Experiment
1. Docker Compose installation and deployment
2.Docker Compose composes nginx image
3.Docker Compose composes tomcat image
3. Questions
1. The difference between Docker Compose and Dockerfile
4. Summary
1. Theory
1.Docker-Compose
(1) Usage scenarios
A Dockerfile template file can be used to define a single application container. If multiple containers need to be defined, service orchestration is required. There are many technical solutions for service orchestration. Today I will introduce Docker Compose, the official product of Docker.
docker swarm (manage cross-nodes)
Dockerfile allows users to manage a single application container; while Compose allows users to define a group of associated application containers (called a project) in a template (YAML format), such as a Web service container plus The database service container on the back end, etc.
(2) Introduction
The Docker-Compose project is Docker’s official open source project, responsible for realizing the rapid orchestration of Docker container clusters.
Docker-Compose divides the managed containers into three layers, namely project (project), service (service) and container (container). All files in the Docker-Compose running directory (docker-compose.yml, extends files or environment variable files, etc.) form a project. If there is no special specification, the project name is the current directory name. A project can contain multiple services, and each service defines the image, parameters, and dependencies for container running. A service can include multiple container instances. Docker-Compose does not solve the problem of load balancing, so other tools are needed to achieve service discovery and load balancing, such as Consul.
The default project configuration file of Docker-Compose is docker-compose.yml. The configuration file can be customized through the environment variable COMPOSE_FILE or the -f parameter, which defines multiple dependent services and the containers in which each service runs.
Using a Dockerfile template file allows users to easily define a separate application container. At work, we often encounter situations where multiple containers need to cooperate with each other to complete a certain task. For example, to implement a Web project, in addition to the Web service container itself, it is often necessary to add a back-end database service container and even a load balancing container.
Compose allows users to define a set of associated application containers as a project through a separate docker-compose.yml template file (YAML format).
The Docker-Compose project is written in Python and calls the API provided by the Docker service to manage containers. Therefore, as long as the platform you are operating on supports the Docker API, you can use Compose for orchestration management.
(3) Deployment
#Note that it must be based on installing 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 #Download the installation package and install DockerCompose separately chmod +x /usr/local/bin/docker-compose #docker-composeAdd execution permissions docker-compose --version #View docker-compose version
(4) Things to note when writing YML files
YML is a markup language that can intuitively display the data serialization format. It is highly readable and similar to XML data. The data structure is represented by indentation, consecutive items are represented by minus signs, key-value pairs are separated by colons, and arrays are represented by centers. Enclosed in parentheses, hash is enclosed in curly brackets {}
Precautions:
①Case clear
② Indicate hierarchical relationships through indentation
③ does not support tabs for indentation, only spaces can be used for indentation
④The number of indented spaces is not important, as long as the same level is aligned left and right, usually 2 spaces are indented at the beginning
⑤# comment
⑥Symbol characters are indented with 1 space, such as colon: comma, horizontal bar – followed by a space
⑦If it contains special characters, it will be treated as a string if it is surrounded by single quotes and double quotes. Single quotes do not recognize variables, and double quotes recognize variables.
#yml format languages: #Sequence mapping -Java - Golang -Python websites: #mapped mapping cpu: 2 memory: 1024M swap: 2048M disk: 60G
Data structure: ObjectMap: a dictionary of key-value pairs animal: pets sequence array: a set of lists in order -Cat - Dog - Goldfish ["Cat", "Dog", "Goldfish"] Boolean value debug: true debug: false Example: #yaml format languages: #Sequence mapping -Java - Golang -Python websites: #mapped mapping cpu: 2 memory: 1024M swap: 2048M disk: 60G Key: {value} #Json format { languages: [ 'Java', 'Golang', 'Python' ], resources: { cpu: '2', memory: '1024M', swap: '2048M', disk: '60G' }, }
(5) Compose configures common fields
Table 1 Compose configuration common fields
field | Description |
build | Specify the Dockerfile file name, to specify the Dockerfile file needs to be in the build Use the dockerfile tag in the child tag of the tag to specify |
dockerfile | Build the image context path dockerfile-nginx |
context | can be the path of the dockerfile, or the url address pointing to the git warehouse |
image | specified image |
command | Execute the command, override the command executed by default after the container starts |
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 | Specify Configuration related to deploying and running services 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, such as host, bridge ,… |
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. To name the volume, define the volume name in top-level volumes |
volumes_from | Mount a volume from another service or container, optional parameters: ro and :rw, only version ‘2’ supports |
hostname | Container host name |
sysctls | Set kernel parameters in the container |
links | Connect to another container, – service name[:service alias] |
privileged | is used to give root permissions to the container , note that it is unsafe, true | false |
restart | Set the restart policy, no, always, no-failure, unless-stoped, no, The default policy is not to restart the container when it exits. on-failure, the container will only be restarted when the container exits abnormally (exit status is non-0). on-failure:3, restart the container when the container exits abnormally, up to 3 times. always, always restart the container when the container exits. unless-stopped, always restart the container when it exits, but does not consider containers that have been stopped when the Docker daemon is started. |
depends_on |
When using Compose, the biggest advantage is that there are fewer startup commands. However, the order in which project containers are started is generally required. If you start the container directly from top to bottom, the startup may fail due to container dependency issues. For example, if you start the application container without starting the database container, the application container will exit because it cannot find the database. The depends_on tag is used to solve container dependencies and startup issues. For example: php: |
(6) Docker Compose common commands
Table 2 Docker Compose common commands
Command | Description |
build | Rebuild service |
ps | List containers |
up | Create and start container |
exec | Execute commands in the container |
scale | Specify the number of service containers to start |
top | Display container process |
logs | View container output |
down | Delete containers, networks, data volumes and images |
stop/start/restart | Stop/start/restart service |
(7) Docker Compose file structure
yum install -y tree tree /opt/compose_nginx /opt/compose_nginx/ ├── docker-compose.yml #Create template script ├── nginx │ ├── Dockerfile #Create container script │ ├── nginx-1.12.0.tar.gz #Copy source package └── wwwroot └── index.html #site web page
(8) Docker Compose writes nginx image
(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 ./ vim run.sh #!/bin/bash /usr/local/nginx/sbin/nginx vimDockerfile #Based on base image FROM centos:7 #User Info MAINTAINER this is nginx image <nginx> #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 the 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 //method one: RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf #Close nginx running in the background #Add run.sh in the host to the container ADD run.sh /run.sh RUN chmod 755 /run.sh CMD ["/run.sh"] //Method Two: ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ] echo "<h1>this is test web</h1>" > /opt/compose_nginx/wwwroot/index.html (2) Write the configuration file docker-compose.yml vim /opt/compose_nginx/docker-compose.yml version: '3' services: nginx: container_name: web1 hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - 1216:80 - 1217:443 networks: lnmp: ipv4_address: 172.18.0.10 volumes: - ./wwwroot:/usr/local/nginx/html networks: lnmp: driver: bridge ipam: config: - subnet: 172.18.0.0/16 cd /opt/compose_nginx/ docker-compose -f docker-compose.yml up -d cd /opt/compose_nginx/ docker-compose ps #This command must be executed in the directory where docker-compose.yml is located CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8296e92e2aef compose_nginx_nginx "/run.sh" 4 minutes ago Up 4 minutes 0.0.0.0:1216->80/tcp, :::1216->80/tcp, 0.0.0.0:1217->443/tcp, :: :1217->443/tcp web1 Browser access: http://192.168.204.141:1216
(9) Docker Compose composes tomcat image
①First create a docker_compose folder of tomcat mkdir -p /opt/compose_tomcat/tomcat ②Put the jdk package and tomcat package compiled and installed by tomcat into the tomcat folder ③Enter the tomcat folder to write the Dockerfile cd /opt/compose_tomcat/tomcat vimDockerfile #The content of the file is as follows FROM centos:7 #Based on centos:7 image ADD jdk-8u201-linux-x64.tar.gz /usr/local ADD apache-tomcat-9.0.16.tar.gz /usr/local RUN mv /usr/local/jdk1.8.0_201 /usr/local/java & amp; & amp; mv /usr/local/apache-tomcat-9.0.16 /usr/local/tomcat #Copy the jdk and tomcat compressed packages to the /usr/local folder of the image and rename them ENV JAVA_HOME /usr/local/java ENV JAVA_BIN /usr/local/java/bin ENV JRE_HOME /usr/local/java/jre ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar ENV PATH $JAVA_BIN:/usr/local/java/jre/bin:$PATH #define path variables RUN mkdir /usr/local/tomcat/webapps/lucien \ & amp; & amp; echo -e "<%@ page language="java" import="java.util.*" pageEncoding="UTF-8 "%>\\ <html>\\ <head>\\ <title>JSP test1 page</title>\\ </head>\\ <body>\\ <% out .println("123456");%>\\ </body>\\ </html>" > /usr/local/tomcat/webapps/lucien/index.jsp \ & amp; & amp; sed -i '71a <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware ="false">' /usr/local/tomcat/conf/server.xml \ & amp; & amp; sed -i '72a <Context docBase="/usr/local/tomcat/webapps/lucien" path="" reloadable="true">' /usr/local /tomcat/conf/server.xml \ & amp; & amp; sed -i '73a </Context>' /usr/local/tomcat/conf/server.xml \ & amp; & amp; sed -i '74a </Host>' /usr/local/tomcat/conf/server.xml #First create the tomcat homepage storage path, then add the homepage content, and modify the environment variables to take effect EXPOSE 8080 #define port ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"] #start tomcat ④Create the yml file of tomcat under the compose_tomcat path cd /opt/compose_tomcat vim docker-compose.yml #The content is as follows: version: '3' #docker-compose version services: #define servers tomcat: #Container name tomcat container_name: web2 hostname: tomcat-test #hostname tomcat-test build: context: ./tomcat dockerfile: Dockerfile #Mirror building dockerfile folder location and name ports: - 1280:8080 #Port Mapping networks: lnmp: ipv4_address: 172.19.0.100 #tomcatipdefinition networks: lnmp: driver: bridge ipam: config: - subnet: 172.19.0.0/16 #Customize network mode and network segment, save and exit ⑤Create and start the tomcat container, pay attention to execute it under the /opt/compose_tomcat path docker-compose -f docker-compose.yml up -d #Create background start tomcat container docker ps -a #View container status and port mapping #Access port 1280 of this machine to see if it is successful Browser access: http://192.168.204.141:1280
2. Experiment
1. Docker Compose installation and deployment
(1) Download the installation package and install DockerCompose separately
(2) docker-compose adds execution permission
(3) View docker-compose version
2.Docker Compose writes nginx image
(1) Prepare dependent files
(2) Write the configuration file docker-compose.yml
(3) Docker Compose file structure
(4) Create and start nginx container
(5) Browser access
3.Docker Compose writes tomcat image
(1) Prepare dependency files
①First create a tomcat docker_compose folder
②Put the jdk package and tomcat package compiled and installed by tomcat into the tomcat folder
③Enter the tomcat folder and write the Dockerfile file
④Create the yml file of tomcat under the compose_tomcat path
⑤Create and start the tomcat container, pay attention to execute
Create a background to start the tomcat container
Browser access
3. Question
1.The difference between Docker Compose and Dockerfile
(1) Difference
Both Docker Compose and Dockerfile are tools for building and managing Docker containers, but they have different roles and usage. A Dockerfile is a text file that defines the rules for building a Docker image. It contains a series of instructions for specifying the operating system, software environment, applications, file copies and other information when the container is running. Through the Dockerfile, a custom Docker image can be built so that the image can run on any host that supports Docker. Docker Compose is a tool for defining and running multiple Docker containers. With Docker Compose, you can use YAML files to describe the application's services, network, storage and other resources, and use a single command to start, stop, restart and manage the entire application. In a Docker Compose configuration file, multiple Docker containers can be defined, each container can use a customized Docker image, and the dependencies and communication methods between containers can be specified. Therefore, Dockerfile is more used for creating and customizing Docker images, while Docker Compose is more used for managing multiple Docker containers so that they can work together to build a complete application.
4. Summary
The sequence gets the value by the index,The map gets the value by the key.
A sequence type is an ordered collection of elements, while a map type is an unordered collection of key-value pairs.
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 docker15001 people are learning the system