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!
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