Docker Compose Networking
In this article, we will learn about Docker Compose Networking.
1. Introduction
Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.
Docker works by providing a standard way to run your code. Docker is an operating system for containers. Similar to how a virtual machine virtualizes (removes the need to directly manage) server hardware, containers virtualize the operating system of a server. Docker is installed on each server and provides simple commands you can use to build, start, or stop containers.
2. Docker compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Compose works in all environments: production, staging, development, testing, as well as CI workflows. It also has commands for managing the whole lifecycle of your application: Start, stop, and rebuild services, view the status of running services, Stream the log output of running services, Run a one-off command on a service.
3. Docker Networking
The ability to connect Docker services and containers to one another as well as to workloads from other platforms makes them extremely powerful. It is not even necessary for docker compose networking for Docker services and containers to be aware that they are running on Docker or that their peers are Docker workloads or not. You may use Docker to manage your Docker hosts in a platform-neutral manner, regardless of whether they run Windows, Linux, or a combination of the two.
Compose creates a single network by default for your project. Each container for a service joins the default network and can be found at a hostname that is the same as the container name, making it both reachable and discoverable by other containers on that network.
The “project name,” which is based on the name of the directory your app resides in, is used to identify your app’s network. The COMPOSE_PROJECT_NAME
environment variable or the --project-name
flag both allows you to change the project name.
For example, suppose your app is in a directory called jcg-example
, and your docker-compose.yml
looks like this:
version: "1.1"
services:docker compose networking
web:
build: .
ports:
- "3333:8000"
db:
image: postgres
ports:
- "3334:5432"
Below are the things which will happen when we run docker compose up
- A network by the name
jcg-example_default
is created. - A container is created using
web
’s configuration. It joins the networkjcg-example_default
under the nameweb
. - A container is created using
db’s
configuration. It joins the networkjcg-example_default
under the namedb
Each container can now search up the IP address of the relevant container by using the hostnames web or db. It’s crucial to understand the difference between CONTAINER_PORT
and HOST_PORT
. The container port in the example above is 5432
(postgres default), and the host port for db
is 3334
. The CONTAINER_PORT
is used for networked service-to-service communication. The service is usable outside of the swarm if HOST_PORT
is configured. Within the web
container, your connection string to db
would look like postgres://db:5432
, and from the host machine, the connection string would look like postgres://{DOCKER_IP}:3334
4. Custom Networks
You can specify your own networks with the top-level networks
key in addition to using the default app network. This enables you to specify unique network drivers and parameters and design more complicated topologies. It can also be used to link services to networks that were built by third parties and are not under Compose’s control. The service-level networks
key, which is a list of names referencing entries under the top-level networks
key, allows each service to select which networks to connect to. Below is an example to define custom networks:
version: "3.9"
services:
cache:
networks:
- frontend
app:
networks:
- frontend
- backend
db:
networks:
- backend
networks:
frontend:
driver: custom-driver-fe
backend:
driver: custom-driver-be
The ipv4 address and/or ipv6 address for each attached network can be set to provide static IP addresses for networks. Use the external option if you want your containers to join an existing network:
services:
# ...
networks:
default:
name: existing-network
external: true
5. Conclusion
In this short article, we learned about Docker and how it differs from a Virtual Machine (VM). We also looked at how to create new networks. We looked at using existing networks and how to create new ones.