Docker撰写安装需求.txt [英] Docker compose installing requirements.txt

查看:124
本文介绍了Docker撰写安装需求.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的docker映像中,我正在克隆git master分支以检索代码。我正在将docker-compose用于开发环境并使用卷运行我的容器。从我的python requirements.txt文件安装新项目需求时,我遇到了一个问题。在开发环境中,它永远不会在开发环境上安装新的要求,因为在重新构建映像时,最新代码是从github提取的。

In my docker image I am cloning the git master branch to retrieve code. I am using docker-compose for the development environment and running my containers with volumes. I ran across an issue when installing new project requirements from my python requirements.txt file. In the development environment, it will never install new requirements on dev environment because when re-building the image, the latest code is pulled from github.

下面是一个示例:我的dockerfile:

Below is an example of my dockerfile:

FROM base

# Clone application
RUN git clone repo-url
# Install application requirements
RUN pip3 install -r app/requirements.txt

# ....

这是我的撰写文件:

myapp:
    image: development
    env_file: .env
    ports:
        - "8000:80"
    volumes:
        - .:/home/app

    command: python3 manage.py runserver 0.0.0.0:8000

是否有任何新安装方法

推荐答案

有两种方法可以实现此目的。

There are two ways you can do this.

您可以输入容器并按自己的方式操作自。缺点:不是自动化的。

You can enter the container and do it yourself. Downside: not automated.

$ docker-compose exec myapp bash
2912d2cd9eab# pip3 install -r /home/app/requirements.txt



使用入口点脚本



您可以使用运行准备工作然后运行命令的入口点脚本。

Using an entrypoint script

You can use an entrypoint script that runs prep work, then runs the command.

Dockerfile:

COPY entrypoint.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh

# ... probably other stuff in here ...

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh:

#!/bin/sh

cd /home/app
pip3 install -r requirements.txt

# May as well do this too, while we're here.
python3 manage.py migrate

exec "$@"

入口点在容器启动时是这样运行的:

The entrypoint is run like this at container startup:

/entrypoint.sh $CMD

其中的扩展为:

/entrypoint.sh python3 manage.py runserver 0.0.0.0:8000

首先运行准备工作,然后在入口点脚本的末尾执行传入的参数。这是您的命令,所以entrypoint.sh退出并由Django应用服务器替换。

The prep work is run first, then at the end of the entrypoint script, the passed-in argument(s) are exec'd. That's your command, so entrypoint.sh exits and is replaced by your Django app server.

更新:

发表评论聊天后,很重要的一点是使用 exec 运行命令,而不是在命令末尾运行它像这样的入口点脚本:

After taking comments to chat, it came up that it is important to use exec to run the command, instead of running it at the end of the entrypoint script like this:

python3 manage.py runserver 0.0.0.0:8000

我不完全记得为什么这很重要,但是我以前也遇到过这个问题。您需要执行该命令,否则它将无法正常工作。

I can't exactly recall why it matters, but I ran into this previously as well. You need to exec the command or it will not work properly.

这篇关于Docker撰写安装需求.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