Abstract
Docker Compose is a tool for defining and running multiple Docker container applications. With Compose you can configure your application service using a YAML file, and with a single command, you can deploy all of your configured services.
The installation
Download the Docker Compose:
The curl -l https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose- ` ` uname - s - ` uname -m ` > /usr/local/bin/docker-composeCopy the code
Change the permissions of this file to executable:
chmod +x /usr/local/bin/docker-compose
Copy the code
Check whether the installation is successful:
docker-compose --version
Copy the code
Steps for using Docker Compose
- Dockerfile is used to define the application environment, which is generally required to modify the initial mirroring behavior.
- Use docker-comematery.yml to define the application services that need to be deployed to perform a one-time deployment of the script;
- Use the docker-compose up command to deploy all application services at once.
Docker-composing. Yml common command
image
Specifies the name of the image to run
# run mysql5.7 mirror
image: Mysql: 5.7
Copy the code
container_name
Configuring the Container Name
# mysql.mysql
container_name: mysql
Copy the code
ports
Specify the port mapping between the HOST and the CONTAINER (HOST:CONTAINER)
Map host port 3306 to container port 3306
ports:
- 3306: 3306
Copy the code
volumes
Mount files or directories on the HOST machine to a CONTAINER (HOST:CONTAINER)
# mount external files to myQL container
volumes:
- /mydata/mysql/log:/var/log/mysql
- /mydata/mysql/data:/var/lib/mysql
- /mydata/mysql/conf:/etc/mysql
Copy the code
environment
Configuring environment Variables
# set mysqlroot password environment variables
environment:
- MYSQL_ROOT_PASSWORD=root
Copy the code
links
Connect services to other containers (SERVICE:ALIAS)
You can use database as the domain name to access the database
links:
- db:database
Copy the code
Docker Compose common command
Build, create, and start related containers:
# -dIndicates running in the background
docker-compose up -d
Copy the code
Stop all related containers:
docker-compose stop
Copy the code
List all container information:
docker-compose ps
Copy the code
Deploy the application using Docker Compose
Write the docker-comemess. yml file
Docker Compose manages containers in three tiers: engineering, service, and container. All the services defined in docker-comemage. yml constitute a project. The services node is the service, and the container is the service. For example, in the mall tiny-docker-compose service, JDBC :mysql://db:3306 can be used to access the mysql service db.
version: '3'
services:
Specify the service name
db:
# specify the mirror used by the service
image: Mysql: 5.7
# specify the container name
container_name: mysql
# specify the port on which the service runs
ports:
- 3306: 3306
# specify the files to be mounted in the container
volumes:
- /mydata/mysql/log:/var/log/mysql
- /mydata/mysql/data:/var/lib/mysql
- /mydata/mysql/conf:/etc/mysql
Specify the container's environment variables
environment:
- MYSQL_ROOT_PASSWORD=root
Specify the service name
mall-tiny-docker-compose:
# specify the mirror used by the service
image: Mall - tiny/mall - tiny - docker - compose: 0.0.1 - the SNAPSHOT
# specify the container name
container_name: mall-tiny-docker-compose
# specify the port on which the service runs
ports:
- 8080: 8080
# specify the files to be mounted in the container
volumes:
- /etc/localtime:/etc/localtime
- /mydata/app/mall-tiny-docker-compose/logs:/var/logs
Copy the code
Note: If the mall tiny-docker-compose service cannot connect to mysql, you need to set up the mall database in mysql and import the mall. SQL script. Specific referenceBuild a Docker image for the SpringBoot application using a DockerfileRun the mysql service and set up section in.
Construct mall-tiny-Docker-compose image using maven plugin
Note: Build problems can be referred toBuild a Docker image for the SpringBoot application using the Maven plugin
Run the Docker Compose command to start all the services
Docker-comemess. yml upload docker-comemess. yml to the Linux server and run the following command in the current directory:
docker-compose up -d
Copy the code
Access interface document address http://192.168.3.101:8080/swagger-ui.html:
Project source code address
Github.com/macrozheng/…
The public,
Mall project full set of learning tutorials serialized, attention to the public number the first time access.