Adam O'Grady

Multiple Containers With docker-compose

For my game project, I want to have multiple Docker containers started up at once which are all linked together by easy-to-use hostnames (not terrible auto-generated Docker hashes).

The solution to this arrived to me pretty easily thanks to my friend @will2bill on Twitter who pointed out that Docker Compose is a thing! By declaring a YAML manifest file called docker-compose.yml and filling it appropriately you can have a series of linked containers come up easily with a single command, sudo docker-compose up -d. I’ve included an example docker-compose.yml file directly below:

version: '3'
services:
  gateway:
    build: ./gateway
    ports:
      - "2222:22"
  another:
    build: ./anyway

As you can see, we define the version of docker-compose manifest first, then we define the services/containers that we’ll want. In this case, we’re creating two containers, one called gateway and the other called another. They’ll both be built individually and locally from Dockerfiles located in their relevant directories, however you can also specify an image property with a Docker image:tag rather than using the build keyword. You’ll also notice the ports array, which in this case maps port 2222 on the host to 22 on the guest container called gateway and allows us to SSH in from our host to the guest. Once we’re inside the guest, we can simply SSH to the other box with ssh user@another, making it super easy for us to get around.

This is exactly what I needed for the problem, however docker-compose has some different features from docker and so the next challenge will be getting multiple docker-compose orchestrated container groups loaded, one per each SSH connection from a user.