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:
- tab indentation is not supported. Space indentation is required
- Usually the beginning is indented by 2 spaces
- Indent 1 space after characters, such as colon:, comma,, horizontal bar-
- Note with # number
- If it contains special characters, enclose them with single quotation marks' '
- Boolean values must be enclosed in quotation marks' '
3, Common fields for Docker Compose configuration
Common field | effect |
---|---|
build | Specify the Dockerfile file name (to specify the Dockerfile file, you need to use the Dockerfile tag in the child tag of the build tag) |
dockerfile | Build mirror context path |
context | It can be the path to the dockerfile or the url to the git repository |
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 |
deploy | Specify the configuration related to deploying and running services, which can only be used in Swarm mode |
environment | Add environment variable |
networks | Join the network |
ports | Expose the container port, the same as - p, but the port cannot be lower than 60 |
volumes | Mount the host path or command volume |
hostname | Container host name |
restart | Restart policy, default no, always, no failure, unless stopped |
Note:
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 notes -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
field | effect |
---|---|
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, Deploy compose
1.Docker Compose environment installation
#The first method is to download directly curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`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/ ls mkdir wwwroot ls 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 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. In the host SH into container ADD run.sh /run.sh RUN chmod 755 /run.sh CMD ["/run.sh"] To create vim run.sh #!/bin/bash /usr/local/nginx/sbin/nginx //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' 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
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: http://192.168.19.77:1216