不会还有人不会用docker init吧?

现在立刻马上给我停止编写 Dockerfiles,改为使用docker init 编写 Dockerfile and docker-compose配置文件!

您是那些觉得写作Dockerfilesdocker-compose.yml文件很痛苦的人之一吗?

至少我从来没有享受过。

我总是想知道我是否遵循了最佳实践,并且在编写配置文件时是否在不知不觉中引入了安全Dockerfile漏洞docker-compose

好吧,我不必再担心这个问题了,感谢 Docker 的优秀人员,他们更好地利用了生成式人工智能,而没有引起太多噪音。

他们创建了一个 CLI 实用工具 —docker init

介绍 docker init

docker initDocker上个月正式发布。我已经尝试过,发现它非常有用,迫不及待地想在日常生活中使用它。

什么是 docker init

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

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

最新版本docker init支持 Go、Python、Node.js、Rust、ASP.NET、PHP 和 Java。它可与 Docker Desktop 一起使用。

如何使用docker init

使用起来docker init很简单,只需几个简单的步骤。首先,转到要设置 Docker 资产的项目目录。

Let me Create a basic Flask app.

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 init系统会要求您提供一些特定于项目的信息,并自动为您的项目生成必要的 Docker 资源。

docker initinit

接下来要做的是选择应用程序平台,例如我们使用的是python。它将建议您的项目的推荐值,如Python版本, port, entrypoint 命令。

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

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

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.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。

它遵循人们在所有示例中不断告诉我们的所有性能和安全最佳实践。

compose.yaml

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

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

为什么使用docker init

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

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

结尾

总而言之,docker init做到了一切。

编写比这里 90% 的同学更好的 Docker 配置。
像书呆子一样遵循最佳实践。
当安全人员的工具生成包含数百个您从未想过存在的漏洞的报告时,可以节省时间、精力和来自安全人员的讽刺评论。

  • 免责声明:就像任何其他基于人工智能的工具一样,这个工具也不完美。不要盲目相信它生成的配置。我建议您在推进配置之前再次查看配置。
posted @ 2024-03-04 15:15  Sureing  阅读(26)  评论(0编辑  收藏  举报