#
IPs, Hostnames, Ports in Docker Containers
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
ordocker 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:
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 thedocker 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:
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.