Docker run command - Create a new container and run a command

Docker run command

docker run : Create a new container and run a command

Docker run Syntax

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

OPTIONS Description:

  • -a stdin: specifies the standard input/output content type, with three options STDIN/STDOUT/STDERR.
  • -d: Runs the container in the background and returns the container ID.
  • -i: Runs the container in interactive mode, usually used in conjunction with -t.
  • -p: random port mapping, where the container’s internal port is randomly mapped to the host’s port
  • -p: specifies port mapping in the format: host (host) port:container port
  • -t: reassign a pseudo-input terminal to the container, usually used in conjunction with -i.
  • –name="nginx-lb": Assign a name to the container.
  • –dns 8.8.8.8: Specify the DNS server used by the container, which by default is the same as the host.
  • –dns-search example.com: Specify a DNS search domain name for the container, the default is the same as the host.
  • -h "mars": Specifies the hostname of the container.
  • -e username="ritchie": Set environment variables.
  • –env-file=[]: Read in environment variables from the specified file.
  • –cpuset="0-2" or –cpuset="0,1,2": Bind the container to run on the specified CPU.
  • -m : Set the maximum value of memory used by the container.
  • –net="bridge": Specify the type of network connection for the container, supporting bridge/host/none/container: four types.
  • –link=[]: Adding a link to another container.
  • –expose=[]: open a port or set of ports.
  • –volume , -v: bind a volume

Docker run Example

Start a container in backend mode using the docker image nginx:latest, and name the container mynginx.

docker run --name mynginx -d nginx:latest

Use the image nginx:latest to start a container in backend mode and map the container’s port 80 to a random port on the host.

docker run -P -d nginx:latest

Use the image nginx:latest to start a container in backend mode, mapping the container’s port 80 to the host’s port 80 and the host’s directory /data to the container’s /data.

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

Bind the container to port 8080 and map it to port 80 on local host 127.0.0.1.

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

Start a container in interactive mode using the image nginx:latest, and execute the /bin/bash command inside the container.

apidemos@apidemos:~$ docker run -it nginx:latest /bin/bash
root@b8573233d675:/# 
Like(1)