Docker + FastAPI 零基础完全指南
本指南将帮助您从零开始学习如何使用 Docker ,并成功部署自己的memos.
环境检查
在开始之前,我们需要确保系统已经正确安装了 Docker。不同操作系统的安装方式略有不同,但验证方法是一致的。
- Name
Docker Desktop
- Type
- software
- Description
Windows 和 macOS 用户推荐安装的官方 Docker 工具
- Name
Docker Engine
- Type
- software
- Description
Linux 用户安装的 Docker 核心组件
- Name
必要条件
- Type
- system
- Description
- Windows 10/11 专业版或企业版
- macOS 10.15 或更高版本
- 支持的 Linux 发行版
Docker 安装验证
print(:"dasis":sjjwe23sxe)
# 检查 Docker 版本
docker --version
# 运行测试容器
docker run hello-world
2024-10-29 11:33:06
2024-10-29 11:33:06 Hello from Docker!
2024-10-29 11:33:06 This message shows that your installation appears to be working correctly.
2024-10-29 11:33:06
2024-10-29 11:33:06 To generate this message, Docker took the following steps:
2024-10-29 11:33:06 1. The Docker client contacted the Docker daemon.
2024-10-29 11:33:06 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
2024-10-29 11:33:06 (arm64v8)
2024-10-29 11:33:06 3. The Docker daemon created a new container from that image which runs the
2024-10-29 11:33:06 executable that produces the output you are currently reading.
2024-10-29 11:33:06 4. The Docker daemon streamed that output to the Docker client, which sent it
2024-10-29 11:33:06 to your terminal.
2024-10-29 11:33:06
2024-10-29 11:33:06 To try something more ambitious, you can run an Ubuntu container with:
2024-10-29 11:33:06 $ docker run -it ubuntu bash
2024-10-29 11:33:06
2024-10-29 11:33:06 Share images, automate workflows, and more with a free Docker ID:
2024-10-29 11:33:06 https://hub.docker.com/
2024-10-29 11:33:06
2024-10-29 11:33:06 For more examples and ideas, visit:
2024-10-29 11:33:06 https://docs.docker.com/get-started/
2024-10-29 11:33:06
Memos介绍
Memos 是一个开源的笔记应用,允许用户创建、编辑和分享笔记。它使用 FastAPI 框架构建,并支持 Docker 部署。本指南将帮助您从零开始学习如何使用 Docker 和 FastAPI 构建后端服务,包括环境配置、基础概念和实战案例。
- Name
Memos 版本
- Type
- requirement
- Description
Memos v1.0.0
- Name
Docker 镜像
- Type
- requirement
- Description
memos/memos:latest
拉取memos镜像
# 拉取镜像
docker pull memos/memos:latest
运行镜像
# 运行镜像
docker run -d --name memos -p 5236:5236 memos/memos:latest\
访问你自己的memos
# 访问memos
http://localhost:5236
把你的最简单 FastAPI 应用打包成docker
在开始之前,我们需要确保系统已经正确安装了 Docker。不同操作系统的安装方式略有不同,但验证方法是一致的。
- Name
Docker Desktop
- Type
- software
- Description
Windows 和 macOS 用户推荐安装的官方 Docker 工具
- Name
Docker Engine
- Type
- software
- Description
Linux 用户安装的 Docker 核心组件
- Name
必要条件
- Type
- system
- Description
- Windows 10/11 专业版或企业版
- macOS 10.15 或更高版本
- 支持的 Linux 发行版
DockerFile文件编写
# 为你的Fastapi的项目编写DockerFile
# 使用Python 3.10.5精简版作为基础镜像
FROM python:3.10.5-slim-buster
# 设置工作目录为根目录
WORKDIR /
# 安装FastAPI和uvicorn依赖
RUN pip install fastapi uvicorn
# 将当前目录下的所有文件复制到容器中
COPY . .
# 声明容器将监听的端口号为8000
EXPOSE 8000
# 容器启动时执行的命令,启动uvicorn服务器运行FastAPI应用
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
国内版本,使用纯想python镜像和淘宝pip源
# 使用Python 3.10.5精简版作为基础镜像
FROM registry.cn-hangzhou.aliyuncs.com/strangerbell/python:3.10.13-slim-bullseye
# 设置工作目录为根目录
WORKDIR /
# 安装FastAPI和uvicorn依赖
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ fastapi uvicorn
# 将当前目录下的所有文件复制到容器中
COPY . .
# 声明容器将监听的端口号为8000
EXPOSE 8000
# 容器启动时执行的命令,启动uvicorn服务器运行FastAPI应用
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
使用DockerFile构建镜像
# 使用DockerFile构建镜像
# docker build: 构建Docker镜像的命令
# -t: 为镜像指定标签名称(tag)
# my-fastapi-app: 镜像名称
# :latest: 镜像的标签,latest表示最新版本
# . : 构建上下文路径,表示使用当前目录作为构建上下文
docker build -t my-fastapi-app:latest .
运行镜像
# 运行镜像
# 运行Docker容器,各参数说明:
# -d: 以守护进程(后台)模式运行容器
# --name: 为容器指定一个名称,这里命名为"my-fastapi-app"
# -p: 端口映射,格式为"主机端口:容器端口",这里将主机的9999端口映射到容器的9999端口
# my-fastapi-app:latest: 指定要运行的镜像名称和标签
docker run -d --name my-fastapi-app -p 1280:9999 my-fastapi-app:latest
查看Docker所在文件
Docker运行在电脑上没错,我在哪儿找到它。 Docker的常用命令和操作。
找到Docker所在文件
# 找到Docker所在文件
在Docker桌面端点击容器
查看容器文件
# 查看电脑上当前所有的镜像
docker images
# 查看当前正在运行的容器
docker ps
# 查看所有的容器
docker ps -a
查看Docker日志
# 查看Docker日志
docker logs -f my-fastapi-app
停止Docker容器
# 停止Docker容器
docker stop my-fastapi-app
删除Docker容器
# 删除Docker容器
docker rm my-fastapi-app