docker init

描述為您的專案建立 Docker 相關啟動檔案
用法docker init [OPTIONS]
要求: Docker Desktop 4.27 及更高版本

描述

使用在容器中執行專案所需的檔案初始化專案。

Docker Desktop 提供了 `docker init` CLI 命令。在您的專案目錄中執行 `docker init` 將引導您建立以下檔案,併為您的專案提供合理的預設值:

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

如果任何檔案已存在,將出現提示並提供警告,同時為您提供覆蓋所有檔案的選項。如果 `docker-compose.yaml` 而不是 `compose.yaml` 已存在,`docker init` 可以覆蓋它,並將 `docker-compose.yaml` 用作 Compose 檔案的名稱。

警告

被覆蓋的檔案無法恢復。在選擇覆蓋之前,請重新命名現有檔案或將其複製到另一個目錄以進行備份。

執行 `docker init` 後,您可以選擇以下模板之一:

  • ASP.NET Core:適用於 ASP.NET Core 應用程式。
  • Go:適用於 Go 伺服器應用程式。
  • Java:適用於使用 Maven 並打包為 uber jar 的 Java 應用程式。
  • Node:適用於 Node 伺服器應用程式。
  • 帶 Apache 的 PHP:適用於 PHP Web 應用程式。
  • Python:適用於 Python 伺服器應用程式。
  • Rust:適用於 Rust 伺服器應用程式。
  • 其他:容器化應用程式的通用起點。

在 `docker init` 完成後,您可能需要修改建立的檔案並根據您的專案進行調整。訪問以下主題以瞭解有關這些檔案的更多資訊:

選項

選項預設值描述
--version顯示 init 外掛的版本

示例

執行 `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 https://: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 https://: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 https://: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 https://: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 https://:8000

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

選擇帶 Apache 的 PHP 的示例

以下示例顯示了選擇 `帶 Apache 的 PHP` 後出現的提示和示例輸入。帶 Apache 的 PHP 模板適用於純 PHP 應用程式和使用 Composer 作為依賴管理器的應用程式。執行 `docker init` 後,您必須手動將應用程式所需的任何 PHP 擴充套件新增到 Dockerfile 中。

? 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 https://: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 https://:9000

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

選擇“其他”的示例

以下示例顯示了選擇 `其他` 後的輸出。

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