Skip to content

Instantly share code, notes, and snippets.

@patharanordev
Created January 8, 2022 09:57
Show Gist options
  • Save patharanordev/1d4ca2d3a63bb00a93daa45098c3719c to your computer and use it in GitHub Desktop.
Save patharanordev/1d4ca2d3a63bb00a93daa45098c3719c to your computer and use it in GitHub Desktop.
Terminate the other container after main container run finished by using docker-compose

Terminate the other container after main container run finished by using docker-compose

Example : I would like to terminate node and filebeat service after test service run finished or exit code is zero (success). We need to create shell script and adding two docker-compose up's options, it's --abort-on-container-exit and --exit-code-from (the shell script below related with docker-container.yml)

shell-scrip.sh

( (docker-compose -f docker-compose.yml down -v) || : ) && \
docker-compose -f docker-compose.yml up --build \
--abort-on-container-exit \
--exit-code-from test && \
docker-compose -f docker-compose.yml down -v

docker-container.yml

version: '3'
services:

  node:
    build:
      context: ./
      dockerfile: Dockerfile
    depends_on:
      - filebeat
    ports:
      - '8002:8002'
    command: bash /build-script/run-local.sh

  filebeat:
    image: docker.elastic.co/beats/filebeat:7.5.0
    volumes:
      - ./ag/logs:/logs
    command: bash -c "(chmod go-w /logs/filebeat.yml) && (bash /logs/start.sh)"

  test:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on: 
      - node
    command: /bin/sh something.sh

networks:
  default:
    driver: bridge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment