Table of contents
3. Use case: Deploy a simple fastapi service
( The following tutorial is based on the environment that has installed the docker service )
1. What is docker compose?
Compose is a technology that combines and deploys multiple docker containers. It can start and suspend all containers with one click by writing yaml configuration files, instead of using a series of docker run commands to start multiple containers.
2. Install docker compose
yum install docker-compose-plugin
docker compose version
3. Use case: Deploy a simple fastapi service
1. Initialize the FastAPI service
FastAPI (1) Create a project_fastapi project creation_Sky Leap Blog-CSDN Blog
2. Get the requirements.txt file
3. Create a new Dockerfile
# 设置基础镜像
FROM python:3.10.8
# 设置工作目录
WORKDIR /app
# 拷贝依赖文件到容器中
COPY requirements.txt .
# 安装依赖
RUN pip install -U --no-cache-dir pip && pip install --no-cache-dir -r requirements.txt
# 当前目录中所有文件复制到容器中
COPY . .
# 暴露8000端口
EXPOSE 8000
# 执行启动命令,main.py文件下的app对象
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
4. Create a new docker-compose.yml file
# compose的版本(与python无关)
version: "3.9"
services:
app:
# 构建当前的目录下的Dockerfile文件
build: .
# 映射端口8000
ports:
- "8000:8000"
5. Find a location in the linux server, create a new folder fastapi-docker-compose, and put the files in this folder.
6. Start the service, linux black window cd to the fastapi-docker-compose folder
docker-compose up -d