catalogue
2, YAML file format and preparation considerations
3, Common fields for Docker Compose configuration
4, Docker Compose common commands
5, Docker Compose file structure
6, Docker Compose environment installation
Installing and running nginx containers using compose orchestration
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. All files (docker-compose.yml, extensions files or environment variable files, etc.) in the docker compose running directory form a project. Unless otherwise specified, the project name is the current directory name. A project can contain multiple services. Each service defines the image, parameters and dependencies of the container. A service can include multiple container instances. Docker compose does not solve the problem of load balancing. Therefore, it is necessary to use other tools to realize service discovery and load balancing, such as consult.
- The project configuration file of docker compose is docker-compose.yml by default. You can use the environment variable compose_ The file or - f parameter defines a custom configuration file that defines multiple dependent services and the container in which each service runs.
- Using a Dockerfile template file allows users to easily define a separate application container. In work, we often encounter the situation that multiple containers need to cooperate with each other to complete a task. For example, to implement a Web project, in addition to the Web service container itself, you often need to add the back-end database service container, and even the 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 the container. Therefore, as long as the operating platform supports Docker API, composition can be used for orchestration management.
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 {}
3, Common fields for Docker Compose configuration
field | describe |
---|---|
build docker context | Specify the Dockerfile file name to build the image context path |
image | Specify mirror |
command | Execute the command to override the default command |
container name | Specify the container name. Because the container name is unique, if you specify a custom name, you cannot scale |
environment | Add environment variable |
networks | Join the network |
ports | Exposed container port, same as - p, but not less than 60 |
volumes | Mount the host path or command volume |
restart | Restart policy, default no, always, no failure, unless stopped |
hostname | Container host name |
restart: Restart 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 common commands
field | describe |
---|---|
build | Rebuild service |
ps | List containers |
up | Create and launch containers |
exec | Execute the command in the container |
scale | Specify the number of service container starts |
top | Show container processes |
logs | View container output |
down | Delete containers, networks, data volumes, and mirrors |
stop/start/restart | Stop / start / restart service |
5, 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 │?? └── run.sh #Start service script └── wwwroot └── index.html #Site page
6, Docker Compose environment installation
Docker Compose yes Docker As a stand-alone product, it needs to be installed Docker Then install separately Docker Compose #Download on Linux, we can download its binary package from GitHub to use. This command is to download the current stable version of Docker Compose ' 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
7, Use compose orchestration
Installing and running nginx containers using compose orchestration
1. Docker Compose Environment installation Docker Compose yes Docker As a stand-alone product, it needs to be installed Docker Then install separately Docker Compose #Download on Linux, we can download its binary package from GitHub to use. This command is to download the current stable version of Docker Compose ' 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 (1)Prepare dependent 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 vim Dockerfile #Base mirror FROM centos:7 #User information MAINTAINER this is nginx image #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 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 EXPOSE 80 EXPOSE 443 //Method 1: RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf #Turn off nginx to run in the background #Add run.sh from the host to the container ADD run.sh /run.sh RUN chmod 755 /run.sh CMD ["/run.sh"] //Method 2: ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ] echo "<h1>this is test web</h1>" > /opt/compose_nginx/wwwroot/index.html (2)Write configuration file docker-compose.yml vim /opt/compose_nginx/docker-compose.yml version: '3' services: nginx: hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - 1216:80 - 1217:443 networks: - lnmp volumes: - ./wwwroot:/usr/local/nginx/html networks: lnmp: cd /opt/compose_nginx/ 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 ---------------------------------------------------------------------------------------------------------- docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b48dceee248f compose_nginx_nginx "/run.sh" About a minute ago Up About a minute 0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp compose_nginx_nginx_1 cd /opt/compose_nginx/ docker-compose ps #You must execute this command in the directory where docker-compose.yml is located Browser access: http://192.168.50.40:1216