Я пытаюсь докернизировать мой API, но когда я запускаю приложение с файлом docker-compose, mongodb не запускается, и приложение не может подключиться к базе данных.При запуске тестов приложения с использованием docker тесты выполняются правильно, но при запуске npm запускается производство, служба mongodb запускается и деактивируется.
docker-compose.yml
version: '3'
services:
backend:
build:
context: ../
dockerfile: docker/Dockerfile.production
args:
port: ${PORT}
env_file:
- ../.env
restart: always
ports:
- "${PORT}:${PORT}"
environment:
WAIT_HOSTS: database:27017
database:
image: mongo:latest
env_file:
- ../.env
volumes:
- ".${MONGO_DATA_DIR}:${MONGO_DATA_DIR}"
expose:
- 27017
command: "mongod --smallfiles --logpath=${MONGO_LOG_FILE}"
docker-compose.test.yml
version: '3'
services:
backend-test:
build:
context: ../
dockerfile: docker/Dockerfile.test
args:
port: ${PORT}
env_file:
- ../.env
environment:
WAIT_HOSTS: database-test:27017
database-test:
image: mongo:latest
env_file:
- ../.env
volumes:
- ".${MONGO_TEST_DATA_DIR}:${MONGO_TEST_DATA_DIR}"
expose:
- 27017
command: "mongod --smallfiles --logpath=${MONGO_LOG_FILE}"
Dockerfile.production
FROM node:10.12.0-alpine
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
WORKDIR /home/nodejs/app
ENV NODE_ENV prod
COPY package*.json ./
RUN npm install --only=production
ARG port=80
EXPOSE $port
COPY . ./
CMD /wait && node index.js
Dockerfile.test
FROM docker_backend
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
ENV NODE_ENV dev
RUN npm install && npm install -g mocha --timeout 10000
ARG port=80
CMD /wait && mocha tests/unitTests.js --exit
index.js
const express = require('express')
const bodyParser = require('body-parser')
const MongoClient = require('mongodb')
const dbName = process.env.NODE_ENV === 'dev' ? 'database-test' : 'database'
const url = `mongodb://${process.env.MONGO_INITDB_ROOT_USERNAME}:${process.env.MONGO_INITDB_ROOT_PASSWORD}@${dbName}:27017?authMechanism=SCRAM-SHA-1&authSource=admin`
const options = {
useNewUrlParser: true,
reconnectTries: 60,
reconnectInterval: 1000
}
const user = require('./routes/user.js')
const port = process.env.PORT || 80
const app = express()
const http = require('http').Server(app)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true}))
app.use('/api/v1', user)
app.use((req, res) => {
res.status(404)
})
MongoClient.connect(url, options, (err, database) => {
if(err) {
console.log(`FATAL MONGODB CONNECTION ERROR: $(err):$(err.stack)`)
process.exit(1)
}
app.locals.db = database.db('api')
http.listen(port, () => {
console.log("Listening on port " + port)
app.emit('APP_STARTED')
})
})
module.exports = app
package.json
{
"name": "user-register-backend",
"version": "1.0.0",
"description": "A api to register users backend using cpf and cnpj validation",
"main": "index.js",
"scripts": {
"test": "docker-compose -f docker/docker-compose.test.yml up --build --abort-on-container-exit",
"production": "docker-compose -f docker/docker-compose.yml up",
"build": "docker-compose -f docker/docker-compose.yml build"
},
"keywords": [
"express",
"mongo"
],
"author": "Jefferson Bruchado",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongodb": "^3.1.10"
},
"devDependencies": {
"mocha": "^5.2.0",
"supertest": "^3.3.0",
"tape": "^4.9.1"
}
}
Запуск npm run test, приложение работает и выполняет модульные тесты, но когда япопробуйте запустить npm run production, служба mongodb не запускается, поэтому приложение не работает.
Ошибка:
> docker-compose -f docker/docker-compose.yml up
Starting docker_database_1 ... done
Starting docker_backend_1 ... done
Attaching to docker_backend_1, docker_database_1
backend_1 | Checking availability of database:27017
backend_1 | Host database:27017 not yet available
database_1 | about to fork child process, waiting until server is ready for connections.
database_1 | forked process: 24
database_1 | 2018-12-29T17:28:25.914+0000 I CONTROL [main] ***** SERVER RESTARTED *****
database_1 | 2018-12-29T17:28:25.918+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] MongoDB starting : pid=24 port=27017 dbpath=/data/db 64-bit host=f1619d77c7b7
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] db version v4.0.5
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] allocator: tcmalloc
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] modules: none
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] build environment:
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] distmod: ubuntu1604
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] distarch: x86_64
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] target_arch: x86_64
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] options: { net: { bindIp: "127.0.0.1", port: 27017, ssl: { mode: "disabled" } }, processManagement: { fork: true, pidFilePath: "/tmp/docker-entrypoint-temp-mongod.pid" }, storage: { mmapv1: { smallFiles: true } }, systemLog: { destination: "file", logAppend: true, path: "/proc/1/fd/1" } }
database_1 | 2018-12-29T17:28:25.948+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=478M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
backend_1 | Host database:27017 not yet available
database_1 | 2018-12-29T17:28:26.837+0000 E STORAGE [initandlisten] WiredTiger error (1) [1546104506:837236][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1546104506:837236][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
database_1 | 2018-12-29T17:28:26.892+0000 E STORAGE [initandlisten] WiredTiger error (17) [1546104506:892012][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: File exists Raw: [1546104506:892012][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: File exists
database_1 | 2018-12-29T17:28:26.895+0000 I STORAGE [initandlisten] WiredTiger message unexpected file WiredTiger.wt found, renamed to WiredTiger.wt.1
database_1 | 2018-12-29T17:28:26.898+0000 E STORAGE [initandlisten] WiredTiger error (1) [1546104506:898402][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1546104506:898402][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
database_1 | 2018-12-29T17:28:26.967+0000 E STORAGE [initandlisten] WiredTiger error (17) [1546104506:967112][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: File exists Raw: [1546104506:967112][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: File exists
database_1 | 2018-12-29T17:28:26.970+0000 I STORAGE [initandlisten] WiredTiger message unexpected file WiredTiger.wt found, renamed to WiredTiger.wt.2
database_1 | 2018-12-29T17:28:26.972+0000 E STORAGE [initandlisten] WiredTiger error (1) [1546104506:972043][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1546104506:972043][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
database_1 | 2018-12-29T17:28:26.974+0000 W STORAGE [initandlisten] Failed to start up WiredTiger under any compatibility version.
database_1 | 2018-12-29T17:28:26.974+0000 F STORAGE [initandlisten] Reason: 1: Operation not permitted
database_1 | 2018-12-29T17:28:26.974+0000 F - [initandlisten] Fatal Assertion 28595 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 638
database_1 | 2018-12-29T17:28:26.974+0000 F - [initandlisten]
database_1 |
database_1 | ***aborting after fassert() failure
database_1 |
database_1 |
database_1 | ERROR: child process failed, exited with error number 14
database_1 | To see additional information in this output, start without the "--fork" option.
docker_database_1 exited with code 14
backend_1 | Host database:27017 not yet available
backend_1 | Host database:27017 not yet available