Install nginx locally with docker

Step: Download docker desktop

  • docker desktop helps make working with docker, credentials, pulling from registry easier
Bash
# check your docker images, containers
$ docker images # show local images 
$ docker ps # show running processes/containers 
$ docker ps --all # show running,stopped processes/containers 
  # -a, --all

Step: install nginx with “pull” and “run”

dockerhub nginx
Bash
$ docker pull nginx:1.25 # choosing a version
$ docker pull nginx # for the latest

$ docker run nginx:1.25
# docker run nginx # for the latest
# notice this blocks the terminal with logs
<ctrl-c>

# run in detach mode to run in the background
$ docker run --detach nginx:1.25
  # -d, --detach

# find the container id to see the logs
$ docker ps

# docs: https://docs.docker.com/engine/reference/commandline/container_ls/#format
# more info: https://thepatricktran.com/2024/02/08/how-to-format-filter-docker-output/
$ docker ps --format "table {{.ID}}\t{{.Image}}\{{.Ports}}"

$ docker logs <container_id>
Docker Desktop shows your running container as well

Key problem: where is nginx serving?

Bash
$ docker ps --format "table {{.ID}}\t{{.Image}}\{{.Ports}}"
>> CONTAINER ID   IMAGE                                             PORTS
>> 8bf31975364f   nginx:1.25                                        80/tcp

$ docker logs 8bf31975364f
>> ...
>> 2024/02/08 16:56:30 [notice] 1#1: using the "epoll" event method
>> 2024/02/08 16:56:30 [notice] 1#1: nginx/1.25.3
>> 2024/02/08 16:56:30 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
>> 2024/02/08 16:56:30 [notice] 1#1: OS: Linux 5.15.49-linuxkit
>> 2024/02/08 16:56:30 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
>> 2024/02/08 16:56:30 [notice] 1#1: start worker processes
>> 2024/02/08 16:56:30 [notice] 1#1: start worker process 29

“80/tcp”

  • 80/tcp indicates that the container has a port exposed internally on TCP port 80, which is the standard port for HTTP web traffic.
  • This output does not show any port mappings to the host, which would be specified in a format like host_port:container_port (for example, 8080:80).
  • The absence of such a mapping means that nginx is set to listen on port 80 inside the container, but there hasn’t been any configuration provided to map this internal port to a port on the host system therefore it’s not accessible from outside the Docker host.

Step: forward host machine requests to container

Bash
$ docker run -d -p 80:80 nginx:1.25
  # -d, --detach
  # -p, --publish
  
$ docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}"
>> CONTAINER ID   IMAGE                                             PORTS
>> 7dc3bd6fdf04   nginx:1.25                                        0.0.0.0:80->80/tcp

“0.0.0.0:80->80/tcp”

  • Now you can visit localhost:80 or 0.0.0.0:80 in your browser
  • 0.0.0.0:80 is pointing to the 80/tcp in the docker container
  • 0.0.0.0:80->80/tcp indicates that any traffic sent to the host machine on port 80 will be forwarded to port 80 of the nginx container, allowing external access to the nginx service through the host’s IP address on port 80.
nginx homepage