The '
docker run' command has the following syntax:
Note that
docker container run is equivalent to
docker run
The command '
docker run', first creates the container (from the specified image) and then starts it.
Let's run the nginx image:
The option '
-d' (
--detach) run the container in background and print container ID.
The option '
--rm' tells Docker to automatically remove the container when it exits.
Let's verify that nginx is up and running:
You can notice that the name of the container (vibrant_mose) is a random name generated by Docker.
To give a custom name to the container, you can use the option '
--name'.
Note that the container's port (80) is not published to the Docker's host.
To publish an exposed container's port and map it to a host's port, you can use the option '
-p HOST_PORT:CONTAINER_PORT'.
You can also use the option '
-P' to publish all exposed ports to random ports in the Docker's host.
Let's use the two options ('
-p', '
--name') and run again the nginx image:
Let's verify that both the specified port (the container port 80 is mapped to the Docker host port 8080) and the name were used:
You can use the option '
--no-trunc' of the '
ps' command to print the full container ID and the command used to start the nginx process.
Let's access nginx at the published port 8080: