Docker-compose runs my entrypoint script twice when it should run it once and exit

SUMMARY:

When I run the following command…

docker-compose up --build --exit-code-from combined --timeout 600; echo $?

My init.sh script, which is the entrypoint for my combined container, is run twice instead of once. I expect the init.sh script to be run only once. Why is this happening and how can I make it run only once?

THE FULL STORY:

I have two Docker containers:

  1. b-db – contains my database
  2. b-combined – contains my web application and a series of tests, which run once the container is up and running.

I’m using a docker-compose.yml file to start both containers.

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:

Below is the Dockerfile for the combined service in docker-compose.yml.

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" ]

Below is what is in my init.sh file.

#!/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 a specific test
NODE_ENV=test $(npm bin)/cypress run --spec "cypress/integration/analytics_spec.js"

# 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"

Below is the Dockerfile for my db service. All its doing is copying some local data into the Docker container and then initialising the database with this data.

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

Below is what is in mongo_restore.sh.

#!/bin/bash
# Creates db using copied data
mongorestore /tmp/dump

When I run the following command…

docker-compose up --build --exit-code-from combined --timeout 600; echo $?

I get the following output:

Building db
Step 1/4 : FROM  mongo:3.6.14-xenial
 ---> 63abac6699cd
Step 2/4 : COPY ./dump/ /tmp/dump/
 ---> Using cache
 ---> c63b5bb8a6e6
Step 3/4 : COPY mongo_restore.sh /docker-entrypoint-initdb.d/
 ---> Using cache
 ---> 62de88a27f64
Step 4/4 : RUN chmod 777 /docker-entrypoint-initdb.d/mongo_restore.sh
 ---> Using cache
 ---> 041757bccef5
Successfully built 041757bccef5
Successfully tagged b-property_db:latest
Building combined
Step 1/17 : FROM cypress/included:3.4.1
 ---> 27da0246551c
Step 2/17 : WORKDIR /usr/src/app
 ---> Using cache
 ---> eea587fa33f8
Step 3/17 : RUN npm install -g n
 ---> Using cache
 ---> 6ee678f738a0
Step 4/17 : RUN n 9.2.0
 ---> Using cache
 ---> 27406ead3cff
Step 5/17 : COPY package*.json ./
 ---> Using cache
 ---> 7793be158259
Step 6/17 : RUN npm install [email protected]
 ---> Using cache
 ---> ec6e4bae63ac
Step 7/17 : RUN rm -rf /usr/local/lib/node_modules/npm
 ---> Using cache
 ---> 3cbf0c0c0ec2
Step 8/17 : RUN mv node_modules/npm /usr/local/lib/node_modules/npm
 ---> Using cache
 ---> f92096e43ae1
Step 9/17 : RUN npm install
 ---> Using cache
 ---> c4b556ca3240
Step 10/17 : COPY . .
 ---> Using cache
 ---> 2c63f18d836d
Step 11/17 : EXPOSE 5000
 ---> Using cache
 ---> 8b8e1e7bab2b
Step 12/17 : RUN npm install -g history-server nodemon
 ---> Using cache
 ---> da8e46eb02b1
Step 13/17 : RUN npm run build-test
 ---> Using cache
 ---> 424f7171c913
Step 14/17 : EXPOSE 8080
 ---> Using cache
 ---> ef25e1f0d272
Step 15/17 : COPY ./docker/combined/init.sh /scripts/init.sh
 ---> Using cache
 ---> bd14264aac05
Step 16/17 : RUN ["chmod", "+x", "/scripts/init.sh"]
 ---> Using cache
 ---> 214afacc9ace
Step 17/17 : ENTRYPOINT [ "/scripts/init.sh" ]
 ---> Using cache
 ---> 9ed5241c92f1
