notes-api
application powered by Express.js and a PostgreSQL database server running in two separate containers.postgres
container and the notes-api
will connect through that. Assuming that the exposed port from the postgres
container is 5432. Now if you try to connect to 127.0.0.1:5432
from inside the notes-api
container, you'll find that the notes-api
can't find the database server at all.127.0.0.1
inside the notes-api
container, you're simply referring to the localhost
of that container and that container only. The postgres
server simply doesn't exist there. As a result the notes-api
application failed to connect.postgres
container using container inspect
command and use that with the port. Assuming the name of the postgres
container is notes-api-db-server
you can easily get the IP address by executing the following command:postgres
is 5432
, you can very easily access the database server by connecting to 172.17.0.2:5432
from the notes-api
container.docker network
group for manipulating networks. To list out the networks in your system, execute the following command:DRIVER
column of the table here. These drivers are can be treated as the type of network. By default, Docker has five networking drivers. They are as follows:bridge
- The default networking driver in Docker. This can be used when multiple containers are running in standard mode and needs to communicate with each other.host
- Removes the network isolation completely. Any container running under a host
network is basically attached to the network of the host system.none
- This driver disables networking for containers altogether. I haven't' found any use-case for this yet.overlay
- This is used for connecting multiple Docker daemons across computers and is out of the scope of this article.macvlan
- Allows assignment of MAC addresses to containers making them function like physical devices in a network.bridge
networking driver in this article.bridge
. Any container you run will be automatically attached to this bridge network:notes-api
and notes-db
the API container will be able to connect to the database container using the notes-db
name.network create
command. The generic syntax for the command is as follows:skynet
execute the following command:network connect
command to attach a container to a network. The generic syntax for the command is as follows:hello-dock
container to the skynet
network, you can execute the following command:network inspect
commands, the hello-dock
container is now attached to both the skynet
and the default bridge
network.--network
option for container run
or container create
commands. The generic syntax for the option is as follows:hello-dock
container to the attached to the same network, you can execute the following command:ping hello-dock
from inside the alpine-box
container works because both of the containers are under the same user-defined bridge network and automatic DNS resolution is working.network disconnect
command can be used for this task. The generic syntax for the command is as follows:hello-dock
container from the skynet
network, you can execute the following command:network connect
command, the network disconnect
command doesn't give any output either.network rm
command. The generic syntax for the command is as follows:skynet
network from your system, you can execute the following command:network prune
command to remove any unused networks from your system. The command also has the -f
or --force
and -a
or --all
options.