← 返回命令列表

Linux command

docker-run 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Run a container interactively

docker run -it [image] [bash]

Run container in background

docker run -d [image]

Run with port mapping

docker run -p [8080:80] [image]

Run with volume mount

docker run -v [/host/path:/container/path] [image]

Run with environment variables

docker run -e [VAR=value] [image]

Run with automatic removal on exit

docker run --rm [image]

Run with custom name

docker run --name [container_name] [image]

Run with memory limit

docker run -m [512m] [image]

说明

docker run creates and starts a new container from an image. It combines docker create and docker start into a single command, providing the primary way to launch containers. The command pulls the image if not present locally, creates a container from it, and starts execution. Options control resource allocation, networking, storage, and runtime behavior. Containers are isolated but can connect to host resources through port mapping, volume mounts, and network configuration. The -it flags enable interactive terminal access for debugging and exploration.

参数

-d, --detach
Run container in background.
-it
Interactive with pseudo-TTY.
-p, --publish _host:container_
Publish container port to host.
-v, --volume _host:container_
Bind mount a volume.
-e, --env _VAR=value_
Set environment variable.
--name _name_
Assign container name.
--rm
Remove container on exit.
-m, --memory _limit_
Memory limit (e.g., 512m, 1g).
--cpus _n_
Number of CPUs.
--network _network_
Connect to network.
--restart _policy_
Restart policy: no, always, unless-stopped, on-failure.
-w, --workdir _dir_
Working directory inside container.
-u, --user _user_
Username or UID.
--entrypoint _cmd_
Override default entrypoint.

FAQ

What is the docker-run command used for?

docker run creates and starts a new container from an image. It combines docker create and docker start into a single command, providing the primary way to launch containers. The command pulls the image if not present locally, creates a container from it, and starts execution. Options control resource allocation, networking, storage, and runtime behavior. Containers are isolated but can connect to host resources through port mapping, volume mounts, and network configuration. The -it flags enable interactive terminal access for debugging and exploration.

How do I run a basic docker-run example?

Run `docker run -it [image] [bash]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -d, --detach do in docker-run?

Run container in background.