Docker run Command
The docker run command is used to create and start a new container.
Syntax
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Common parameter description:
-dRun the container in the background and return the container ID.-itRun the container interactively and allocate a pseudo-TTY.--nameAssign a name to the container.-pPort mapping, format ishost_port:container_port。-vVolume mount, format ishost_dir:container_dir。--rmAutomatically remove the container after it stops.--envor-eSet environment variables.--networkSpecify the container's network mode.--restartContainer restart policy (e.g.,no、on-failure、always、unless-stopped)。-u: Specify user.
Example
1. Basic usage
docker run ubuntu
Pull the ubuntu image and start a container in the foreground.
2. Run container in background
docker run -d ubuntu
Run an ubuntu container in the background and return the container ID.
3. Interactive run with terminal allocation
docker run -it ubuntu /bin/bash
Run the ubuntu container in interactive mode and start a Bash shell.
4. Specify the 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 host's 8080 port to the container's 80 port.
6. Mount volume
docker run -v /host/data:/container/data ubuntu
Mount the host's /host/data directory to the container's /container/data directory.
7. Set environment variables
docker run -e MY_ENV_VAR=my_value ubuntu
Run an ubuntu container with the environment variable MY_ENV_VAR set to my_value.
8. Use a network mode
docker run --network host nginx
Run an nginx container using the host's network mode.
9. Specify the restart policy
docker run --restart always nginx
Set the container's restart policy to always, so it will automatically restart even if the container stops.
10. Specify the 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 8080 port to the container's 80 port, and mount the host's /host/data directory to the container's /data directory.
other extensions