Docker: run nginx in a container
Pretty simple to run nginx container
$ sudo docker container run --detach --publish 8080:80 --name nginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
852e50cd189d: Pull complete
571d7e852307: Pull complete
addb10abd9cb: Pull complete
d20aa7ccdb77: Pull complete
8b03f1e11359: Pull complete
Digest: sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
Status: Downloaded newer image for nginx:latest
9fd210ce59f2965b5553ccfdb00201b0fa62b7f012d61d5464805a3fceb15f32
Then check your local host port 8080

NGINX is a lightweight web server. You can access it on port 8080 on your localhost.
You are using a couple of new flags here. The --detach
flag will run this container in the background. The publish
flag publishes port 80 in the container (the default port for NGINX) by using port 8080 on your host. Remember that the NET namespace gives processes of the container their own network stack. The --publish
flag is a feature that can expose networking through the container onto the host.
How do you know port 80 is the default port for NGINX? Because it is listed in the documentation on the Docker Store. In general, the documentation for the verified images is very good, and you will want to refer to it when you run containers using those images.
You are also specifying the --name
flag, which names the container. Every container has a name. If you don’t specify one, Docker will randomly assign one for you. Specifying your own name makes it easier to run subsequent commands on your container because you can reference the name instead of the id of the container. For example, you can specify docker container inspect nginx
instead of docker container inspect 5e1
.
Because this is the first time you are running the NGINX container, it will pull down the NGINX image from the Docker Store. Subsequent containers created from the NGINX image will use the existing image located on your host.
If you want to create several instances for testing a larger scale deployment try this:
$ for x in 4 5 6 7 8 9 ; do sudo docker container run --detach --publish 808$x:80 --name nginx$x nginx ; done
d983cc12e2742ab2e44b675640c92b66cf8ee86f880ef04c26804b90e5c027e1
9a90203fa4253aef3b8df738f182d55d54bcae6a8054852e5fdbc714dbb94358
42626e2ebff300fa35bb9afcb146a9587f86e2ca8c80d8558ab73cf869e0f5e3
docker: Error response from daemon: driver failed programming external connectivity on endpoint nginx6 (5aa4d97abeb434d48eeb1610385aaee333d6e935f0d6fe9f6e7dece5ee040558): Error starting userland proxy: listen tcp 0.0.0.0:8086: bind: address already in use.
b3629ef9655ab89711e0951d8978405ff558736f5383f24388b75622d7270349
c74772c5f0e34d62fa1da7ca1426a07a019816bc111b3aea4a203cb9dedcf81a
docker: Error response from daemon: driver failed programming external connectivity on endpoint nginx8 (d5f724bc2435a701a4e5f9a4524530f3eeecf2da25d9a33b8296705f12d1abf2): Error starting userland proxy: listen tcp 0.0.0.0:8088: bind: address already in use.
55f264b68a900654de52bd73da6768be921af66b10cc569c45a558967325398c
$ sudo docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55f264b68a90 nginx "/docker-entrypoint.…" 19 seconds ago Up 16 seconds 0.0.0.0:8089->80/tcp nginx9
b3629ef9655a nginx "/docker-entrypoint.…" 23 seconds ago Up 20 seconds 0.0.0.0:8087->80/tcp nginx7
9a90203fa425 nginx "/docker-entrypoint.…" 27 seconds ago Up 24 seconds 0.0.0.0:8085->80/tcp nginx5
d983cc12e274 nginx "/docker-entrypoint.…" 28 seconds ago Up 26 seconds 0.0.0.0:8084->80/tcp nginx4
d0ed6519620c nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:8083->80/tcp nginx3
0f133e21af02 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:8082->80/tcp nginx2
5afdcc0e758e nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:8080->80/tcp nginx
b88627f05581 mongo:3.4 "docker-entrypoint.s…" 33 minutes ago Up 32 minutes 0.0.0.0:8081->27017/tcp mongo