Successfully built 9ed5241c92f1
Successfully tagged b-property_combined:latest
Recreating b-db ... done
Recreating b-combined ... done
Attaching to b-db, b-combined
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=3bbb8f5fe956
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten] db version v3.6.14
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten] git version: cbef87692475857c7ee6e764c8f5104b39c342a1
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten] allocator: tcmalloc
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten] modules: none
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten] build environment:
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten]     distarch: x86_64
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten]     target_arch: x86_64
b-db | 2019-12-13T11:27:30.229+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true } }
b-db | 2019-12-13T11:27:30.230+0000 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
b-db | 2019-12-13T11:27:30.230+0000 I STORAGE  [initandlisten]
b-db | 2019-12-13T11:27:30.230+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
b-db | 2019-12-13T11:27:30.230+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
b-db | 2019-12-13T11:27:30.230+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=2466M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),compatibility=(release="3.0",require_max="3.0"),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
b-db | 2019-12-13T11:27:30.882+0000 I STORAGE  [initandlisten] WiredTiger message [1576236450:882897][1:0x7f78cf0bca40], txn-recover: Main recovery loop: starting at 70/242560
b-db | 2019-12-13T11:27:30.960+0000 I STORAGE  [initandlisten] WiredTiger message [1576236450:960786][1:0x7f78cf0bca40], txn-recover: Recovering log 70 through 71
b-db | 2019-12-13T11:27:31.025+0000 I STORAGE  [initandlisten] WiredTiger message [1576236451:25011][1:0x7f78cf0bca40], txn-recover: Recovering log 71 through 71
b-db | 2019-12-13T11:27:31.073+0000 I STORAGE  [initandlisten] WiredTiger message [1576236451:73785][1:0x7f78cf0bca40], txn-recover: Set global recovery timestamp: 0
b-db | 2019-12-13T11:27:31.111+0000 I CONTROL  [initandlisten]
b-db | 2019-12-13T11:27:31.111+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
b-db | 2019-12-13T11:27:31.111+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
b-db | 2019-12-13T11:27:31.111+0000 I CONTROL  [initandlisten]
b-db | 2019-12-13T11:27:31.113+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
b-db | 2019-12-13T11:27:31.114+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
b-combined | [nodemon] 2.0.2
b-combined | [nodemon] to restart at any time, enter `rs`
b-combined | [nodemon] watching dir(s): *.*
b-combined | [nodemon] watching extensions: js,mjs,json
b-combined | [nodemon] starting `node server.js`
b-combined | history-server listening on port 8080; Ctrl+C to stop
b-combined | [email protected] alert: no DSN provided, error reporting disabled
b-combined | test
b-combined | server started 5000
b-db | 2019-12-13T11:27:32.336+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33086 #1 (1 connection now open)
b-db | 2019-12-13T11:27:32.340+0000 I NETWORK  [conn1] received client metadata from 172.22.0.3:33086 conn1: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v9.2.0, LE, mongodb-core: 3.2.7" }
b-combined | Mongoose connected to mongodb://db:27017/book-test
b-db | 2019-12-13T11:27:32.359+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33088 #2 (2 connections now open)
b-db | 2019-12-13T11:27:32.361+0000 I NETWORK  [conn2] received client metadata from 172.22.0.3:33088 conn2: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v9.2.0, LE, mongodb-core: 3.2.7" }
b-db | 2019-12-13T11:27:32.364+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33090 #3 (3 connections now open)
b-db | 2019-12-13T11:27:32.366+0000 I NETWORK  [conn3] received client metadata from 172.22.0.3:33090 conn3: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v9.2.0, LE, mongodb-core: 3.2.7" }
b-db | 2019-12-13T11:27:36.866+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33092 #4 (4 connections now open)
b-db | 2019-12-13T11:27:36.874+0000 I NETWORK  [conn4] received client metadata from 172.22.0.3:33092 conn4: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v8.9.3, LE, mongodb-core: 3.2.7" }
b-db | 2019-12-13T11:27:36.874+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33094 #5 (5 connections now open)
b-combined | Mongoose connected to mongodb://db:27017/book-test
b-db | 2019-12-13T11:27:36.892+0000 I NETWORK  [conn5] received client metadata from 172.22.0.3:33094 conn5: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v8.9.3, LE, mongodb-core: 3.2.7" }
b-db | 2019-12-13T11:27:36.896+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33096 #6 (6 connections now open)
b-db | 2019-12-13T11:27:36.897+0000 I NETWORK  [conn6] received client metadata from 172.22.0.3:33096 conn6: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v8.9.3, LE, mongodb-core: 3.2.7" }
b-db | 2019-12-13T11:27:36.902+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33098 #7 (7 connections now open)
b-db | 2019-12-13T11:27:36.903+0000 I NETWORK  [conn7] received client metadata from 172.22.0.3:33098 conn7: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v8.9.3, LE, mongodb-core: 3.2.7" }
b-combined |
b-combined | ====================================================================================================
b-combined |
b-combined |   (Run Starting)
b-combined |
b-combined |   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
b-combined |   │ Cypress:    3.4.1                                                                              │
b-combined |   │ Browser:    Chrome 75                                                                          │
b-combined |   │ Specs:      1 found (analytics_spec.js)                                                        │
b-combined |   │ Searched:   cypress/integration/analytics_spec.js                                              │
b-combined |   └────────────────────────────────────────────────────────────────────────────────────────────────┘
b-combined |
b-combined |
b-combined | ────────────────────────────────────────────────────────────────────────────────────────────────────
b-combined |
b-combined |   Running: analytics_spec.js...                                                            (1 of 1)
b-combined |
b-combined |
b-combined |   Analytics
b-combined | (node:212) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
b-combined | (node:212) DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
    ✓ displays listings on the side bar lazily for upcoming events (6113ms)
