[Docker series] docker learning nine, initial experience of Compose content arrangement official website
We learned docker in the previous article, why do we need Compose? What the hell is Compose?
Docker Compose can easily and efficiently manage containers, define and run multiple containers
Let's take a look at the official introduction docs
What is Compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
talked about three points:
- Compose can define and run multiple containers
- Need to use to YAML configuration file
- A single command can create and start all services
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
Docker Compose can run in all environments
Using Compose is basically a three-step process:
1、Define your app’s environment with a Dockerfile
so it can be reproduced anywhere.
2、Define the services
that make up your app in docker-compose.yml
so they can be run together in an isolated environment.
3、Run docker compose up
and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary.
three steps:
- Need to define the Dockerfile to ensure that it can run in any environment
- Services are defined in the docker-compose.yml file, then how to write this yml file? How to define services
- Start the project with
docker-compose binary
summarizes the above official description:
Docker Compose for batch container orchestration
docker run
in a project (dozens or hundreds), is it silly to use 061ac2b40343b2 one by one? And it is also a very unfriendly thing for operation and maintenance. To optimize such problems, we have Docker Compose
Is Compose available by default in Docker?
By default, there is no Compose in Docker. Compose is the official open source project of Docker. We use Compose, which needs to be installed separately.
How to write Compose's yml file?
Let's take a look at how the yml of the official document is structured:
A docker-compose.yml
looks like this:
version: "3.9" # optional since v1.27.0
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
- services
Designated service
- volumes
Specify mount volume
Through the above description of the official document, we can know that Compose has two important concepts:
- services services are containers and related applications
- Project is a set of associated containers
Compose installation
1. Let's choose to install Docker Compose under linux, run the following instructions to install
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
After the installation is successful, there will be a docker-compose program /usr/local/bin/
2. Add executable permissions to the program docker-compose
sudo chmod +x /usr/local/bin/docker-compose
3. The installation is successful, check the version of docker-compose, and see the following information as success
# docker-compose version
docker-compose version 1.29.2, build 5becea4c
docker-py version: 5.0.0
CPython version: 3.7.10
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
Compose official case experience
Now that we have installed docker-compose, let's experience the official example together, we will use it first, and then we will study
Prepare environment and code
1. Create a compose test directory, you can execute the following commands in any directory
mkdir composetest
cd composetest
2. Write the app.py file
app.py
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
The function of the py file is to register a route of /
. When we access the server's /
, the program will read the counter of redis to confirm how many times the website has been visited
3. Create a file requirements.txt for later installation
requirements.txt
flask
redis
Create DockerFile file
Write Dockerfile file
Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
The meaning in the Dockerfile file is:
- Build an image based on python:3.7-alpine
- Set the working directory to /code
- Set the FLASK_APP environment variable
- Set the FLASK_RUN_HOST environment variable
- Run the
apk add --no-cache gcc musl-dev linux-headers
command - Copy the file
requirements.txt
to the container - Run pip to install the components in requirements.txt
- Expose 5000 ports
- Copy. to.
- Execute flask run command
Define Compose file (yml file)
docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
This compose file defines 2 services
- web service, port 5000 is exposed
- redis
Build and run our Compose
Before running the command, let's check what our compose test directory is:
start building
docker-compose up
#docker-compose up
You can see that docker-compose up
, Compose is also executed layer by layer, and we can see that compose is the first to establish a custom network
Creating network "composetest_default" with the default driver
Seeing this, we found that Compose will automatically help us create redis container and web container
Creating composetest_web_1 ... done
Creating composetest_redis_1 ... done
Finally, we saw that Compose helped us start redis and web, and the program was running normally,
Let's use the curl command on the host to request this web service
curl localhost:5000
Sure enough, the official compose experience no problem, nice
Check the mirror image
Use docker images to check the image and find that there are more composetest_web, python, redis alpine versions, which are also automatically done for us by compose, which is very convenient
Check out the network
docker network ls
When compose is built, a network will be created for us from the beginning
doubt?
A careful friend found out why our container names are composetest_web_1 and composetest_redis_1
This is a rule in Docker Compose, which makes it easy to identify the corresponding copy
For example, the container will be named like this in compose:
文件名_服务名_num
In the case of multiple server clusters, the role of this num is reflected, and num identifies the number of copies
Network rules
As long as multiple containers are in a local area network, they can ping each other, communicate with each other, and access through domain names
For example, for services in the mysql cluster, we can access mysql:3306, and compose will give us access to the service mysql:3306
We can check the custom network created by docker compose above for us
docker network ls
# docker network inspect composetest_default
...
"Containers": {
"25b5814cfded10e00d2e59a8e17fcba670232bce135fdabd558b7c0530d011a4": {
"Name": "composetest_web_1",
"EndpointID": "cb131464ea9112403f851b14a37fa5c67b023f2ce28a1e85c409e3f284f78db4",
"MacAddress": "02:42:ac:13:00:03",
"IPv4Address": "172.19.0.3/16",
"IPv6Address": ""
},
"e7fedce77d3759fefde5bef84c759a5c59e033a6f48850e5930825bfc8a8444c": {
"Name": "composetest_redis_1",
"EndpointID": "3af891f7d52cba7ec75eb01533af1d5dae4dcd0d8bf4c55e6b342075f971be22",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16",
"IPv6Address": ""
}
},
...
I found that in the above example, the web service and redis service are under the same network, and all can communicate with each other
Stop compose
We can use ctrl + c to stop compose
You can also stop compose docker-compose down
Stop compose, then all services involved in compose will be stopped
# docker-compose down
Stopping composetest_web_1 ... done
Stopping composetest_redis_1 ... done
Removing composetest_web_1 ... done
Removing composetest_redis_1 ... done
Removing network composetest_default
- Stop composetest_web_1
- Stop composetest_redis_1
- Delete composetest_web_1
- Delete composetest_redis_1
- Remove the custom network composetest_default
summary
- We use the docker image, through the docker run command, you can create and start the container
- DockerFile can build a mirror, that is, package the service
- docker-compose can start projects, orchestrate multiple microservices, and deploy one-click in the environment
- Docker network, custom network
Reference materials:
Welcome to like, follow, favorite
Friends, your support and encouragement are my motivation to keep sharing and improve quality
Okay, that's it for this time
Technology is open, and our mindset should be more open. Embrace the change, live toward the sun, and work hard to move forward.
I am Nezha , welcome to like and follow the collection, see you next time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。