代码之家  ›  专栏  ›  技术社区  ›  bluesummers

Docker容器在中安装新包要求.txt启动时

  •  2
  • bluesummers  · 技术社区  · 6 年前

    requirements.txt 启动时-如果有任何更改。

    给定一个Dockerfile,如下所示

    FROM python 
    
    WORKDIR /opt/code
    
    COPY ./requirements.txt /opt/code/requirements.txt
    
    ...
    
    RUN pip --no-cache-dir install --upgrade -r requirements.txt
    
    ...
    

    要求.txt 要求.txt 然后打电话来 docker restart my_container .

    作为一个额外的好处,如果我可以写一个日志文件,如果新的软件包只安装了新安装的名称(避免“已经安装”的消息)

    1 回复  |  直到 6 年前
        1
  •  2
  •   BMitch    6 年前

    您可以在运行应用程序之前创建一个执行此操作的入口点脚本。我可能会使用compose文件来部署它,而不是直接在映像中部署它,这样生产环境中就不会有动态更新的容器。

    #!/bin/sh
    # if a command is not provided, set a default command
    if [ $# -eq 0 ]; then
      set -- python app.py
    fi
    # update to new requirements on each container start
    pip --no-cache-dir install --upgrade -r requirements.txt
    # use exec to replace pid 1 with the command (e.g. python app.py)
    exec "$@"
    

    COPY entrypoint-dev.sh /
    

    然后在开发组合文件中,可以使用该入口点:

    version: '3'
    services:
      app:
        image: app:dev
        entrypoint:
        - /entrypoint-dev.sh
        volumes:
        - .:/opt/code