b-combined | user disconnected
b-combined | user disconnected
    ✓ displays listings on the side bar lazily for archived events (3635ms)
b-combined | user disconnected
b-combined | user disconnected
    ✓ allows the user to click between upcoming and archived (3538ms)
b-combined | user disconnected
b-combined | user disconnected
b-combined | user disconnected
b-combined | (node:30) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#-findandmodify-
    ✓ updates prediction and view count (8660ms)
b-combined | user disconnected
b-combined | user disconnected
b-combined | user disconnected
b-combined | user disconnected
    ✓ takes users to the correct routes (7302ms)
b-combined |
b-combined |
b-combined |   5 passing (30s)
b-combined |
b-combined |
b-combined |   (Results)
b-combined |
b-combined |   ┌─────────────────────────────────┐
b-combined |   │ Tests:        5                 │
b-combined |   │ Passing:      5                 │
b-combined |   │ Failing:      0                 │
b-combined |   │ Pending:      0                 │
b-combined |   │ Skipped:      0                 │
b-combined |   │ Screenshots:  0                 │
b-combined |   │ Video:        false             │
b-combined |   │ Duration:     29 seconds        │
b-combined |   │ Spec Ran:     analytics_spec.js │
b-combined |   └─────────────────────────────────┘
b-combined |
b-combined |
b-combined | ====================================================================================================
b-combined |
b-combined |   (Run Finished)
b-combined |
b-combined |
b-combined |       Spec                                                Tests  Passing  Failing  Pending  Skipped
b-combined |   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
b-combined |   │ ✔ analytics_spec.js                         00:29        5        5        -        -        - │
b-combined |   └────────────────────────────────────────────────────────────────────────────────────────────────┘
b-combined |     All specs passed!                           00:29        5        5        -        -        -
b-combined |
b-combined | Mongoose disconnected
b-db | 2019-12-13T11:28:09.278+0000 I NETWORK  [conn4] end connection 172.22.0.3:33092 (6 connections now open)
b-db | 2019-12-13T11:28:09.279+0000 I NETWORK  [conn6] end connection 172.22.0.3:33096 (5 connections now open)
b-combined | Mongoose disconnected through app shutdown
b-db | 2019-12-13T11:28:09.279+0000 I NETWORK  [conn7] end connection 172.22.0.3:33098 (4 connections now open)
b-db | 2019-12-13T11:28:09.305+0000 I NETWORK  [conn5] end connection 172.22.0.3:33094 (3 connections now open)
b-combined | user disconnected
b-combined | TEST ENDED WITH EXIT CODE OF: 0 ===========================
b-combined | EXITING SCRIPT WITH EXIT CODE OF: 0 =====================================
b-db | 2019-12-13T11:28:09.512+0000 I NETWORK  [conn2] end connection 172.22.0.3:33088 (2 connections now open)
b-db | 2019-12-13T11:28:09.512+0000 I NETWORK  [conn3] end connection 172.22.0.3:33090 (1 connection now open)
b-db | 2019-12-13T11:28:09.512+0000 I NETWORK  [conn1] end connection 172.22.0.3:33086 (0 connections now open)
b-combined | [nodemon] 2.0.2
b-combined | [nodemon] to restart at any time, enter `rs`
b-combined | [nodemon] watching dir(s): *.*
b-combined | [nodemon] watching extensions: js,mjs,json
b-combined | [nodemon] starting `node server.js`
b-combined | history-server listening on port 8080; Ctrl+C to stop
b-combined | [email protected] alert: no DSN provided, error reporting disabled
b-combined | test
b-combined | server started 5000
b-db | 2019-12-13T11:28:13.290+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33342 #8 (1 connection now open)
b-db | 2019-12-13T11:28:13.293+0000 I NETWORK  [conn8] received client metadata from 172.22.0.3:33342 conn8: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v9.2.0, LE, mongodb-core: 3.2.7" }
b-combined | Mongoose connected to mongodb://db:27017/book-test
b-db | 2019-12-13T11:28:13.312+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33344 #9 (2 connections now open)
b-db | 2019-12-13T11:28:13.313+0000 I NETWORK  [conn9] received client metadata from 172.22.0.3:33344 conn9: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v9.2.0, LE, mongodb-core: 3.2.7" }
b-db | 2019-12-13T11:28:13.317+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33346 #10 (3 connections now open)
b-db | 2019-12-13T11:28:13.317+0000 I NETWORK  [conn10] received client metadata from 172.22.0.3:33346 conn10: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v9.2.0, LE, mongodb-core: 3.2.7" }
b-db | 2019-12-13T11:28:17.821+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33348 #11 (4 connections now open)
b-db | 2019-12-13T11:28:17.826+0000 I NETWORK  [conn11] received client metadata from 172.22.0.3:33348 conn11: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v8.9.3, LE, mongodb-core: 3.2.7" }
b-combined | Mongoose connected to mongodb://db:27017/book-test
b-db | 2019-12-13T11:28:17.843+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33350 #12 (5 connections now open)
b-db | 2019-12-13T11:28:17.849+0000 I NETWORK  [conn12] received client metadata from 172.22.0.3:33350 conn12: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v8.9.3, LE, mongodb-core: 3.2.7" }
b-db | 2019-12-13T11:28:17.851+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33352 #13 (6 connections now open)
b-db | 2019-12-13T11:28:17.851+0000 I NETWORK  [conn13] received client metadata from 172.22.0.3:33352 conn13: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v8.9.3, LE, mongodb-core: 3.2.7" }
b-db | 2019-12-13T11:28:17.857+0000 I NETWORK  [listener] connection accepted from 172.22.0.3:33354 #14 (7 connections now open)
b-db | 2019-12-13T11:28:17.857+0000 I NETWORK  [conn14] received client metadata from 172.22.0.3:33354 conn14: { driver: { name: "nodejs", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v8.9.3, LE, mongodb-core: 3.2.7" }
b-combined |
b-combined | ====================================================================================================
b-combined |
b-combined |   (Run Starting)
b-combined |
b-combined |   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
b-combined |   │ Cypress:    3.4.1                                                                              │
b-combined |   │ Browser:    Chrome 75                                                                          │
b-combined |   │ Specs:      1 found (analytics_spec.js)                                                        │
b-combined |   │ Searched:   cypress/integration/analytics_spec.js                                              │
b-combined |   └────────────────────────────────────────────────────────────────────────────────────────────────┘
b-combined |
b-combined |
b-combined | ────────────────────────────────────────────────────────────────────────────────────────────────────
b-combined |
b-combined |   Running: analytics_spec.js...                                                            (1 of 1)
b-combined |
b-combined |
b-combined |   Analytics
b-combined | (node:212) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
b-combined | (node:212) DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
    ✓ displays listings on the side bar lazily for upcoming events (6565ms)
b-combined | user disconnected
b-combined | user disconnected
    ✓ displays listings on the side bar lazily for archived events (4227ms)
b-combined | user disconnected
b-combined | user disconnected
    ✓ allows the user to click between upcoming and archived (3456ms)
b-combined | user disconnected
b-combined | user disconnected
b-combined | user disconnected
b-combined | (node:30) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#-findandmodify-
    ✓ updates prediction and view count (9194ms)
b-combined | user disconnected
b-combined | user disconnected
b-combined | user disconnected
b-combined | user disconnected
    ✓ takes users to the correct routes (8759ms)
b-combined |
b-combined |
b-combined |   5 passing (32s)
b-combined |
b-combined |
b-combined |   (Results)
b-combined |
b-combined |   ┌─────────────────────────────────┐
b-combined |   │ Tests:        5                 │
b-combined |   │ Passing:      5                 │
b-combined |   │ Failing:      0                 │
b-combined |   │ Pending:      0                 │
b-combined |   │ Skipped:      0                 │
b-combined |   │ Screenshots:  0                 │
b-combined |   │ Video:        false             │
b-combined |   │ Duration:     32 seconds        │
b-combined |   │ Spec Ran:     analytics_spec.js │
b-combined |   └─────────────────────────────────┘
b-combined |
b-combined |
b-combined | ====================================================================================================
b-combined |
b-combined |   (Run Finished)
b-combined |
b-combined |
b-combined |       Spec                                                Tests  Passing  Failing  Pending  Skipped
b-combined |   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
b-combined |   │ ✔ analytics_spec.js                         00:32        5        5        -        -        - │
b-combined |   └────────────────────────────────────────────────────────────────────────────────────────────────┘
b-combined |     All specs passed!                           00:32        5        5        -        -        -
b-combined |
b-combined | user disconnected
b-combined | Mongoose disconnected
b-db | 2019-12-13T11:28:53.042+0000 I NETWORK  [conn13] end connection 172.22.0.3:33352 (6 connections now open)
b-db | 2019-12-13T11:28:53.042+0000 I NETWORK  [conn11] end connection 172.22.0.3:33348 (5 connections now open)
b-db | 2019-12-13T11:28:53.042+0000 I NETWORK  [conn14] end connection 172.22.0.3:33354 (4 connections now open)
b-combined | Mongoose disconnected through app shutdown
b-db | 2019-12-13T11:28:53.065+0000 I NETWORK  [conn12] end connection 172.22.0.3:33350 (3 connections now open)
b-combined | TEST ENDED WITH EXIT CODE OF: 0 ===========================
b-combined | EXITING SCRIPT WITH EXIT CODE OF: 0 =====================================
b-db | 2019-12-13T11:28:53.204+0000 I NETWORK  [conn9] end connection 172.22.0.3:33344 (2 connections now open)
b-db | 2019-12-13T11:28:53.205+0000 I NETWORK  [conn8] end connection 172.22.0.3:33342 (1 connection now open)
b-db | 2019-12-13T11:28:53.205+0000 I NETWORK  [conn10] end connection 172.22.0.3:33346 (0 connections now open)
b-combined exited with code 0
Aborting on container exit...
Stopping b-combined   ... done
Stopping b-db         ... done
0

I expect my init.sh script to be run once but as you can see from the above output, docker-compose runs my init.sh script twice.

I’ve pulled out the following snippets from the above output to demonstrate that the init.sh script is indeed being run twice:

172.22.0.3:33094 (3 connections now open)
    b-combined | user disconnected
    b-combined | TEST ENDED WITH EXIT CODE OF: 0 ===========================
    b-combined | EXITING SCRIPT WITH EXIT CODE OF: 0 =====================================

...

172.22.0.3:33350 (3 connections now open)
    b-combined | TEST ENDED WITH EXIT CODE OF: 0 ===========================
    b-combined | EXITING SCRIPT WITH EXIT CODE OF: 0 

Why is this happening and how can I ensure docker-compose runs my init.sh script once?

Source: StackOverflow