Skip to content

Docker Containers

A Docker container is a runnable instance of an image. Containers are relatively well isolated from both other containers and the host machine by default. However, they can be connected to one or more Docker networks, or have Docker volumes attached to them to give them persisting memory.

To list containers running on a machine, we can run:

Text Only
docker ps

After opening the Docker daemon, which is done by opening the application that you downloaded from https://www.docker.com, we can start containers with Docker in the terminal. To run a container from Docker, run:

Text Only
docker run [options] {container} {command}

Docker contains a pre-made container which contains a webpage tutorial to help you get familiar with Docker. To start this tutorial, open up your terminal and run:

Text Only
docker run -d -p 80:80 docker/getting-started

This command will run the docker image called docker/getting-started. Since this image is not on the local machine, it is downloaded from DockerHub. The flag -d detatches the command from the terminal so that the prompt is returned to us. Finally, -p 80:80 maps the container's ports to the host's. To view the webpage that it is now available, open a browser, and navigate to 'localhost:80'. From here you can follow along with the tutorial.

The docker-compose File

Another way to create a group of Docker containers from Docker images is to use a docker-compose.yaml file. These files allow you to define instructions for starting containers from images with certain commands, names, volumes, and more.

Here is an example:

YAML
version: '3.7'

services:
  frb-master:
    image: chimefrb/frb-master:latest
    container_name: frb_master
    environment:
      - DEBUG=1
      - FRB_MASTER_USERNAME=debug
      - FRB_MASTER_PASSWORD=debug
    volumes:
      - .:/frb-master
    ports:
      - "8001:8001"
    networks:
      - frb_master_network

  frb-distributor:
    image: chimefrb/frb-master:latest
    container_name: frb_distributor
    environment:
      - DEBUG=1
    command: poetry run python distributor.py
    volumes:
      - .:/frb-master
    ports:
      - "8002:8002"
    networks:
      - frb_master_network

  mongodb:
    image: mongo
    container_name: mongo
    command: mongod
    ports:
      - "27017:27017"
      - "27018:27018"
      - "27019:27019"
    networks:
      - frb_master_network

networks:
  frb_master_network:

In this example, we are setting instructions to create three containers: frb-master, frb-distributor, and mongodb. These three services can communicate with each other through the defined frb_master_network (more about Docker Networks here). Most arguments that you can provide with docker run can simply be provided here in a YAML configuration.

You can now run docker compose up to start the containers if the docker-compose.yaml file is in your current directory (otherwise docker compose -f location/of/file.yaml up). To stop these containers, you can write docker compose down.