Docker Run Commands

1. About

docker run : create a new container and run a command

2. Grammar

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

3, parameter description

  • -a stdin: Specifies the standard input and output content type, optional STDIN/STDOUT/STDERR three items;
  • -d: Run the container in the background and return the container ID;
  • -i: Run the container in interactive mode, usually used together with -t;
  • -P: Random port mapping, the internal port of the container is randomly mapped to the port of the host
  • -p: Specify port mapping, the format is: host (host) port: container port
  • -t: Reassign a pseudo-input terminal for the container, usually used together with -i;
  • --name="nginx-lb": specify a name for the container;
  • --dns 8.8.8.8: Specify the DNS server used by the container, which is the same as the host by default;
  • --dns-search example.com: Specifies the container DNS search domain name, which is the same as the host by default;
  • -h "mars": specify the hostname of the container;
  • -e username="ritchie": set environment variables;
  • --env-file=[]: read environment variables from the specified file;
  • --cpuset="0-2" or --cpuset="0,1,2": Bind the container to the specified CPU to run;
  • -m : Set the maximum memory used by the container;
  • --net="bridge": Specify the network connection type of the container, support bridge/host/none/container four types;
  • --link=[]: add a link to another container;
  • --expose=[]: expose a port or a group of ports;
  • --volume , -v: bind a volume

4. Examples

Use the Docker image nginx:latest to start a container in background mode, and name the container mynginx.

Bash
1
docker run --name mynginx -d nginx:latest

Use the image nginx:latest to start a container in background mode, and map the 80 port of the container to a random port on the host.

Bash
1
docker run -P -d nginx:latest

Use the image nginx:latest to start a container in background mode, map the 80 port of the container to the 80 port of the host, and map the /data directory of the host to the /data of the container.

Bash
1
docker run -p 80:80 -v /data:/data -d nginx:latest

Bind container port 8080 and map it to 80 port on localhost 127.0.0.1.

Bash
1
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

Use the image nginx:latest to start a container in interactive mode, and execute the /bin/bash command in the container.

Bash
1
2
runoob@runoob:~$ docker run -it nginx:latest /bin/bash
root@b8573233d675:/#

refer to

Docker run command
https://www.runoob.com/docker/docker-run-command.html
Author
Hsukqi Lee
Posted on

2023-05-21

Edited on

2023-05-21

Licensed under

CC BY-NC-ND 4.0

Comments

Name
Mail
Site
None yet