# Docker create 命令

[Docker命令大全](/docker/manual/docker-command-manual.md)

`docker create` 命令用于创建一个新的容器，但不会启动它。

`docker create` 命令会根据指定的镜像和参数创建一个容器实例，但容器只会在创建时进行初始化，并不会执行任何进程。

用法同 [docker run](/docker/manual/docker-run-command.md)。

### 语法

docker create \[OPTIONS] IMAGE \[COMMAND] \[ARG...]

### 常用参数

* `--name`: 给容器指定一个名称。
* `-p, --publish`: 端口映射，格式为 `host_port:container_port`。
* `-v, --volume`: 挂载卷，格式为 `host_dir:container_dir`。
* `-e, --env`: 设置环境变量。
* `--network`: 指定容器的网络模式。
* `--restart`: 容器的重启策略（如 `no`、`on-failure`、`always`、`unless-stopped`）。
* `-u, --user`: 指定用户。
* `--entrypoint`: 覆盖容器的默认入口点。
* `--detach`: 在后台创建容器。

### 实例

创建一个容器：

```shell
docker create ubuntu
```

根据 ubuntu 镜像创建一个容器，但不会启动它。

创建并指定容器名称：

```shell
docker create --name my_container ubuntu
```

创建一个名为 my\_container 的容器，但不会启动它。

创建并设置环境变量：

```shell
docker create -e MY_ENV_VAR=my_value ubuntu
```

创建一个容器，并设置环境变量 MY\_ENV\_VAR 的值为 my\_value。

创建并挂载卷：

```shell
docker create -v /host/data:/container/data ubuntu
```

创建一个容器，并将主机的 /host/data 目录挂载到容器的 /container/data 目录。

创建并端口映射：

```shell
docker create -p 8080:80 nginx
```

创建一个容器，将本地主机的 8080 端口映射到容器的 80 端口，但不会启动它。

创建并指定重启策略：

```shell
docker create --restart always nginx
```

创建一个容器，并将重启策略设置为 always。

创建并指定用户：

```shell
docker create -u user123 ubuntu
```

创建一个容器，并以 user123 用户运行容器。

### 查看容器

在创建容器之后，可以使用 docker ps -a 命令查看所有容器，包括已创建但未启动的容器。

```shell
docker ps -a
```

### 启动已创建的容器

使用 docker start 命令来启动已创建但未启动的容器：

```shell
docker start my_container
```

### 总结

* `docker create`: 用于创建一个新的容器实例但不启动它。可以通过各种参数设置容器的配置。
* `docker start`: 启动已创建的容器，使其开始运行。

`docker create` 命令允许用户预先配置容器的设置，并在需要时手动启动容器，这对于自动化部署和测试场景特别有用。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tuonioooo-notebook.gitbook.io/docker/manual/docker-create-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
