docker 初始化
描述 | 为您的项目创建与 Docker 相关的启动文件 |
---|---|
用法 | docker init [OPTIONS] |
描述
使用在容器中运行项目所需的文件初始化项目。
Docker Desktop 提供了docker init
CLI 命令。跑docker init
在您的项目目录中,逐步完成为您的项目创建以下具有合理默认值的文件:
- .dockerignore
- Dockerfile 文件
- compose.yaml
- README.Docker.md
如果任何文件已存在,则会显示提示并提供警告
以及为您提供覆盖所有文件的选项。如果docker-compose.yaml
already exists 而不是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 与 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 的示例
以下示例显示了选择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 的示例
以下示例显示了选择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 的示例
以下示例显示了选择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.