The '
docker run' command has the following syntax:
The command '
docker run', first create the container and then start it.
Let's run the nginx image:
The option '
-d' (
--detach) run the container in background and print container ID.
Let's verify that nginx is up and running:
You can notice 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.
The second thing to notice is the name of the container (vibrant_mose) which is a random name generated by Docker.
To give a custom name to the container, you can use the option '
--name'.
Let's use the two options ('
-P', '
--name') and run again the nginx image:
Let's verify that both the specified port 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: