[docker]封装python的docker镜像

简介: [docker]封装python的docker镜像

前言

基于alpine的python镜像封装。

docker pull python:3.10-alpine

准备

  • requirements.txt内容:
fastapi
uvicorn
  • server.py内容
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
def read_root():
    return {"Hello": "world"}
if __name__ == '__main__':
    uvicorn.run("server:app",host="0.0.0.0",port=8000)
  • Dockerfile内容
FROM python:3.10-alpine
LABEL author="heruos"
WORKDIR /app
ADD . /app
EXPOSE 8000
RUN python3 -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
CMD python3 /app/server.py

打包

build方式

# 使用主机网络,避免容器内访问不到外网的奇怪问题
docker build --network=host -t mypy:1.0 .

commit方式

docker run -it --rm --network=host python:3.10-alpine sh
python3 -m pip install fastapi uvicorn -i https://pypi.tuna.tsinghua.edu.cn/simple
# docker cp将文件放到容器内
# 依据修改后的容器封装成新的镜像
docker commit -a "heruos" -m "install fastapi" 容器id myfastapi:1.0

compose方式

在docker-compose文件中添加封装配置,封装后直接启动。

  • docker-compose.yaml内容
version: "3"
services:
  fastapi:
    build:
      context: .
      dockerfile: Dockerfile
      # 使用主机网络
      network: host
    # 指定的镜像就是封装后的镜像文件名
    image: fastapi:1.0
    container_name: fa_app
    hostname: fa_app
    ports:
      - 8000:8000
  • 构建并启动
docker-compose up -d

测试

# 查看镜像和容器
docker images
docker ps -a
# curl测试
curl http://127.0.0.1:8000
相关文章
|
23天前
|
Docker 容器
|
10天前
|
Java Docker 微服务
SpringBoot微服务打包Docker镜像
SpringBoot微服务打包Docker镜像
44 11
|
3天前
|
Go Docker Python
docker的python与go镜像的制作
docker的python与go镜像的制作
10 1
|
10天前
|
Docker 容器
用Docker发布自己的镜像
用Docker发布自己的镜像
42 9
|
12天前
|
存储 安全 Ubuntu
docker中的镜像
【10月更文挑战第1天】
27 4
|
10天前
|
缓存 安全 Linux
docker镜像管理问题
【10月更文挑战第3天】
48 1
|
12天前
|
Docker 容器
docker的导入本地镜像和导出本地镜像
本文介绍了如何使用Docker对本地镜像进行导入和导出操作,包括从本地导入`nginx.tar`镜像以及将`open-webui`镜像导出并压缩为`open-webui.tar.gz`。
23 1
|
12天前
|
前端开发 应用服务中间件 nginx
docker运行nginx镜像
这篇文章详细说明了如何在Docker中部署并运行Nginx服务,包括拉取镜像、配置文件的挂载以及容器的启动配置。
70 0
docker运行nginx镜像
|
16天前
|
搜索推荐 应用服务中间件 nginx
docker与containerd镜像获取及导出导入的区别与注意事项(报错信息:ctr: content digest sha256........ac47: not found)
docker与containerd镜像获取及导出导入的区别与注意事项(报错信息:ctr: content digest sha256........ac47: not found)
|
3天前
|
网络协议 Docker 容器
docker pull命令拉取镜像失败的解决方案
docker pull命令拉取镜像失败的解决方案
68 0