但是将一个项目构建成image的挑战有很多: 依赖、环境变量、绝对路径、网路配置.
比如我具体遇到的报错:
1. 依赖问题: 有一个python package使用的是github安装而不是pypi的包, 所以需要自定义一下conda 导出的依赖
2. 环境变量问题: 后台需要读取环境变量中的文件, 如token、api-url, 但是docker image的环境变量设置不正确
3. 绝对路径: tensor_path, image_path,使用了绝对路径和相对路径, 在docker中不一致, 找不到文件
4. 网络问题: 容器间的网络连接, mongo、redis的host路径不能使用localhost得使用container_name. 容器与宿主机的网络: api监听失败, 容器中的api需要监听所有ip,, 不能监听localhost, 否则端口转发也不会成功.
5. 打包内容: 不能在非docker file上下文中将文件打包到image, 这种情况应该使用mount. 如: 大型model文件不要直接写入image, 应该使用mount的方式. 具体命令# Use NVIDIA's CUDA image as a base FROM nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu20.04 # Set the working directory WORKDIR /app # 设置 DEBIAN_FRONTEND 环境变量,避免交互 ENV DEBIAN_FRONTEND=noninteractive # 安装 tzdata 时自动选择时区,避免手动交互 RUN ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime && \ apt update &&\ apt-get install -y tzdata && \ dpkg-reconfigure --frontend noninteractive tzdata # Install necessary dependencies for conda RUN apt-get update && apt-get install -y \ wget \ bzip2 \ ca-certificates \ libglib2.0-0 \ libxext6 \ libsm6 \ libxrender1 \ git \ && rm -rf /var/lib/apt/lists/* # Install Miniconda RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /tmp/miniconda.sh && \ bash /tmp/miniconda.sh -b -p /opt/conda && \ rm /tmp/miniconda.sh ENV PATH=/opt/conda/bin:$PATH # Copy the environment file and install dependencies COPY meme-tg-environment.yml . RUN conda env create -f meme-tg-environment.yml # Activate the environment RUN echo "source activate meme" >> ~/.bashrc ENV PATH /opt/conda/envs/meme/bin:$PATH # 定义构建时的参数 ARG TOKEN ARG API_URL ENV TOKEN=$TOKEN ENV API_URL=$API_URL RUN echo "export TOKEN=$TOKEN" >> ~/.bashrc && \ echo "export API_URL=$API_URL" >> ~/.bashrc # Set the working directory to /app WORKDIR /app # Install any other project files COPY . /app RUN mkdir -p /root/.cache/huggingface/ RUN echo '#!/bin/bash\n\ cd /app/memeapi && python celery_worker.py &\n\ cd /app/memeapi && python main.py &\n\ cd /app/imbot && python main.py &\n\ wait' > /app/start_services.sh # 给脚本可执行权限 RUN chmod +x /app/start_services.sh # 容器启动时执行脚本 CMD ["/bin/bash", "/app/start_services.sh"]
sudo docker network create meme_network sudo docker network disconnect bridge 0f6606a9fd35 sudo docker network connect meme_network 0f6606a9fd35 sudo docker network disconnect bridge 04b3f9f0de9e sudo docker network connect meme_network 04b3f9f0de9e sudo docker build --build-arg TOKEN=$TOKEN --build-arg API_URL=$API_URL -t meme-cuda-image . sudo docker run --gpus all -td --network meme_network -p 8000:8000 -v /home/ubuntu/.cache/huggingface/:/root/.cache/huggingface/ --name aibot-meme meme-cuda-image
标签:meme,network,app,绝对路径,&&,docker,image,网路 From: https://www.cnblogs.com/aibot/p/18464642