title: Docker-compose exit code is 137 when there is no OOM exception
body: <p>When I run the following command, I expect the exit code to be 0 since my <code>combined</code> container runs a test that successfully exits with an exit code of 0.</p>
  
  <pre><code>docker-compose up --build --exit-code-from combined
  </code></pre>
  
  <p>Unfortunately, I consistently receive an exit code of 137 even when the tests in my <code>combined</code> container run successfully and I exit that container with an exit code of 0 (more details on how that happens are specified below).</p>
  
  <p>Below is my docker-compose version:</p>
  
  <pre><code>docker-compose version 1.25.0, build 0a186604
  </code></pre>
  
  <p>According to this <a href="https://success.docker.com/article/what-causes-a-container-to-exit-with-code-137" rel="noreferrer">post</a>, the exit code of 137 can be due to two main issues.</p>
  
  <ol>
  <li>The container received a <code>docker stop</code> and the app is not gracefully handling SIGTERM</li>
  <li>The container has run out of memory (OOM).</li>
  </ol>
  
  <p><em>I know the 137 exit code is not because my container has run out of memory.</em> When I run <code>docker inspect &lt;container-id&gt;</code>, I can see that "OOMKilled" is false as shown in the snippet below. I also have 6GB of memory allocated to the Docker Engine which is plenty for my application.</p>
  
  <pre><code>[
      {
          "Id": "db4a48c8e4bab69edff479b59d7697362762a8083db2b2088c58945fcb005625",
          "Created": "2019-12-12T01:43:16.9813461Z",
          "Path": "/scripts/init.sh",
          "Args": [],
          "State": {
              "Status": "exited",
              "Running": false,
              "Paused": false,
              "Restarting": false,
              "OOMKilled": false, &lt;---- shows container did not run out of memory
              "Dead": false,
              "Pid": 0,
              "ExitCode": 137,
              "Error": "",
              "StartedAt": "2019-12-12T01:44:01.346592Z",
              "FinishedAt": "2019-12-12T01:44:11.5407553Z"
          },
  </code></pre>
  
  <p><em>My container doesn't exit from a <code>docker stop</code> so I don't think the first reason is relevant to my situation either.</em></p>
  
  <p><strong>How my Docker containers are set up</strong></p>
  
  <p>I have two Docker containers:</p>
  
  <ol>
  <li><strong>b-db</strong> - contains my database</li>
  <li><strong>b-combined</strong> - contains my web application and a series of tests, which run once the container is up and running.</li>
  </ol>
  
  <p>I'm using a docker-compose.yml file to start both containers.</p>
  
  <pre><code>version: '3'
  services:
      db:
          build:
              context: .
              dockerfile: ./docker/db/Dockerfile
          container_name: b-db
          restart: unless-stopped
          volumes:     
              - dbdata:/data/db
          ports:
              - "27017:27017"
          networks:
              - app-network
  
      combined:
          build:
              context: .
              dockerfile: ./docker/combined/Dockerfile
          container_name: b-combined
          restart: unless-stopped
          env_file: .env
          ports:
              - "5000:5000"
              - "8080:8080"
          networks:
              - app-network
          depends_on:
              - db
  
  networks:
      app-network:
          driver: bridge
  
  volumes:
      dbdata:
      node_modules:
  </code></pre>
  
  <p>Below is the Dockerfile for the <code>combined</code> service in <code>docker-compose.yml</code>.</p>
  
  <pre><code>FROM cypress/included:3.4.1
  
  WORKDIR /usr/src/app
  
  COPY package*.json ./
  
  RUN npm install
  
  COPY . .
  
  EXPOSE 5000
  
  RUN npm install -g history-server nodemon
  
  RUN npm run build-test
  
  EXPOSE 8080
  
  COPY ./docker/combined/init.sh /scripts/init.sh
  
  RUN ["chmod", "+x", "/scripts/init.sh"]
  
  ENTRYPOINT [ "/scripts/init.sh" ]
  </code></pre>
  
  <p>Below is what is in my <code>init.sh</code> file.</p>
  
  <pre><code>#!/bin/bash
  # Start front end server
  history-server dist -p 8080 &amp;
  front_pid=$!
  
  # Start back end server that interacts with DB
  nodemon -L server &amp;
  back_pid=$!
  
  # Run tests
  NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome
  
  # Error code of the test
  test_exit_code=$?
  
  echo "TEST ENDED WITH EXIT CODE OF: $test_exit_code"
  
  # End front and backend server
  kill -9 $front_pid
  kill -9 $back_pid
  
  # Exit with the error code of the test
  echo "EXITING SCRIPT WITH EXIT CODE OF: $test_exit_code"
  exit "$test_exit_code"
  </code></pre>
  
  <p>Below is the Dockerfile for my <code>db</code> service. All its doing is copying some local data into the Docker container and then initialising the database with this data.</p>
  
  <pre><code>FROM  mongo:3.6.14-xenial
  
  COPY ./dump/ /tmp/dump/
  
  COPY mongo_restore.sh /docker-entrypoint-initdb.d/
  
  RUN chmod 777 /docker-entrypoint-initdb.d/mongo_restore.sh
  </code></pre>
  
  <p>Below is what is in <code>mongo_restore.sh</code>.</p>
  
  <pre><code>#!/bin/bash
  # Creates db using copied data
  mongorestore /tmp/dump
  </code></pre>
  
  <p>Below are the last few lines of output when I run <code>docker-compose up --build --exit-code-from combined; echo $?</code>.</p>
  
  <pre><code>...
  b-combined | user disconnected
  b-combined | Mongoose disconnected
  b-combined | Mongoose disconnected through Heroku app shutdown
  b-combined | TEST ENDED WITH EXIT CODE OF: 0 ===========================
  b-combined | EXITING SCRIPT WITH EXIT CODE OF: 0 =====================================
  Aborting on container exit...
  Stopping b-combined   ... done
  137
  </code></pre>
  
  <p><strong>What is confusing as you can see above, is that the test and script ended with exit code of 0 since all my tests passed successfully but the container still exited with an exit code of 137.</strong> </p>
  
  <p><strong>What is even more confusing is that when I comment out the following line (which runs my Cypress integration tests) from my <code>init.sh</code> file, the container exits with a 0 exit code as shown below.</strong></p>
  
  <pre><code>NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome
  </code></pre>
  
  <p>Below is the output I receive when I comment out / remove the above line from <code>init.sh</code>, which is a command that runs my Cypress integration tests.</p>
  
  <pre><code>...
  b-combined | TEST ENDED WITH EXIT CODE OF: 0 ===========================
  b-combined | EXITING SCRIPT WITH EXIT CODE OF: 0 =====================================
  Aborting on container exit...
  Stopping b-combined   ... done
  0
  </code></pre>
  
  <p><strong>How do I get docker-compose to return me a zero exit code when my tests run successfully and a non-zero exit code when they fail?</strong></p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>After running the following docker-compose command in debug mode, I noticed that b-db seems to have some trouble shutting down and potentially is receiving a SIGKILL signal from Docker because of that.</p>
  
  <pre><code>docker-compose --log-level DEBUG up --build --exit-code-from combined; echo $?
  </code></pre>
  
  <p>Is this indeed the case according to the following output?</p>
  
  <pre><code>...
  b-combined exited with code 0
  Aborting on container exit...
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  http://localhost:None "GET /v1.25/containers/json?limit=-1&amp;all=1&amp;size=0&amp;trunc_cmd=0&amp;filters=%7B%22label%22%3A+%5B%22com.docker.compose.project%3Db-property%22%2C+%22com.docker.compose.oneoff%3DFalse%22%5D%7D HTTP/1.1" 200 3819
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  http://localhost:None "GET /v1.25/containers/0626d6bf49e5236440c82de4e969f31f4f86280d6f8f555f05b157fa53bae9b8/json HTTP/1.1" 200 None
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  http://localhost:None "GET /v1.25/containers/json?limit=-1&amp;all=0&amp;size=0&amp;trunc_cmd=0&amp;filters=%7B%22label%22%3A+%5B%22com.docker.compose.project%3Db-property%22%2C+%22com.docker.compose.oneoff%3DFalse%22%5D%7D HTTP/1.1" 200 4039
  http://localhost:None "POST /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/attach?logs=0&amp;stdout=1&amp;stderr=1&amp;stream=1 HTTP/1.1" 101 0
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  http://localhost:None "GET /v1.25/containers/0626d6bf49e5236440c82de4e969f31f4f86280d6f8f555f05b157fa53bae9b8/json HTTP/1.1" 200 None
  Stopping b-combined   ...
  Stopping b-db         ...
  Pending: {&lt;Container: b-db (0626d6)&gt;, &lt;Container: b-combined (196f3e)&gt;}
  Starting producer thread for &lt;Container: b-combined (196f3e)&gt;
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  http://localhost:None "POST /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/wait HTTP/1.1" 200 32
  http://localhost:None "POST /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/stop?t=10 HTTP/1.1" 204 0
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  http://localhost:None "POST /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561bStopping b-combined   ... done
  Finished processing: &lt;Container: b-combined (196f3e)&gt;
  Pending: {&lt;Container: b-db (0626d6)&gt;}
  Starting producer thread for &lt;Container: b-db (0626d6)&gt;
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  http://localhost:None "GET /v1.25/containers/0626d6bf49e5236440c82de4e969f31f4f86280d6f8f555f05b157fa53bae9b8/json HTTP/1.1" 200 None
  Pending: set()
  Pending: set()
  Pending: set()
  Pending: set()
  Pending: set()
  Pending: set()
  http://localhost:None "GET /v1.25/containers/0626d6bf49e5236440c82de4e969f31f4f86280d6f8f555f05b157fa53bae9b8/json HTTP/1.1" 200 None
  http://localhost:None "POST /v1.25/containers/0626d6bf49e5236440c82de4e969f31f4f86280d6f8f555f05b157fa53bae9b8/stop?t=10 HTTP/1.1" 204 0
  http://localhost:None "POST /v1.25/containers/0626d6bf49e5236440c82de4e969f31f4f86280d6f8f555f05b157fa53bae9b8/wait HTTP/1.1" 200 30
  Stopping b-db         ... done
  Pending: set()
  http://localhost:None "GET /v1.25/containers/0626d6bf49e5236440c82de4e969f31f4f86280d6f8f555f05b157fa53bae9b8/json HTTP/1.1" 200 None
  http://localhost:None "GET /v1.25/containers/196f3e622847b4c4c82d8d761f9f19155561be961eecfe874bbb04def5b7c9e5/json HTTP/1.1" 200 None
  137
  </code></pre>