Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Docker run command

Docker run command

Docker Command ReferenceDocker Command Reference


The docker run command is used to create and start a new container.

Syntax

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Description of commonly used parameters:

Example

1. Basic usage



docker run ubuntu

Pull the ubuntu image and start a container in the foreground.

** 2. Run a container in the background**



docker run -d ubuntu

Run the ubuntu container in the background and return the container ID.

** 3. Run interactively and allocate a terminal**



docker run -it ubuntu /bin/bash

Run the ubuntu container in interactive mode and start a Bash shell.

** 4. Specify a container name**



docker run --name my_container ubuntu

Run an ubuntu container and name it my_container.

** 5. Port mapping**



docker run -p 8080:80 nginx

Run an nginx container mapping the local host's port 8080 to port 80 in the container.

** 6. Mount a volume**



docker run -v /host/data:/container/data ubuntu

Mount the host's /host/data directory to the /container/data directory inside the container.

** 7. Set environment variables**



docker run -e MY_ENV_VAR=my_value ubuntu

Set the environment variable MY_ENV_VAR to my_value and run an ubuntu container.

** 8. Use a network mode**



docker run --network host nginx

Run an nginx container using the host's network mode.

** 9. Specify a restart policy**



docker run --restart always nginx

Set the container's restart policy to always so it restarts automatically even after stopping.

** 10. Specify a user**



docker run -u user123 ubuntu

Run the ubuntu container as user user123.

** 11. Combine multiple options**



docker run -d -p 8080:80 -v /host/data:/data --name webserver nginx

Run an nginx container named webserver in the background, map the host's port 8080 to port 80 in the container, and mount the host's /host/data directory to the container's /data directory.


Docker Command ReferenceDocker Command Reference