I am currently building an application using NestJS by following microservice architecture. And use NATS as a messaging system. It works fine on my local machine but I am unable to dockerized the NATS server.
The folder structure of my project:
├─── services/
├─── nats/
| Dockerfile
├─── client/
| ...
| ...
| .dockerignore
| Dockerfile
└─── docker-compose.yml
Now, the Dockerfile inside nats/ folder describes as below:
FROM nats:2.1.9
EXPOSE 4222
CMD [ "nats-server" ]
And Dockerfile inside client/ folder describes as:
FROM node:12
WORKDIR /myProject/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 8081
CMD [ "node", "dist/main" ]
And the docker-compose.yml is:
version: '3'
services:
nats-server:
image: nats:2.1.9
restart: always
build: ./nats
ports:
- 4222:4222
client-service:
build: ./client
volumes:
- ./client:/myProject/src/app
depends_on:
- nats-server
environment:
NODE_ENV: development
ports:
- 8081:3000
I've run the command sudo docker-compose build and found no error, after that run the command sudo docker-compose up and found the following error:
Recreating services_nats-server_1 ... done
Recreating services_client-service_1 ... done
Attaching to services_nats-server_1, services_client-service_1
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
services_nats-server_1 exited with code 1
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [NestFactory] Starting Nest application...
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] AppModule dependencies initialized +41ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] MongooseModule dependencies initialized +1ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] ClientsModule dependencies initialized +1ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] ClientsModule dependencies initialized +0ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] ConfigHostModule dependencies initialized +1ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] ConfigModule dependencies initialized +1ms
services_nats-server_1 exited with code 1
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
There is no problem with the client service Dockerfile, as it depends on the nats-server tried to run it first. Can anyone please help me to figure out the problem?
I would like to thank you for your time and consideration.
The build: command for the nats-server service has overwritten the Docker Hub image. I just need to pull the image and expose the port.
So, my docker-compose.yml file will be as following:
version: '3'
services:
nats-server:
image: nats:2.1.9
restart: always
ports:
- 4222:4222
client-service:
build: ./client
volumes:
- ./client:/myProject/src/app
depends_on:
- nats-server
environment:
NODE_ENV: development
ports:
- 8081:3000
Check the Dockerfile and you'll notice that ENTRYPOINT is set to /nats-server and CMD is the parameters to ENTRYPOINT (--config nats-server.conf in that case).
Unless you extend the nats image you don't need to build a new one based on it (remove build: ./nats from the compose file). Overriding the default CMD can be achieved by setting command: on the docker-compose.yml.
Related
This question already has answers here:
Docker - Can't reach database server at `localhost`:`3306` with Prisma service
(2 answers)
ECONNREFUSED for Postgres on nodeJS with dockers
(7 answers)
Closed 17 days ago.
This post was edited and submitted for review 16 days ago and failed to reopen the post:
Original close reason(s) were not resolved
I am trying to build a docker image from my project and run it in a container,
The project is keystone6 project connecting to a postgres database, everything worked well when I normally run the project and it connects successfully to the database.
Here is my dockerfile:
FROM node:18.13.0-alpine3.16
ENV NODE_VERSION 18.13.0
ENV NODE_ENV=development
LABEL Name="di-wrapp" Version="1"
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install
COPY .env .
EXPOSE 9999
CMD ["npm", "run", "dev"]
I am building an image using the command docker build -t di-wrapp:1.0 .
after that I run docker-compose file which contains the following code:
version: "3.8"
services:
postgres:
image: postgres:15-alpine
container_name: localhost
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=di_wrapp
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
dashboard:
image: di-wrapp:1.0
container_name: di-wrapp-container
restart: always
environment:
- DB_CONNECTION=postgres
- DB_PORT=5432
- DB_HOST=localhost
- DB_USER=postgres
- DB_PASSWORD=postgres
- DB_NAME=di_wrapp
tty: true
depends_on:
- postgres
ports:
- 8273:9999
links:
- postgres
command: "npm run dev"
volumes:
- /usr/src/app
volumes:
postgres-data:
And this is the connection URI used to connect my project to postgres:
DATABASE_URL=postgresql://postgres:postgres#localhost:5432/di_wrapp
which I am using to configure my db setting in keystone config file like this:
export const db: DatabaseConfig<BaseKeystoneTypeInfo> = {
provider: "postgresql",
url: String(DATABASE_URL!),
};
when I run the command docker-compose -f docker-compose.yaml up
This is what I receive:
localhost | 2023-02-03 13:43:35.034 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
localhost | 2023-02-03 13:43:35.034 UTC [1] LOG: listening on IPv6 address "::", port 5432
localhost | 2023-02-03 13:43:35.067 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
localhost | 2023-02-03 13:43:35.121 UTC [24] LOG: database system was shut down at 2023-02-03 13:43:08 UTC
localhost | 2023-02-03 13:43:35.155 UTC [1] LOG: database system is ready to accept connections
di-wrapp-container | > keystone-app#1.0.2 dev
di-wrapp-container | > keystone dev
di-wrapp-container |
di-wrapp-container | ✨ Starting Keystone
di-wrapp-container | ⭐️ Server listening on :8273 (http://localhost:8273/)
di-wrapp-container | ⭐️ GraphQL API available at /api/graphql
di-wrapp-container | ✨ Generating GraphQL and Prisma schemas
di-wrapp-container | Error: P1001: Can't reach database server at `localhost`:`5432`
di-wrapp-container |
di-wrapp-container | Please make sure your database server is running at `localhost`:`5432`.
di-wrapp-container | at Object.createDatabase (/usr/src/app/node_modules/#prisma/internals/dist/migrateEngineCommands.js:115:15)
di-wrapp-container | at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
di-wrapp-container | at async ensureDatabaseExists (/usr/src/app/node_modules/#keystone-6/core/dist/migrations-e3b5740b.cjs.dev.js:262:19)
di-wrapp-container | at async Object.pushPrismaSchemaToDatabase (/usr/src/app/node_modules/#keystone-6/core/dist/migrations-e3b5740b.cjs.dev.js:68:3)
di-wrapp-container | at async Promise.all (index 1)
di-wrapp-container | at async setupInitialKeystone (/usr/src/app/node_modules/#keystone-6/core/scripts/cli/dist/keystone-6-core-scripts-cli.cjs.dev.js:984:3)
di-wrapp-container | at async initKeystone (/usr/src/app/node_modules/#keystone-6/core/scripts/cli/dist/keystone-6-core-scripts-cli.cjs.dev.js:762:35)di-wrapp-container exited with code 1
even though I receive that the database server is up on port 5432, my app container can't connect to it.
any help is appreciated.
Good day everyone!
I have an old app from dev that now isn't working in our company. I need to start this app but don't have enough experience in NodeJS (I don't have it at all, TBH).
The problem is: I can build a docker image, start it, and use the app, but when I make something that requires to make a request to MQSQL server, the app crashes.
But there are no issues with requests to Postgres DB.
This is my docker build output
docker build -t fixver .
Sending build context to Docker daemon 2.427MB
Step 1/21 : FROM node:16-alpine as base
---> c4ee3c9d7bc1
Step 2/21 : ARG NODE_ENV=production
---> Using cache
---> ba79cfac2e2c
Step 3/21 : ENV NODE_ENV=${NODE_ENV} NODE_OPTIONS="--max_old_space_size=8192"
---> Using cache
---> 1a344f8791d8
Step 4/21 : WORKDIR /usr/src/app
---> Using cache
---> 1c591a772bcd
Step 5/21 : FROM base as clientBuilder
---> 1c591a772bcd
Step 6/21 : COPY ./client/package.json ./client/yarn.lock ./
---> Using cache
---> 7382b944fcc0
Step 7/21 : RUN yarn install --production=false --frozen-lockfile
---> Using cache
---> 431700be035b
Step 8/21 : COPY ./client .
---> Using cache
---> 87790e7c061a
Step 9/21 : RUN yarn build
---> Using cache
---> 9ba70dd8301c
Step 10/21 : FROM base as serverBuilder
---> 1c591a772bcd
Step 11/21 : COPY ./server/package.json ./server/yarn.lock ./
---> Using cache
---> bf5dc70ee2eb
Step 12/21 : RUN yarn install --production=false --frozen-lockfile
---> Using cache
---> c26b02f2af5c
Step 13/21 : COPY ./server .
---> 16fdc772d650
Step 14/21 : RUN yarn build
---> Running in 676753d20a77
yarn run v1.22.19
$ node_modules/.bin/rimraf dist
$ node_modules/.bin/nest build
Done in 5.75s.
Removing intermediate container 676753d20a77
---> 66a53b6af7cd
Step 15/21 : FROM base as production
---> 1c591a772bcd
Step 16/21 : COPY ./server/package.json ./server/.env ./
---> dbbbe7295cfd
Step 17/21 : RUN yarn install --pure-lockfile
---> Running in f3b965d1ec0d
yarn install v1.22.19
info No lockfile found.
[1/4] Resolving packages...
warning Resolution field "uuid#8.3.2" is incompatible with requested version "uuid#^3.1.0"
warning jest > jest-cli > jest-config > jest-environment-jsdom > jsdom > w3c-hr-time#1.0.2: Use your platform's native performance.now() and performance.timeOrigin.
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning " > ts-loader#9.4.2" has unmet peer dependency "webpack#^5.0.0".
[4/4] Building fresh packages...
Done in 39.04s.
Removing intermediate container f3b965d1ec0d
---> 8d804071a8d2
Step 18/21 : COPY --from=serverBuilder /usr/src/app/dist ./
---> f760fda4b1a1
Step 19/21 : COPY --from=clientBuilder /usr/src/app/build ./public
---> 4f697bab8bc6
Step 20/21 : EXPOSE 80
---> Running in 95fae0df9266
Removing intermediate container 95fae0df9266
---> 4bebff7957bc
Step 21/21 : CMD ["node", "./main"]
---> Running in 19b4df36b37e
Removing intermediate container 19b4df36b37e
---> 362fbadf0b4a
Successfully built 362fbadf0b4a
Successfully tagged fixver:latest
Starting the app
docker run -p 7007:80 fixver
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [NestFactory] Starting Nest application...
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] AppModule dependencies initialized +60ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] PassportModule dependencies initialized +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ServeStaticModule dependencies initialized +0ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] AuthModule dependencies initialized +8ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] PagesModule dependencies initialized +0ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ScriptsModule dependencies initialized +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RoutesResolver] ScriptsController {/api/scripts}: +8ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts, GET} route +4ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts/:id, GET} route +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts/:id/check, POST} route +0ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts/:id/apply, POST} route +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RoutesResolver] PagesController {/api/pages}: +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/pages/:id, GET} route +0ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [NestApplication] Nest application successfully started +5ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG Server started! Port: 80
And error right after making request to MSSQL
node:events:491
throw er; // Unhandled 'error' event
^
Error: No event 'featureExtAck' in state 'SentLogin7WithNTLMLogin'
at Connection.dispatchEvent (/usr/src/app/node_modules/tedious/lib/connection.js:1663:26)
at Parser.<anonymous> (/usr/src/app/node_modules/tedious/lib/connection.js:1224:12)
at Parser.emit (node:events:513:28)
at Readable.<anonymous> (/usr/src/app/node_modules/tedious/lib/token/token-stream-parser.js:27:14)
at Readable.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Readable.push (node:internal/streams/readable:228:10)
at next (node:internal/streams/from:98:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Readable instance at:
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
I really don't know what to do with it, how to debug it and find the problem.
Any help and advise is appreciated.
Problem was solved by changing this code in Dockerfile
-COPY ./server/package.json ./server/.env ./
-RUN yarn install --pure-lockfile
+COPY ./server/package.json ./server/yarn.lock ./server/.env ./
+RUN yarn install --production=true --frozen-lockfile
I have got the same problem described in this post, but inside a docker container.
I don't really know where my pgadmin file reside to edit it's default path.How do I go about fixing this issue? Please be as detailed as possible because I don't know how to docker.
Here is an abstract of the verbatim of docker-compose up command:
php-worker_1 | 2020-11-11 05:50:13,700 INFO spawned: 'laravel-worker_03' with pid 67
pgadmin_1 | [2020-11-11 05:50:13 +0000] [223] [INFO] Worker exiting (pid: 223)
pgadmin_1 | WARNING: Failed to set ACL on the directory containing the configuration database:
pgadmin_1 | [Errno 1] Operation not permitted: '/var/lib/pgadmin'
pgadmin_1 | HINT : You may need to manually set the permissions on
pgadmin_1 | /var/lib/pgadmin to allow pgadmin to write to it.
pgadmin_1 | ERROR : Failed to create the directory /var/lib/pgadmin/sessions:
pgadmin_1 | [Errno 13] Permission denied: '/var/lib/pgadmin/sessions'
pgadmin_1 | HINT : Create the directory /var/lib/pgadmin/sessions, ensure it is writeable by
pgadmin_1 | 'pgadmin', and try again, or, create a config_local.py file
pgadmin_1 | and override the SESSION_DB_PATH setting per
pgadmin_1 | https://www.pgadmin.org/docs/pgadmin4/4.27/config_py.html
pgadmin_1 | /usr/local/lib/python3.8/os.py:1023: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
pgadmin_1 | return io.open(fd, *args, **kwargs)
pgadmin_1 | [2020-11-11 05:50:13 +0000] [224] [INFO] Booting worker with pid: 224
my docker-compose.yml:
### pgAdmin ##############################################
pgadmin:
image: dpage/pgadmin4:latest
environment:
- "PGADMIN_DEFAULT_EMAIL=${PGADMIN_DEFAULT_EMAIL}"
- "PGADMIN_DEFAULT_PASSWORD=${PGADMIN_DEFAULT_PASSWORD}"
ports:
- "${PGADMIN_PORT}:80"
volumes:
- ${DATA_PATH_HOST}/pgadmin:/var/lib/pgadmin
depends_on:
- postgres
networks:
- frontend
- backend
Okay. looks like problem appears when you try to run pgadmin service.
This part
### pgAdmin ##############################################
pgadmin:
image: dpage/pgadmin4:latest
environment:
- "PGADMIN_DEFAULT_EMAIL=${PGADMIN_DEFAULT_EMAIL}"
- "PGADMIN_DEFAULT_PASSWORD=${PGADMIN_DEFAULT_PASSWORD}"
ports:
- "${PGADMIN_PORT}:80"
volumes:
- ${DATA_PATH_HOST}/pgadmin:/var/lib/pgadmin
depends_on:
- postgres
networks:
- frontend
- backend
As you can see you trying to mount local directory ${DATA_PATH_HOST}/pgadmin into container's /var/lib/pgadmin
volumes:
- ${DATA_PATH_HOST}/pgadmin:/var/lib/pgadmin
As you can read in this article your local ${DATA_PATH_HOST}/pgadmin directory's UID and GID must be 5050. Is this 5050?
You can check it by running
ls -l ${DATA_PATH_HOST}
Output will be like
drwxrwxr-x 1 5050 5050 12693 Nov 11 14:56 pgadmin
or
drwxrwxr-x 1 SOME_USER SOME_GROUP 12693 Nov 11 14:56 pgadmin
if SOME_USER's and SOME_GROUP's IDs are 5050, it is okay. 5050 as is also okay. If not, try to do as described in article above.
sudo chown -R 5050:5050 ${DATA_PATH_HOST}/pgadmin
Also you need to check is environment variable exists:
# run it as same user as you running docker-compose
echo ${DATA_PATH_HOST}
If output will be empty you need to set ${DATA_PATH_HOST} or allow docker to read variables from file. There are many ways to do it.
If you're on Windows, add this line to your docker-compose.yml. It gives container an access to your local folder
version: "3.9"
services:
postgres:
user: root <- this one
container_name: postgres_container
image: postgres:14.2
When running in kubernetes environment, I had to add these values.
spec:
containers:
- name: pgadmin
image: dpage/pgadmin4:5.4
securityContext:
runAsUser: 0
runAsGroup: 0
I can't connect MongoDB to my LoopBack framework.
I can connect into database with Mongo Express. I can create db and collections. But it's running on localhost. From my app i need to connect with mongo:27017.
docker-compose.yml
version: "3.7"
services:
web:
build:
context: .
dockerfile: .docker/node/Dockerfile
volumes:
- .:/home/node/app
ports:
- 3000:3000
depends_on:
- mongo
links:
- mongo
mongo:
image: mongo:latest
restart: always
volumes:
- ./src/datasources/mongodb:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
Dockerfile
# Check out https://hub.docker.com/_/node to select a new base image
FROM node:10-slim
# Set to a non-root built-in user `node`
USER node
# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY --chown=node package*.json ./
RUN npm install
# Bundle app source code
COPY --chown=node . /home/node/app
RUN npm run build
# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000
EXPOSE ${PORT}
CMD [ "node", "." ]
LoopBack datasources - mongo-db.datasource.config.json
{
"name": "MongoDB",
"connector": "mongodb",
"url": "",
"host": "mongo",
"port": 27017,
"user": "root",
"password": "sportee",
"database": "sportee",
"useNewUrlParser": true
}
docker error
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at Timeout.waitQueueMember.timer.setTimeout [as _onTimeout] (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:430:30)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
Emitted 'error' event at:
at MongoDbDataSource.postInit (/home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:502:16)
at onError (/home/node/app/node_modules/loopback-connector-mongodb/lib/mongodb.js:316:21)
at /home/node/app/node_modules/loopback-connector-mongodb/lib/mongodb.js:324:9
at /home/node/app/node_modules/mongodb/lib/utils.js:722:9
at err (/home/node/app/node_modules/mongodb/lib/mongo_client.js:216:23)
at connectCallback (/home/node/app/node_modules/mongodb/lib/operations/connect.js:350:5)
at topology.connect.err (/home/node/app/node_modules/mongodb/lib/operations/connect.js:583:14)
at Object.selectServer.err [as callback] (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:285:11)
at Timeout.waitQueueMember.timer.setTimeout [as _onTimeout] (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:435:25)
[... lines matching original stack trace ...]
Unhandled error in GET /users: 500 Error: Timeout in connecting after 5000 ms
at Timeout._onTimeout (/home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:2640:10)
at /home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:343:12
Can someone help please? :)
UPDATE:
I resolve my problem with adding networks into my docker-compose.yml
networks:
app-tier:
driver: bridge
me-tier:
driver: bridge
Today I've been trying to setup Docker for my GraphQL API that runs with Knex.js, PostgreSQL and Node.js. The problem that I'm facing is that Knex.js is timing out when trying to access data in my database. I believe that it's due to my incorrect way of trying to link them together.
I've really tried to do this on my own, but I can't figure this out. I'd like to walk through each file that play a part of making this work so (hopefully) someone could spot my mistake(s) and help me.
knexfile.js
In my knexfile I have a connection key which used to look like this:
connection: 'postgres://localhost/devblog'
This worked just fine, but this wont work if I want to use Docker. So I modified it a bit and ended up with this:
connection: {
host: 'db' || 'localhost',
port: process.env.DB_PORT || 5432,
user: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD || undefined,
database: process.env.DATABASE // DATABASE = devblog
}
I've noticed that something is wrong with host. Since it always times-out when I have something else (in this case, db) than localhost.
Dockerfile
My Dockerfile looks like this:
FROM node:9
WORKDIR /app
COPY package-lock.json /app
COPY package.json /app
RUN npm install
COPY dist /app
COPY wait-for-it.sh /app
CMD node index.js
EXPOSE 3010
docker-compose.yml
And this file looks like this:
version: "3"
services:
redis:
image: redis
networks:
- webnet
db:
image: postgres
networks:
- webnet
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: martinnord
POSTGRES_DB: devblog
web:
image: node:8-alpine
command: "node /app/dist/index.js"
volumes:
- ".:/app"
working_dir: "/app"
depends_on:
- "db"
ports:
- "3010:3010"
environment:
DB_PASSWORD: password
DB_USER: martinnord
DB_NAME: devblog
DB_HOST: db
REDIS_HOST: redis
networks:
webnet:
When I try to run this with docker-compose up I get the following output:
Starting backend_db_1 ... done
Starting backend_web_1 ... done
Attaching to backend_db_1, backend_redis_1, backend_web_1
redis_1 | 1:C 12 Feb 16:05:21.303 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 12 Feb 16:05:21.303 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
db_1 | 2018-02-12 16:05:21.337 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
redis_1 | 1:C 12 Feb 16:05:21.303 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 12 Feb 16:05:21.311 * Running mode=standalone, port=6379.
db_1 | 2018-02-12 16:05:21.338 UTC [1] LOG: listening on IPv6 address "::", port 5432
redis_1 | 1:M 12 Feb 16:05:21.311 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 12 Feb 16:05:21.314 # Server initialized
redis_1 | 1:M 12 Feb 16:05:21.315 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
db_1 | 2018-02-12 16:05:21.348 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2018-02-12 16:05:21.367 UTC [20] LOG: database system was shut down at 2018-02-12 16:01:17 UTC
redis_1 | 1:M 12 Feb 16:05:21.317 * DB loaded from disk: 0.002 seconds
redis_1 | 1:M 12 Feb 16:05:21.317 * Ready to accept connections
db_1 | 2018-02-12 16:05:21.374 UTC [1] LOG: database system is ready to accept connections
web_1 | DB_HOST db
web_1 |
web_1 | App listening on 3010
web_1 | Env: undefined
But when I try to make a query with GraphQL I get:
"message": "Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?"
I really don't know why this is not working for me and it's driving me nuts. If anyone could help me with this I would be delighted. I have also added a link to my project below.
Thanks a lot for reading! Cheers.
LINK TO PROJECT: https://github.com/Martinnord/DevBlog/tree/master/backend
Updated docker-compose file:
version: "3"
services:
redis:
image: redis
networks:
- webnet
db:
image: postgres
networks:
- webnet
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: martinnord
POSTGRES_DB: devblog
ports:
- "15432:5432"
web:
image: devblog-server
ports:
- "3010:3010"
networks:
- webnet
environment:
DB_PASSWORD: password
DB_USER: martinnord
DB_NAME: devblog
DB_HOST: db
REDIS_HOST: redis
command: ["./wait-for-it.sh", "db:5432", "--", "node", "index.js"]
networks:
webnet:
Maybe like this:
version: "3"
services:
redis:
image: redis
db:
image: postgres
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: martinnord
POSTGRES_DB: devblog
ports:
- "15432:5432"
web:
image: node:8-alpine
command: "node /app/dist/index.js"
volumes:
- ".:/app"
working_dir: "/app"
depends_on:
- "db"
ports:
- "3010:3010"
links:
- "db"
- "redis"
environment:
DB_PASSWORD: password
DB_USER: martinnord
DB_NAME: devblog
DB_HOST: db
REDIS_HOST: redis
And you should be able to connect from webapp to postgres with:
postgres://martinnord:password#db/devblog
or
connection: {
host: process.DB_HOST,
port: process.env.DB_PORT || 5432,
user: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME || 'postgres'
}
I also added row that exposes postgres running in docker to a port 15432 so you can try to connect it first directly from your host machine with
psql postgres://martinnord:password#localhost:15432/devblog