Lab Environment Setup Using Docker Compose

In this manual, we will use Compose to create a lab environment setup that consists of a LAN and containers. The setup is depicted in the following Figure.

Writing the docker-compose.yml file

With Compose, we use a YAML file called docker-compose.yml to configure our containers. Then, with a single command, we can create and start all the containers from your configuration. The following is an example of the file.

version: "3"

services:
    HostA:
        build: ./ubuntu_base
        image: seed-ubuntu-base-image
        container_name: host-10.9.0.5
        tty: true
        cap_add:
                - ALL
        networks:
            net-10.9.0.0:
                ipv4_address: 10.9.0.5

    HostB:
        image: seed-ubuntu-base-image
        container_name: host-10.9.0.6
        tty: true
        cap_add:
                - ALL
        networks:
            net-10.9.0.0:
                ipv4_address: 10.9.0.6

    HostC:
        image: seed-ubuntu-base-image
        container_name: host-10.9.0.7
        tty: true
        cap_add:
                - ALL
        networks:
            net-10.9.0.0:
                ipv4_address: 10.9.0.7

networks:
    net-10.9.0.0:
        name: net-10.9.0.0
        ipam:
            config:
                - subnet: 10.9.0.0/24

Let us explain explain how it works.

Note: When Docker creates a network, it automatically attaches the host machine (i.e., the VM) to the network, and gives the .1 as its IP address. Namely, for the 10.9.0.0/24 network, the host machine's IP address is 10.9.0.1. Therefore, the host machine can directly communicate with all the containers.