docker 初始化

描述为您的项目创建与 Docker 相关的启动文件
用法docker init [OPTIONS]

描述

使用在容器中运行项目所需的文件初始化项目。

Docker Desktop 提供 CLI 命令。在项目目录中运行,以逐步创建以下文件,并为项目设置合理的默认值:docker initdocker init

  • .dockerignore
  • Dockerfile 文件
  • compose.yaml
  • README.Docker.md

如果任何文件已存在,则会显示提示并提供警告 以及为您提供覆盖所有文件的选项。如果已存在,而不是 ,则可以覆盖它,使用 Compose 文件。docker-compose.yamlcompose.yamldocker initdocker-compose.yaml

警告

您无法恢复被覆盖的文件。 要在选择覆盖现有文件之前备份该文件,请重命名该文件或将其复制到另一个目录。

运行 后,您可以选择以下模板之一:docker init

  • ASP.NET Core:适用于 ASP.NET Core 应用程序。
  • Go:适用于 Go 服务器应用程序。
  • Java:适用于使用 Maven 并将包作为 uber jar 的 Java 应用程序。
  • Node:适用于 Node 服务器应用程序。
  • PHP 与 Apache:适用于 PHP Web 应用程序。
  • Python:适用于 Python 服务器应用程序。
  • Rust:适用于 Rust 服务器应用程序。
  • 其他:容器化应用程序的通用起点。

完成后,您可能需要修改创建的文件并根据您的项目对其进行定制。请访问以下主题以了解有关文件的更多信息:docker init

选项

选择违约描述
--versioninit 插件的显示版本

例子

运行 docker init 的示例

以下示例显示了运行 .请参阅其他示例以查看每种语言或框架的选项。docker init

$ docker init

Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml
  - README.Docker.md

Let's get started!

? What application platform does your project use?  [Use arrows to move, type to filter]
> PHP with Apache - (detected) suitable for a PHP web application
  Go - suitable for a Go server application
  Java - suitable for a Java application that uses Maven and packages as an uber jar
  Python - suitable for a Python server application
  Node - suitable for a Node server application
  Rust - suitable for a Rust server application
  ASP.NET Core - suitable for an ASP.NET Core application
  Other - general purpose starting point for containerizing your application
  Don't see something you need? Let us know!
  Quit

选择 Go 的示例

以下示例显示了在选择和示例输入后出现的提示。Go

? What application platform does your project use? Go
? What version of Go do you want to use? 1.20
? What's the relative directory (with a leading .) of your main package? .
? What port does your server listen on? 3333

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml
CREATED: README.Docker.md

✔ Your Docker files are ready!

Take a moment to review them and tailor them to your application.

When you're ready, start your application by running: docker compose up --build

Your application will be available at http://localhost:3333

Consult README.Docker.md for more information about using the generated files.

选择 Node 的示例

以下示例显示了在选择和示例输入后出现的提示。Node

? What application platform does your project use? Node
? What version of Node do you want to use? 18
? Which package manager do you want to use? yarn
? Do you want to run "yarn run build" before starting your server? Yes
? What directory is your build output to? (comma-separate if multiple) output
? What command do you want to use to start the app? node index.js
? What port does your server listen on? 8000

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml
CREATED: README.Docker.md

✔ Your Docker files are ready!

Take a moment to review them and tailor them to your application.

When you're ready, start your application by running: docker compose up --build

Your application will be available at http://localhost:8000

Consult README.Docker.md for more information about using the generated files.

选择 Python 的示例

以下示例显示了在选择和示例输入后出现的提示。Python

? What application platform does your project use? Python
? What version of Python do you want to use? 3.8
? What port do you want your app to listen on? 8000
? What is the command to run your app (e.g., gunicorn 'myapp.example:app' --bind=0.0.0.0:8000)? python ./app.py

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml
CREATED: README.Docker.md

✔ Your Docker files are ready!

Take a moment to review them and tailor them to your application.

When you're ready, start your application by running: docker compose up --build

Your application will be available at http://localhost:8000

Consult README.Docker.md for more information about using the generated files.

选择 Rust 的示例

以下示例显示了在选择和示例输入后出现的提示。Rust

? What application platform does your project use? Rust
? What version of Rust do you want to use? 1.70.0
? What port does your server listen on? 8000

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml
CREATED: README.Docker.md

✔ Your Docker files are ready!

Take a moment to review them and tailor them to your application.

When you're ready, start your application by running: docker compose up --build

Your application will be available at http://localhost:8000

Consult README.Docker.md for more information about using the generated files.

选择 ASP.NET Core 的示例

以下示例显示了在选择和示例输入后出现的提示。ASP.NET Core

? What application platform does your project use? ASP.NET Core
? What's the name of your solution's main project? myapp
? What version of .NET do you want to use? 6.0
? What local port do you want to use to access your server? 8000

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml
CREATED: README.Docker.md

✔ Your Docker files are ready!

Take a moment to review them and tailor them to your application.

When you're ready, start your application by running: docker compose up --build

Your application will be available at http://localhost:8000

Consult README.Docker.md for more information about using the generated files.

使用 Apache 选择 PHP 的示例

以下示例显示了在选择和示例输入后出现的提示。带有 Apache 的 PHP 模板适用于纯 PHP 应用程序和使用 Composer 作为依赖项管理器的应用程序。运行 后,必须手动将应用程序所需的任何 PHP 扩展添加到 Dockerfile。PHP with Apachedocker init

? What application platform does your project use? PHP with Apache
? What version of PHP do you want to use? 8.2
? What's the relative directory (with a leading .) for your app? ./src
? What local port do you want to use to access your server? 9000

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml
CREATED: README.Docker.md

✔ Your Docker files are ready!

Take a moment to review them and tailor them to your application.

If your application requires specific PHP extensions, you can follow the instructions in the Dockerfile to add them.

When you're ready, start your application by running: docker compose up --build

Your application will be available at http://localhost:9000

Consult README.Docker.md for more information about using the generated files.

选择 Java 的示例

以下示例显示了在选择和示例输入后出现的提示。Java

? What application platform does your project use? Java
? What version of Java do you want to use? 17
? What's the relative directory (with a leading .) for your app? ./src
? What port does your server listen on? 9000

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml
CREATED: README.Docker.md

✔ Your Docker files are ready!

Take a moment to review them and tailor them to your application.

When you're ready, start your application by running: docker compose up --build

Your application will be available at http://localhost:9000

Consult README.Docker.md for more information about using the generated files.

选择 Other 的示例

以下示例显示了选择 后的输出。Other

? What application platform does your project use? Other

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml
CREATED: README.Docker.md

✔ Your Docker files are ready!

Take a moment to review them and tailor them to your application.

When you're ready, start your application by running: docker compose up --build

Consult README.Docker.md for more information about using the generated files.