Skip to content

Simple Networking with Docker-Compose

It's one thing to create networks manually, but another thing to use them inside docker-compose. In this lecture I want to create a simple network and use it in docker-compose. Let's dive right in and see how that works!

View the Full Course Now

Understanding, creating and using networks in docker-compose.yml

This exercise is here to showcase a typical network configuration and segregation using docker-compose.yml. There are service-level and top-level network definitions. The service level is for "which network does this service join". The top-level for "how does the network look like". Let's use this docker-compose.yml file:

version: "3.7"

services:

  app1:
    image: httpd:latest
    container_name: app1
    ports:
     - 8080:80
    networks:
      - app1_net

networks:
  app1_net:
* Creates one network called "app1_net" * top-level, last line, without any further configuration. This will be a bridge network. * Attaches container app1 to the network

docker-compose up
  • Starts the container

Open http://localhost:8080 in a browser and bbserve the output. Then in a second Terminal window get the docker container infos:

docker ps
  • Get the currently running containers
docker inspect app1
  • Observe the networking part!

Close the second terminal with exit

Back to terminal 1:

ctrl+c
  • Stops the docker-compose
docker-compose down
  • cleanup