请立刻停止编写 Dockerfiles 并使用 docker init

wayn 2024-04-16 10:42:39
您是那种觉得编写 Dockerfile 和 docker-compose.yml 文件很痛苦的人之一吗?

 

我承认,我就是其中之一。

 

我总是想知道我是否遵循了 Dockerfile、 docker-compose 文件的最佳编写实践,我害怕在不知不觉中引入了安全漏洞。

 

但是现在,我不必再担心这个问题了,感谢 Docker 的优秀开发人员,他们结合了生成式人工智能,创建了一个 CLI 实用工具 — docker init。

 

介绍 docker init

 

图片

 

前段时间,Docker 推出了 docker init 的通用版本。我已经尝试过,发现它非常有用,迫不及待地想在日常生活中使用它。

 

什么是 docker init?

 

docker init 是一个命令行应用程序,可帮助初始化项目中的 Docker 资源。它根据项目的要求创建 Dockerfiles、docker-compose 文件和 .dockerignore 文件。

 

这简化了为项目配置 Docker 的过程,节省时间并降低复杂性。

 

最新版本的 docker init 支持 Go、Python、Node.js、Rust、ASP.NET、PHP 和 Java。目前它只能于 Docker Desktop 一起使用,也就是说大家目前在 Linux 系统中是无法使用 docker init 的。

 

如何使用 docker init?

 

使用 docker init 很简单,只需几个简单的步骤。首先,转到您要在其中设置 Docker 资源的项目目录。

 

举个例子,我来创建一个基本的 Flask 应用程序。

 

 

1、创建 app.py 以及 requirements.txt

 

  •  
touch app.py requirements.txt

 

将以下代码复制到相应文件中

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
# app.pyfrom 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.txtFlask
 

2、使用 docker init 初始化

 

docker init 将扫描您的项目并要求您确认并选择最适合您的应用程序的模板。选择模板后,docker init 会要求您提供一些特定于项目的信息,自动为您的项目生成必要的 Docker 资源。

 

现在让我们来执行 docker init。

 

  •  
docker init

 

出现如下结果,

 

图片

 

接下来要做的就是选择应用程序平台,在我们的示例中,我们使用 python。它将建议您的项目的推荐值,例如 Python 版本、端口、入口点命令。

 

图片

 

您可以选择默认值或提供所需的值,它将创建您的 docker 配置文件以及动态运行应用程序的说明。

 

图片

 

让我们来看看这个自动生成的配置是什么样子。

 

 

3、生成 Dockerfile 文件

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
# 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.7FROM 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=10001RUN 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 帖子中不断告诉我们的所有性能和安全最佳实践。

 

 

docker-compose.yml

 

图片

 

它编写了 docker-compose 配置来运行应用程序。由于我们的应用程序不包含与数据库的任何连接,因此它注释掉了数据库容器可能需要的代码。

 

如果您想在 Flask 应用程序中使用数据库,请从 docker-compose 文件中取消注释 db 服务配置,创建一个包含机密的本地文件,然后运行该应用程序。它还为我们生成了 .dockerignore 文件。

 

为什么使用 docker init?

 

docker init 使 Docker 化变得轻而易举,特别是对于 Docker 新手来说。它消除了编写 Dockerfile 和其他配置文件的手动任务,从而节省时间并最大限度地减少错误。

 

它使用模板根据您的应用程序类型自定义 Docker 设置,同时遵循行业最佳实践。

 

总结一下

 

总而言之,docker init 完成了上面这一切。

 

  • 它可以编写比 90% 的孩子更好的 Docker 配置。

  • 像书呆子一样遵循最佳实践。

  • 当安全人员的工具生成包含数百个您从未想过存在的漏洞的报告时,可以节省时间、精力和来自安全人员的讽刺评论。

 

最后需要说明的是,就像任何其他基于人工智能的工具一样,这个工具也不完美。不要盲目相信它生成的配置。我建议您在使用配置之前再次检查下配置。

 

作者丨wayn
来源丨公众号:程序员wayn(ID:gh_cb28562524da)
dbaplus社群欢迎广大技术人员投稿,投稿邮箱:editor@dbaplus.cn
最新评论
访客 2023年08月20日

230721

访客 2023年08月16日

1、导入Mongo Monitor监控工具表结构(mongo_monitor…

访客 2023年08月04日

上面提到: 在问题描述的架构图中我们可以看到,Click…

访客 2023年07月19日

PMM不香吗?

访客 2023年06月20日

如今看都很棒

活动预告