Making network services available in Docker

Intermediate Docker

Mike Metzger

Data Engineering Consultant

Network services

  • Network services listen on a given port
  • Only one program can listen on an IP:port combo at a given time
    • For example, 10.1.2.3:80 would be listening on 10.1.2.3 on port 80.
  • Consider trying to debug different versions of a web server that listens on port 80
    • Could only run one copy of the application at a time given that it listens only on that port
Intermediate Docker

Containerized services

  • Wrapping application in a container means that each container can now listen on that port (as the IP:port combo is different, each container has a different IP)
  • Can have multiple copies of the containers running at once
  • But how to connect to container's version of application from the host?
Intermediate Docker

Port mapping

  • The answer is the use of port mapping, or port forwarding / translation
  • Port mapping takes a connection to a given IP:port and automatically forwards it to another IP:port combo
  • In this case, we could map an unused port on our host and point it to port 80 on the container(s)
  • The Docker engine can handle this automatically if we configure it to

Port mapping example

Intermediate Docker

Enabling port mapping

  • To enable port mapping on a given container, we use the docker run command, and the -p flag
  • -p <host port>:<container port>
  • -p 5501:80
  • Can have multiple -p flags for different ports
repl@host:~$ docker run -p 5501:80 nginx

repl@host:~$ docker ps -a
CONTAINER ID IMAGE ... PORTS NAMES 84266724ff47 nginx ... 0.0.0.0:5501->80/tcp, :::5501->80/tcp coiled_elgamal
Intermediate Docker

Let's practice!

Intermediate Docker

Preparing Video For Download...