basic grammatical structure
version: "3.8"
services: # 容器
servicename: # 服务名字,这个名字也是内部 bridge网络可以使用的 DNS name
image: # 镜像的名字
command: # 可选,如果设置,则会覆盖默认镜像里的 CMD命令
environment: # 可选,相当于 docker run里的 --env
volumes: # 可选,相当于docker run里的 -v
networks: # 可选,相当于 docker run里的 --network
ports: # 可选,相当于 docker run里的 -p
servicename2:
volumes: # 可选,相当于 docker volume create
networks: # 可选,相当于 docker network create
Take Python Flask + Redis exercise: as an example, transform it into a docker-compose file
Create a new flask-redis folder with app.py, Dockerfile
#app.py文件
from flask import Flask
from redis import Redis
import os
import socket
app = Flask(__name__)
redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379)
@app.route('/')
def hello():
redis.incr('hits')
return f"Hello Container World! I have been seen {redis.get('hits').decode('utf-8')} times and my hostname is {socket.gethostname()}.\n"
#Dockerfile文件
FROM python:3.9.5-slim
RUN pip install flask redis && \
groupadd -r flask && useradd -r -g flask flask && \
mkdir /src && \
chown -R flask:flask /src
USER flask
COPY app.py /src/app.py
WORKDIR /src
ENV FLASK_APP=app.py REDIS_HOST=redis
EXPOSE 5000
CMD ["flask", "run", "-h", "0.0.0.0"]
1. Pull the redis image
docker image pull redis
2. Pull the python image
docker pull python:3.9.5-slim
3. Enter the current folder of flask-redis and execute
docker image build -t flask-redis .
4. Enter docker image ls, there are 3 images
5. Create a network
docker network create -d bridge demo-network
6. Create the container
docker container run -d --name redis-server --network demo-network redis
docker container run -d --network demo-network --name flask-demo --env REDIS_HOST=redis-server -p 5000:5000 flask-demo
After you are familiar with this chapter, you will first build two images of flask-redis and redis, and then run two containers. But the content of the docker-compose.yml file is not used.
Create a new docker-compose.yml file
#docker-compose.yml文件
version: "3.8"
services:
flask-redis:
container_name: flask-redis
image: flask-redis:latest
environment:
- REDIS_HOST=redis-server
networks:
- demo-network
ports:
- 8080:5000
redis-server:
container_name: redis-latest
image: redis:latest
networks:
- demo-network
networks:
demo-network:
Then execute the docker-compose up -d command to run two containers at the same time, that is, to complete the image building and container operation directly through the docker-compose command, which is in place in one step.
6. Use the docker container rm xxx xxx command to delete the two containers created before, and keep the previous image file
7. Execute docker-compose
docker-compose up
This command is to create two containers and start them normally after you have the flask-redis image
Press ctrl+c to exit and stop.
Enter to view the container, and you can see that two containers whose status is exited are automatically created for you.
docker container ls -a
Use the command -d to run in the background instead
docker-compose up -d
But I bought this Ali server, but it cannot be opened normally through the ip address + port number.
Later, I found out that the Alibaba Cloud server I purchased was caused by the port not being opened.
You can also see it by running the docker-compose ps command
docker-compose ps
The main thing is that the docker-compose ps command must be run in the current directory.
Execute docker-compose stop to stop running
docker-compose stop
Ability to view stopped containers
Execute delete, all will be deleted
docker-compose rm
You also need to create the network just now, docker-compose will not delete it for you, you have to manually delete it yourself.
For learning reference only.
The above are based on the premise of building a flask-redis image in advance, to docker-compose
What if we don't have this mirror image? Next, by modifying the docker-compose.yml file, the flask-redis image is automatically built, and two containers are created and run normally.
For example, I have a flask-py folder, a flask folder, and docker-compose.yml. These two are at the same level.
version: "3.8"
services:
#flask是容器名称
flask:
#通过build构建镜像
build:
context: ./flask #构建的文件夹./flask context意思是要的build的目录
dockerfile: Dockerfile # Dockerfile的名字
image: flask:latest #镜像的名称
environment:
- REDIS_HOST=redis-server
networks:
- demo-network
ports:
- 8080:5000
redis-server:
container_name: redis-latest
image: redis:latest
networks:
- demo-network
networks:
demo-network:
Then delete the flask-redis image created before, docker image rm xxx(id)
Then upload the flask-py folder, and then cd into it
Excuting an order
docker-compose build
But he only built a mirror for me here, without the two containers and networks that were automatically created.
That means docker-compose build failed?
Then it is impossible to build directly.
Delete the built image first. Execute the command pull to pull the image of flask-redis, which will automatically build the image.
docker-compose pull
Then up -d starts in the background,
docker-compose up -d
Finally docker-compose ps
docker image ls
docker container ls -a
docker network ls
can see the data.
Finally execute delete all just created
docker system prune -f
reference