# IPs, Hostnames, Ports in Docker Containers

In 
Published 2022-12-03

This tutorial explains some concepts regarding IPs, hostnames, ports in Docker containers.

Here are some important things to know :

  • A container has no information about what kind of network it’s attached to (a bridge, an overlay, a macvlan network, or a custom network plugin). A container only sees a network interface with an IP address, a gateway, a routing table, DNS services, and other networking details (excepting containers which use the none network driver where there are no such information).

  • By default, when you create or run a container using docker create or docker run, the container doesn’t expose any of it's ports to the outside world. To make a port available to services outside of Docker, or to Docker containers running on a different network, use the --publish or -p flag.

Examples:

Flag value Description
-p 8080:80 Map TCP port 8080 on the Docker host to port 80 in the container.
-p 192.168.2.100:8080:80 Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.2.100.
-p 8080:80/udp Map UDP port 80 in the container to port 8080 on the Docker host.
-p 8080:80/tcp -p 8080:80/udp Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.
  • By default, the container gets an IP address for every Docker network it attaches to (excepting containers which use the none network driver). The Docker daemon effectively acts as a DHCP server for each container.

  • When a container starts, it can only attach to a single network, using the --network flag. You can connect a running container to multiple networks using the docker network connect command.

  • When you use the --network flag, you can specify the IP address for the container on that network using the --ip or --ip6 flags.

  • You can override the hostname using --hostname.

For instance, you can run the following command:

linux
docker run -d --name app1 -p 8080:80 --hostname app1  httpd:latest

You will see that an app1 container is created, and its hostname is app1.

The container use the port 80 which is mapped to Docker's port 8080.