9. Docker-compose

9.1. Docker-compose workflow

  • File name convention docker-compose.yaml

  • Name can be any other, but then docker-compose -f FILENAME.yaml command

  1. Create file

    Listing 9.1. docker-compose.yaml
    version: '3'
    
    services:
      alpine:
        image: alpine
        volumes:
          - /home/alpine:/data
    
  2. Run

    $ docker-compose up
    
  3. Run in background (daemon)

    $ docker-compose up -d
    

9.2. Network

version: '3'

networks:
  mynetwork:
    driver: bridge

services:
  host1:
    image: alpine
    networks:
      - mynetwork

  host2:
    image: alpine
    networks:
      - mynetwork

9.3. Examples

9.3.1. Jenkins

  1. Create file jenkins.yaml

    Listing 9.2. jenkins.yaml
    version: '3'
    
    networks:
      mynetwork:
        driver: bridge
    
    services:
      jenkins:
        image: jenkins/jenkins
        container_name: jenkins
        restart: "no"
        ports:
          - "8080:8080"
        networks:
          - mynetwork
        volumes:
          - /home/jenkins:/var/jenkins_home/
          - /var/run/docker.sock:/var/run/docker.sock
    
  2. Run Jenkins in background (daemon)

    $ docker-compose up -d
    

9.3.2. Django application

Listing 9.3. docker-compose.yaml
version: '3'

services:
  db:
    image: postgres
    ports:
      - "5432:5432"

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/www
    ports:
      - "8000:8000"
    depends_on:
      - db
$ docker-compose up -d
$ docker swarm init
$ docker stack deploy -c docker-compose.yml my-stack

9.3.3. CI/CD ecosystem

version: '3'

networks:
  ecosystem:
    driver: bridge

services:
  jenkins:
    image: jenkins/jenkins
    container_name: jenkins
    restart: always
    ports:
      - "8080:8080"
    networks:
      - ecosystem
    volumes:
      - /home/jenkins:/var/jenkins_home/
    depends_on:
      - sonar
      - gitlab
      - artifactory
    environment:
      - SONAR_PORT=9000

  sonar:
    image: sonarqube
    container_name: sonarqube
    restart: always
    ports:
     - "9000:9000"
     - "9092:9092"
    networks:
      - ecosystem

  gitlab:
    image: gitlab/gitlab-ce:latest
    container_name: gitlab
    restart: always
    volumes:
      - /home/gitlab/config:/etc/gitlab
      - /home/gitlab/logs:/var/log/gitlab
      - /home/gitlab/data:/var/opt/gitlab
    ports:
     - "443:443"
     - "80:80"
     - "2222:22"
    networks:
      - ecosystem

  artifactory:
    image: docker.bintray.io/jfrog/artifactory-oss:latest
    container_name: artifactory
    restart: always
    ports:
      - "8081:8081"
    networks:
      - ecosystem
$ docker-compose up -d

9.4. Assignments

9.4.1. Docker Compose

  1. Ściągnij repozytorium https://github.com/AstroTech/ecosystem-example-java

  2. Zbuduj projekt za pomocą mvn install

  3. Przygotuj obraz oraz uruchom aplikację wykorzystując Docker

  4. Użyj pliku docker-compose.yaml do opisu środowiska kontenera