简体   繁体   English

我如何在正在运行的 docker 容器中安装包并且包在不重新创建容器的情况下生效?

[英]how I can install packages inside a running docker container and packages take effect without recreating the container?

I am running docker compose up which consists of multiple containers on of which is python 3.* and all the containers have volumes attached to them.我正在运行docker compose up ,它由多个容器组成,其中 python 3.* 并且所有容器都附加了卷。 also I have already created requirements.txt file I have entered python container and install x packages then I did pip freeze > requirements.txt I then I stoped the containers and restart the containers again, but python container didn't start and the log says modules x is not found , so what I did is that O deleted the container and created a new one and it worked, my questions is, Is there any way to not deleting the container (I think its over kill)我也已经创建了 requirements.txt 文件我输入了 python 容器并安装了 x 包然后我做了pip freeze > requirements.txt然后我停止了容器并再次重新启动容器,但是 python 容器没有启动并且日志说modules x is not found ,所以我所做的是 O 删除了容器并创建了一个新容器并且它有效,我的问题是,有没有办法不删除容器(我认为它过度杀戮)
but some-who still able to manage installing packages in the container?但是有些人仍然能够管理在容器中安装包?

Dockerfile Dockerfile

FROM python:3.6
RUN apt-get update
RUN apt-get install -y gettext

RUN mkdir -p /var/www/server
COPY src/requirements.txt /var/www/server/

WORKDIR /var/www/server
RUN pip install -r ./requirements.txt
EXPOSE 8100
ENTRYPOINT sleep 3 && python manage.py migrate && python manage.py runserver 0.0.0.0:8100

You should move your project source files into the container during build and within in it run the pip install -r requirements.txt .您应该在构建期间将项目源文件移动到容器中,并在其中运行pip install -r requirements.txt

Below is an example to give you an idea:下面是一个给你一个想法的例子:

--- Other build commands follow
WORKDIR /usr/src/service
COPY ./service . # Here, I am moving everything of the service folder/module into WORKDIR within docker
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
-- Other build commands follow. 

Finally, you will use docker-compose build service to build the define service in the docker-compose.yml pointing to the Dockerfile in the build context.最后,您将使用 docker-compose 构建服务在构建上下文中指向 Dockerfile 的 docker-compose.yml 中构建定义服务。

...
build:
  context: .
    dockerfile: service/Dockerfile
...

Broadly, set up your Dockerfile such that you need to do the least-changing and most time-costly work first大体上,设置你的 Dockerfile,这样你需要首先做最少变化和最耗时的工作

FROM FOO
RUN get os-level and build dependencies
COPY only exactly files needed to identify dependencies
RUN install dependencies that takes a long time
RUN install more frequently-changing dependencies
COPY rest of your wanted content
ENTRYPOINT define me

as @coldly says in their Answer , write your dependencies into a requirements file and install them during the container build!正如@coldly在他们的回答中所说,将您的依赖项写入需求文件并在容器构建期间安装它们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM