您是那些觉得编写Dockerfiles
和docker-compose.yml
很痛苦的人之一吗?
至少我从来没有享受过。我总是想知道我是否遵循了最佳实践,并且在编写配置文件时是否在不知不觉中引入了安全Dockerfile漏洞。
好吧,我不必再担心这个问题了,感谢 Docker 的优秀人员,他们更好地利用了生成式人工智能,而没有引起太多噪音。他们创建了一个 CLI 实用工具 —docker init
官网:https://docs.docker.com/engine/reference/commandline/init/
docker init
是Docker正式发布的。我已经尝试过,发现它非常有用,迫不及待地想在日常生活中使用它。
docker init
是一个命令行实用程序,可帮助初始化项目中的 Docker 资源。.dockerignore
它根据项目的要求创建 Dockerfile
、Compose 文件
。这简化了为项目配置 Docker 的过程,节省时间并降低复杂性。
最新版本docker init支持 Go、Python、Node.js、Rust、ASP.NET、PHP 和 Java
。它可与 Docker Desktop
一起使用。
使用起来docker init很简单,只需几个简单的步骤。首先,转到要设置 Docker 的项目目录。 让我创建一个基本的 Flask 应用程序。
touch app.py requirements.txt
将以下代码复制到相应文件中
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_docker():
return '<h1> hello world </h1'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
# requirements.txt
Flask
docker init将扫描您的项目并要求您确认并选择最适合您的应用程序的模板。选择模板后,docker init系统会要求您提供一些特定于项目的信息,并自动为您的项目生成必要的 Docker 资源。
docker init
接下来您要做的就是选择应用程序平台,对于我们正在使用的示例,python.它将为您的项目建议值,例如Python版本port、entrypoint命令。
您可以选择默认值或提供所需的值,它将创建您的 docker 配置文件以及动态运行应用程序的说明。
让我们看看这个自动生成的配置是什么样的。
# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/
ARG PYTHON_VERSION=3.11.7
FROM python:${PYTHON_VERSION}-slim as base
# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1
# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1
WORKDIR /app
# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
python -m pip install -r requirements.txt
# Switch to the non-privileged user to run the application.
USER appuser
# Copy the source code into the container.
COPY . .
# Expose the port that the application listens on.
EXPOSE 5000
# Run the application.
CMD gunicorn 'app:app' --bind=0.0.0.0:5000
看看它,它写了一个比我更好的 Dockerfile。它遵循人们在所有 Linkedin 和 Medium 帖子中不断告诉我们的所有性能和安全最佳实践。
image.png
它编写了 docker-compose 配置来运行应用程序。由于我们的应用程序不包含与数据库的任何连接,因此它注释掉了数据库容器可能需要的代码。
如果您想在 Flask 应用程序中使用数据库,请从 docker-compose
文件中取消注释 db 服务配置,创建一个包含机密的本地文件,然后运行该应用程序。它还生成了 .dockerignore
文件。
docker init使应用服务 docker 化变得轻而易举,特别是对于 Docker 新手来说。它消除了编写 Dockerfile 和其他配置文件的手动任务,从而节省时间并最大限度地减少错误。它使用模板根据您的应用程序类型自定义 Docker 设置,同时遵循行业最佳实践。
总而言之,docker init做到了一切。
免责声明:就像任何其他基于人工智能的工具一样,这个工具也不完美。不要盲目相信它生成的配置。我建议您在推进配置之前再次查看配置。
文章原文:
https://medium.com/@akhilesh-mishra/you-should-stop-writing-dockerfiles-today-do-this-instead-3cd8a44cb8b0