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 应用程序。
- 节点:适用于节点服务器应用程序。
- 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 http://localhost:3333
Consult README.Docker.md for more information about using the generated files.
选择节点的示例
以下示例展示了选择 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 与 Apache 的示例
下面的示例展示了选择 PHP with Apache 和示例输入后出现的提示。带有 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 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.