2. Docker-compose

2.1. Installation

$ apt install docker-compose

2.1.1. Error

  • If you see the following error:

$ docker build . -t myimg
free(): invalid pointer
SIGABRT: abort
PC=0x7ff7b3a55e97 m=0 sigcode=18446744073709551610
signal arrived during cgo execution

goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x4afd50, 0xc420047cc0, 0xc420047ce8)
    /usr/lib/go-1.8/src/runtime/cgocall.go:131 +0xe2 fp=0xc420047c90 sp=0xc420047c50
github.com/docker/docker-credential-helpers/secretservice._Cfunc_free(0x1681da0)
    github.com/docker/docker-credential-helpers/secretservice/_obj/_cgo_gotypes.go:111 +0x41 fp=0xc420047cc0 sp=0xc420047c90
github.com/docker/docker-credential-helpers/secretservice.Secretservice.List.func5(0x1681da0)
    /build/golang-github-docker-docker-credential-helpers-cMhSy1/golang-github-docker-docker-credential-helpers-0.5.0/obj-x86_64-linux-gnu/src/github.com/docker/docker-credential-helpers/secretservice/secretservice_linux.go:96 +0x60 fp=0xc420047cf8 sp=0xc420047cc0
github.com/docker/docker-credential-helpers/secretservice.Secretservice.List(0x0, 0x756060, 0xc420014350)
    /build/golang-github-docker-docker-credential-helpers-cMhSy1/golang-github-docker-docker-credential-helpers-0.5.0/obj-x86_64-linux-gnu/src/github.com/docker/docker-credential-helpers/secretservice/secretservice_linux.go:97 +0x217 fp=0xc420047da0 sp=0xc420047cf8
github.com/docker/docker-credential-helpers/secretservice.(*Secretservice).List(0x77e548, 0xc420047e88, 0x410022, 0xc4200142b0)
    <autogenerated>:4 +0x46 fp=0xc420047de0 sp=0xc420047da0
github.com/docker/docker-credential-helpers/credentials.List(0x756ba0, 0x77e548, 0x7560e0, 0xc42000e018, 0x0, 0x10)
    /build/golang-github-docker-docker-credential-helpers-cMhSy1/golang-github-docker-docker-credential-helpers-0.5.0/obj-x86_64-linux-gnu/src/github.com/docker/docker-credential-helpers/credentials/credentials.go:145 +0x3e fp=0xc420047e68 sp=0xc420047de0
github.com/docker/docker-credential-helpers/credentials.HandleCommand(0x756ba0, 0x77e548, 0x7ffda611c853, 0x4, 0x7560a0, 0xc42000e010, 0x7560e0, 0xc42000e018, 0x40e398, 0x4d35c0)
    /build/golang-github-docker-docker-credential-helpers-cMhSy1/golang-github-docker-docker-credential-helpers-0.5.0/obj-x86_64-linux-gnu/src/github.com/docker/docker-credential-helpers/credentials/credentials.go:60 +0x16d fp=0xc420047ed8 sp=0xc420047e68
github.com/docker/docker-credential-helpers/credentials.Serve(0x756ba0, 0x77e548)
    /build/golang-github-docker-docker-credential-helpers-cMhSy1/golang-github-docker-docker-credential-helpers-0.5.0/obj-x86_64-linux-gnu/src/github.com/docker/docker-credential-helpers/credentials/credentials.go:41 +0x1cb fp=0xc420047f58 sp=0xc420047ed8
main.main()
    /build/golang-github-docker-docker-credential-helpers-cMhSy1/golang-github-docker-docker-credential-helpers-0.5.0/secretservice/cmd/main_linux.go:9 +0x4f fp=0xc420047f88 sp=0xc420047f58
runtime.main()
    /usr/lib/go-1.8/src/runtime/proc.go:185 +0x20a fp=0xc420047fe0 sp=0xc420047f88
runtime.goexit()
    /usr/lib/go-1.8/src/runtime/asm_amd64.s:2197 +0x1 fp=0xc420047fe8 sp=0xc420047fe0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/lib/go-1.8/src/runtime/asm_amd64.s:2197 +0x1

rax    0x0
rbx    0x7ffda611c0d0
rcx    0x7ff7b3a55e97
rdx    0x0
rdi    0x2
rsi    0x7ffda611be60
rbp    0x7ffda611c1d0
rsp    0x7ffda611be60
r8     0x0
r9     0x7ffda611be60
r10    0x8
r11    0x246
r12    0x7ffda611c0d0
r13    0x1000
r14    0x0
r15    0x30
rip    0x7ff7b3a55e97
rflags 0x246
cs     0x33
fs     0x0
gs     0x0
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM alpine
 ---> cc0abc535e36
Step 2/2 : RUN ...
Removing intermediate container b130d10e7452
 ---> f6cb08eff4fd
Successfully built f6cb08eff4fd
Successfully tagged myimg:latest

Then this will resolve the issue:

$ dpkg -r --force-depends golang-docker-credential-helpers

2.2. Docker-compose Files

  • File name convention docker-compose.yaml, running:

    $ docker-compose up
    
  • Name can be any other (for example myfile.yaml), but then running

    $ docker-compose -f myfile.yaml up
    

2.3. Docker-compose workflow

Listing 26. alpine.yaml
version: '3'

services:
  alpine:
    image: alpine
    volumes:
      - /home/alpine:/data
Listing 27. Run in foreground
$ docker-compose -f alpine.yaml up
Listing 28. Run in background (daemon)
$ docker-compose -f alpine.yaml up -d

2.4. Network

version: '3'

networks:
  mynetwork:
    driver: bridge

services:
  host1:
    image: alpine
    networks:
      - mynetwork

  host2:
    image: alpine
    networks:
      - mynetwork

2.5. Examples

2.5.1. Jenkins

Listing 29. jenkins.yaml
version: '3'

networks:
  ecosystem:
    driver: bridge

services:
  jenkins:
    image: jenkins/jenkins
    container_name: jenkins
    restart: "no"
    ports:
      - "8100:8080"
    networks:
      - ecosystem
    volumes:
      - /home/jenkins:/var/jenkins_home/
      - /var/run/docker.sock:/var/run/docker.sock
$ docker-compose -f jenkins.yaml up

2.5.2. Django application

Listing 30. myapp.yaml
version: '3'

networks:
  mynetwork:
    driver: bridge

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

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    networks:
      - mynetwork
    volumes:
      - .:/srv
    ports:
      - "8000:8000"
    depends_on:
      - db
$ docker-compose -f myapp.yaml up
$ docker swarm init
$ docker stack deploy -c myapp.yml my-stack

2.6. Assignments

2.6.1. Docker Compose

  1. Ściągnij repozytorium:

  2. Zbuduj projekt / lub uruchom testy

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

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