docker init

說明為您的專案建立 Docker 相關的啟動檔案
用法docker init [選項]

說明

初始化一個專案,並建立在容器中運行該專案所需的檔案。

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 伺服器應用程式。
  • PHP with Apache:適用於 PHP 網頁應用程式。
  • 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 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.

選擇 PHP with Apache 的範例

以下範例顯示選擇 PHP with Apache 和範例輸入後出現的提示。PHP with Apache 範本適用於純 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 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 後的輸出。

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