Docker上的Flask应用程序带有加密功能

时间:2017-05-01 20:58:02

标签: python docker flask https lets-encrypt

我想在Docker实例中创建一个Flask应用程序,该实例使用Let的加密方法获取SSL证书并启用HTTPS。证书还需要经常自动更新(我认为3个月),这已经在我的服务器上完成,但Flask应用程序也需要访问该文件!

我需要在此Docker文件上修改哪些内容才能启用加密功能?

FROM ubuntu:latest
RUN apt-get update -y && apt-get upgrade -y
RUN apt-get install -y python-pip python-dev build-essential
RUN pip install --upgrade pip
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["web/app.py"]

1 个答案:

答案 0 :(得分:0)

您可以使用docker volume feature

卷是主机和容器之间的安装目录。

使用泊坞窗创建卷有两种方法:

  1. 您还可以在dockerfile中声明VOLUME命令

    FROM ubuntu:latest RUN apt-get update -y && apt-get upgrade -y RUN apt-get install -y python-pip python-dev build-essential RUN pip install --upgrade pip COPY . /app WORKDIR /app RUN pip install -r requirements.txt VOLUME /certdir ENTRYPOINT ["python"] CMD ["web/app.py"]

  2. 这将创建一个以/var/lib/docker/volumes内的容器ID命名的目录。

    当您想要将容器中的内容共享到主机时,此解决方案更有用,但如果它反过来则不太实用。

    1. 您可以在docker create或docker run上使用-v标志向容器添加卷:

      docker run -v /certdir ./certdir web/app.py

    2. 其中/certdir是容器内的目录/ certdir,而./certdir是项目目录中主机上的目录。

      此解决方案将起作用,因为主机目录将安装在定义位置的容器内。但是如果没有在某些文档中明确指定它或为docker run / create命令提供易于使用的别名,则其他用户将不知道如何定义它。

      PS:快速提示:

      将RUN命令放在一个语句中:

      ```
      FROM ubuntu:latest
      RUN apt-get update -y && apt-get upgrade -y \
          && apt-get install -y python-pip python-dev build-essential \
          && pip install --upgrade pip
      COPY . /app
      ```
      

      优点是docker只会创建一个用于安装依赖项的层而不是三层。 (见documentation