Skip to Content
CoursesLearn DockerUnderstand Networking in Docker-Compose

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
Last updated on