I expect the exit code to be 0 when I run the following command because my combined container performs a test that successfully exits with an exit code of 0.
docker-compose up --build --exit-code-from combined
Unfortunately, even when the tests in my combined container run well and I quit that container with an exit code of 0, I always get an exit code of 137. (more details on how that happens are specified below).
My Docker-compose version like this:
docker-compose version 1.25.0, build 0a186604
The exit code of 137 might be caused by two main concerns, according to one article.
1. The app is not gracefully receiving SIGTERM because the container received a docker stop.
2. The container's memory has been depleted (OOM).
I'm pretty sure the 137 exit code means my container hasn't ran out of memory. I can see that "OOMKilled" is false when I run docker inspect container-id>, as demonstrated in the sample below. I've also given the Docker Engine 6GB of memory, which is more than enough for my application.
[
{
"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, <---- 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"
},
Because my container does not depart from a docker stop, I don't believe the first reason applies to me.
How my Docker containers looks like?
I've got two Docker containers running:
1. b-db - this is where I save my database.
2. b-combined - contains my web application as well as a set of tests that run once the container is started.
To start both containers, I use a docker-compose.yml file.
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:
In docker-compose.yml, you'll find the Dockerfile for the combined service.
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" ]
My init.sh
#!/bin/bash
# Start front end server
history-server dist -p 8080 &
front_pid=$!
# Start back end server that interacts with DB
nodemon -L server &
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"
The Dockerfile for my database service is shown below. All it does is put some local data into the Docker container and then use that data to populate the database.
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
My mongo_restore.sh
#!/bin/bash
# Creates db using copied data
mongorestore /tmp/dump
The last few lines of output from docker-compose up —build —exit-code-from combined; echo $? are shown below.
...
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
What's perplexing, as you can see above, is that the test and script both exited with a 0 exit code, despite the fact that all of my tests passed, but the container still exited with a 137 exit code.
Even more perplexing, when I comment out the following line from my init.sh file (which runs my Cypress integration tests), the container exits with a 0 exit code, as shown below.
NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome
When I comment out / remove the above line from init.sh, which is a programme that runs my Cypress integration tests, I get the following output.
...
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
How do I get docker-compose to return a zero exit code for successful tests and a non-zero exit code for failed tests?