Docker create command
docker create The command creates a new container but does not start it.
docker create The command creates a container instance based on the specified image and arguments, but the container is only initialized at creation and does not execute any process.
Usage is the same as docker run。
Syntax
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Common parameters
--name: Assigns a name to the container.-p, --publish: Port mapping, in the formathost_port:container_port。-v, --volume: Mounts a volume, in the formathost_dir:container_dir。-e, --env: Sets environment variables.--network: Specifies the container's network mode.--restart: The container's restart policy (such asno、on-failure、always、unless-stopped)。-u, --user: Specifies the user.--entrypoint: Overrides the container's default entrypoint.--detach: Creates the container in the background.
Example
Create a container:
docker create ubuntu
Creates a container from the ubuntu image but does not start it.
Create and specify a container name:
docker create --name my_container ubuntu
Creates a container named my_container but does not start it.
Create and set environment variables:
docker create -e MY_ENV_VAR=my_value ubuntu
Creates a container and sets the environment variable MY_ENV_VAR to my_value.
Create and mount a volume:
docker create -v /host/data:/container/data ubuntu
Creates a container and mounts the host's /host/data directory to the container's /container/data directory.
Create with port mapping:
docker create -p 8080:80 nginx
Creates a container and maps local host port 8080 to container port 80, but does not start it.
Create and specify a restart policy:
docker create --restart always nginx
Creates a container and sets its restart policy to always.
Create and specify a user:
docker create -u user123 ubuntu
Creates a container and runs it as user user123.
View containers
After creating a container, you can use the docker ps -a command to view all containers, including those created but not started.
docker ps -a
Start a created container
Use the docker start command to start a created but unstarted container:
docker start my_container
Summary
docker create: Used to create a new container instance without starting it. The container's configuration can be set through various arguments.docker start: Starts a created container so it begins running.
docker create The command allows users to preconfigure container settings and manually start the container when needed, which is particularly useful for automated deployment and testing scenarios.
Docker Command Reference