1. Docker Compose Theory
1.1. Origin of Compose Tools
The Compose project is the official open source project for Docker, responsible for the rapid organization of Docker container clusters.
Using the Dockerfile file file described earlier, it is easy to define a separate application container.However, in daily development work, it is common to encounter situations where multiple containers are needed to work together to complete a task.
For example, to implement a Web project, in addition to the Web service container itself, there is often a need to add a back-end database service container; for example, distributed applications typically contain several services, each of which typically deploys multiple instances.
If each service needs to be started and stopped manually, then the inefficiency and the amount of maintenance can be imagined.A tool is needed to manage a set of related application containers, called Docker Compose.
Compose has two important concepts:
Project: A complete business unit consisting of a set of associated application containers, defined in the docker-compose.yml file.
Service: An application container that can actually include several container instances running the same mirror.
Common fields for 1.2, docker-compose.yml files
1.3, Common Docker Compose commands
Basic usage format
docker-compose [options] [COMMAND] [ARG...] //docker-compose option: --verbose Output more debugging information --version Print version and exit -f, -file FILE Use specific compose Template file, default is docker-compose.yml -p, --project-name NAME Specify the project name, using the directory name by default //Give an example: docker-compose -f docker-compose.yml up -d
2. Install Docker Compose Tools
2.1. Install docker environment
The docker deployment is not described here, you can see my previous blog if you need to
2.2, Download the docker-compose installation package
Loading official sources will be slower
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
2.3. Deploying docker-compose
//You can use the compose command in any directory cp -p docker-compose /usr/local/bin [root@localhost docker]# cp -p docker-compose /usr/local/bin/ [root@localhost docker]# docker-compose -v docker-compose version 1.21.1, build 5a3f1a3
// Create a total directory (mirror nginx) mkdir /root/compose_nginx cd /root/compose_nginx // Create a nginx mkdir nginx //Create Dockerfile for nginx in the nginx directory (compressed package, startup script, Dockerfile script) // Create docker-compose.yml cd /root/compose_nginx vim /root/compose_nginx/docker-compose.yml version: '3' // Version declaration is'3' services: // Do service nginx: // Do nginx services hostname: nginx // The host name of the container: nginx build: // Start creating (directly related to Dockerfile) context: ./nginx // Warehouse location for specific text dockerfile: Dockerfile // dockerfile file ports: // Provided Port - 1216:80 // Outer Port - 1217:443 // Internal Port networks: // Network name, if a cluster is established this is the cluster name - abc // Name (Name must match if you want to be on the same network as another container) volumes: // Data Volume - ./wwwroot:/usr/local/nginx/html //Site location bound to container networks: // The name of the cluster under the declaration abc: // The name of the cluster // Create wwwroot site directory cd /root/compose_nginx mkdir wwwroot cd wwwroot/ vi index.html <h1>this is nginx </h1>
docker-compose -f docker-compose.yml up -d // -f Points to a file
View Mirror and Container Display created successfully and we can access the service.