本文翻译自 medium 论坛,原文链接:medium.com/@akhilesh-m… , 原文作者:
Akhilesh Mishra
您是那种觉得编写 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 应用程序。
一、创立 app.py 以及 requirements.txt
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 init。
docker init
出现如下结果,
接下来要做的便是挑选应用程序渠道,在咱们的示例中,咱们运用 python。它将建议您的项目的推荐值,例如 Python 版别、端口、进口点指令。
您可以挑选默认值或供给所需的值,它将创立您的 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。
它遵从人们在所有 Linkedin 和 Medium 帖子中不断告知咱们的所有功能和安全最佳实践。
docker-compose.yml
它编写了 docker-compose 装备来运转应用程序。由于咱们的应用程序不包括与数据库的任何连接,因此它注释掉了数据库容器可能需求的代码。
如果您想在 Flask 应用程序中运用数据库,请从 docker-compose 文件中撤销注释 db 服务装备,创立一个包括机密的本地文件,然后运转该应用程序。它还为咱们生成了 .dockerignore 文件。
为什么运用 docker init?
docker init 使 Docker 化变得轻而易举,特别是对于 Docker 新手来说。它消除了编写 Dockerfile 和其他装备文件的手动任务,然后节省时间并最大限度地减少错误。
它运用模板依据您的应用程序类型自定义 Docker 设置,一起遵从职业最佳实践。
总结一下
总而言之,docker init 完成了上面这一切。
- 它可以编写比这里 90% 的孩子更好的 Docker 装备。
- 像书呆子一样遵从最佳实践。
- 当安全人员的东西生成包括数百个您从未想过存在的缝隙的陈述时,可以节省时间、精力和来自安全人员的讽刺评论。
最后需求说明的是,就像任何其他基于人工智能的东西一样,这个东西也不完美。不要盲目信任它生成的装备。我建议您在运用装备之前再次查看下装备。
如果觉得这篇文章翻译不错的话,无妨点赞加重视,我会更新更多技能干货、项目教学、经验共享的文章。





