In this short tutorial, we’ll show you through a simple example how to use docker compose to run your first container.

To do so, we’ll create a service for “Traefik”, which is is an open-source Edge Router that makes publishing your services a fun and easy experience.

Prerequisites

We suppose that you have docker and docker-compose installed on linux ubuntu.

@see docker and docker compose documentation.

Create compose file

  • Create a new workspace (eg: /home/work/docker)
  • Create a new file “docker-compose.yml”

Using cat command:

Type the command, press the “Enter” key to execute it, then press “Ctrl-C” to stop it.

$ cat > docker-compose.yml

PS: if you type anything before “Ctrl-C” it will be written in the file.

Edit compose file

  • Edit the file by adding the following content:
version: '3.7'

services:
  traefik:
    image: traefik:v2.1
    command: --api.insecure=true --providers.docker=true --providers.docker.swarmMode=false --log.level=DEBUG
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - public_oitc
    ports:
      - "90:80"
      - "8777:8080"

#### configs, volumes, networks
networks:
  public_oitc:

You can use vim text editor by typing the following command:

$ vi docker-compose.yml

To install the vim text editor use the following commands:

$ sudo apt-get update

$ sudo apt-get install vim

Run the container

  • Go to your file location and type the following command:

$ docker-compose up

Use “-f” option to specify an alternate compose file (default: docker-compose.yml)

$ docker-compose -f <file_name.yml> up

Type the following command to stop the container:

$ docker-compose -f <file_name.yml> down

List containers

  • Type the following command to list the containers:

$ docker-compose -f <file_name.yml> ps

  • Open traefik in your brower:

http://<host>:8777

Traefik – using labels

  • Use traefik labels to make URL user-friendly:
version: '3.7'

services:
  traefik:
    image: traefik:v2.1
    command: --api.insecure=true --providers.docker=true --providers.docker.swarmMode=false --log.level=DEBUG
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - public_oitc
    ports:
      - "80:80"
      - "8888:8080"
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=public_oitc"
      - "traefik.http.routers.traefik.service=traefik"
      - "traefik.http.routers.traefik.rule=Host(`traefik.infra.ubu`)"
      - "traefik.http.services.traefik.loadbalancer.server.port=8080"

#### configs, volumes, networks
networks:
  public_oitc:
  • Add the url “traefik.infra.ubu” to your hosts file.

Windows: C:/Windows/System32/drivers/etc/hosts

Linux: /etc/hosts

@see Traefik documentation for more details.

Enjoy!

Avatar

Ramzi is a Solutions Architect, He writes about technology, coding best practices, leadership and various topics..

Leave a comment

Your email address will not be published. Required fields are marked *