# docker #docker运行全流程!必看 ! docker image镜像生成、docker运行、重启、docker-compose外挂文件、
必须保证原始启动端口(server启动端口或者通过环境变量设置的端口)、dockerfile expose的端口、docker-compose.yml被映射的端口(如果不需要使用docker-compose.yml、请忽略)三口一致。
·
一、核对信息
必须保证
原始启动端口(server启动端口或者通过环境变量设置的端口)、
dockerfile expose的端口、
docker-compose.yml被映射的端口(如果不需要使用docker-compose.yml、请忽略)
三口一致
env、dockerfile、docker-compose 配置信息示例

我这里环境变量端口号是49070
FROM python:3.11-slim # Use an official Python runtime as a parent image
RUN apt-get update && apt-get install -y git # Install git
RUN pip install poetry==1.6.1 # Install poetry
RUN poetry config virtualenvs.create false # Disable virtualenvs
WORKDIR /code # Set the working directory to /code # Copy the current directory contents into the container at /code
COPY ./pyproject.toml ./requirements.txt ./README.md ./poetry.lock* ./ # Install any needed packages specified in requirements.txt
COPY ./package[s] ./packages # Install any needed packages specified in requirements.txt
RUN pip3 install -r requirements.txt # Install any needed packages specified in requirements.txt
COPY ./app ./app # Make port 80 available to the world outside this container
COPY ./.env.template ./.env # Define environment variables
EXPOSE 49070 # Run app.py when the container launches
ENV PYTHONPATH=/code # Set the PYTHONPATH environment variable
CMD ["python", "/code/app/server.py"] # Run app.py when the container launches

version: '2.24.7' # specify docker-compose version
services: # services to run
xinrenrag49061: # name of the first service
image: xinrenrag49061:latest # specify image to build container from
volumes: # specify volumes to mount
- /home/xinrui/projects/xinren-rag/configs/:/code/configs/
- /home/xinrui/projects/xinren-rag/.env:/code/.env
ports: # specify ports mapping
- 49070:49070
environment: # environment variables
- PYTHONPATH=/code
二、生成docker image
vscode只需要从界面点点点就可以生成镜像了,具体操作如下:

等待生成
生成成功,按任意键跳出

可以在terminal 使用docker images查看生成好的容器
失败解决
如果遇到失败,请检查dockerfile的端口和你server运行端口是否一致、需要pip的库是否可以下载、需要pip的库是否有冲突、端口是否已经被占用、运行路径是否正确等等,总之检查你的dockerfile内容对不对
三、运行docker image及其他操作
以下是一些用于管理Docker镜像和容器的命令:
-
列出所有当前存储在你的系统上的Docker镜像:
docker images -
从
[imagename]镜像启动一个新的Docker容器,并将容器的端口映射到主机系统的端口:docker run -p [映射出的端口号]:[dockerfile中开放的端口号] [imagename] -
停止ID为
[ID]的Docker容器:docker stop [ID] -
删除ID为
[ID]的Docker容器:docker rm [ID] -
删除名为
[imagename]的Docker镜像:docker rmi [imagename] -
列出所有基于
[imagename]镜像的Docker容器(包括正在运行的和已经停止的):docker ps -a | grep [imagename]
四、使用docker-compose 挂载外部文件

docker-compose up -d
五、如果是重启
移除镜像

更多推荐
所有评论(0)