docker init
描述 | 为您的项目创建 Docker 相关的启动文件 |
---|---|
用法 | docker init [OPTIONS] |
描述
使用必要的项目文件初始化项目,以便在容器中运行项目。
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 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://127.0.0.1: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://127.0.0.1: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://127.0.0.1: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://127.0.0.1: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://127.0.0.1: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 https://127.0.0.1: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://127.0.0.1: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.