Docker run Command
docker run: Create a new container and run a command
Syntax
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS Description:
-
-a stdin: Specify the standard input/output content type;可选 STDIN/STDOUT/STDERR 三项;
-
-d: Run container in the background and return the container ID;
-
-i: Run the container in interactive mode, usually used with -t;
-
-P: Random port mapping, map the container's internal ports to random ports on the host;
-
-p: Specify port mapping, format: host (host) port: container port
;
-
-t: Reassign a pseudo-input terminal for the container, usually used with -i;
-
--name="nginx-lb": Specify a name for the container;
-
--dns 8.8.8.8: Specify the DNS server used by the container, defaults to the host's DNS settings;
-
--dns-search example.com: Specify the DNS search domain for the container, defaults to the host's settings;
-
-h "mars": Specify the container's hostname;
-
-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 usage for the container;
-
--net="bridge": Specify the network connection type for the container, supports bridge/host/none/container;
-
--link=[]: Add a link to another container;
-
--expose=[]: Expose a port or a range of ports;
-
--volume, -v: Bind a volume
Examples
Start a container in the background using the nginx:latest image and name it mynginx.
docker run --name mynginx -d nginx:latest
Start a container in the background using the nginx:latest image and map the container's port 80 to a random port on the host.
docker run -P -d nginx:latest
Start a container in the background using the nginx:latest image, map the container's port 80 to the host's port 80, and map the host's directory /data to the container's /data.
docker run -p 80:80 -v /data:/data -d nginx:latest
Bind the container's port 8080 and map it to port 80 on localhost.
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
Start a container in interactive mode using the nginx:latest image and execute the /bin/bash command inside the container.
tutorialpro@tutorialpro:~$ docker run -it nginx:latest /bin/bash
root@b8573233d675:/#