Here I am using postgresql image and node official image and
my docker-compose.yml file looks like
version: '2'
services:
postgres:
restart: always
image: sameersbn/postgresql:9.6-2
ports:
- "5432:5432"
environment:
- DB_USER=shorturl
- DB_PASS=shorturl
- DB_NAME=shorturl
web:
build: .
ports:
- "4000:4000"
volumes:
- .:/shortlr
depends_on:
- postgres
command: ["./wait-for-it.sh","postgres:5432", "--", "npm", "start"]
and when i run docker-compose up then my logs looks like
shubham#shuboy2014:~/shortlr$ docker-compose up
Recreating shortlr_postgres_1 ...
Recreating shortlr_postgres_1 ... done
Recreating shortlr_web_1 ...
Recreating shortlr_web_1 ... done
Attaching to shortlr_postgres_1, shortlr_web_1
postgres_1 | Initializing datadir...
web_1 | wait-for-it.sh: waiting 15 seconds for postgres:5432
postgres_1 | Initializing certdir...
postgres_1 | Initializing logdir...
postgres_1 | Initializing rundir...
postgres_1 | Setting resolv.conf ACLs...
postgres_1 | Creating database user: shorturl
postgres_1 | Creating database: shorturl...
postgres_1 | ‣ Granting access to shorturl user...
postgres_1 | Starting PostgreSQL 9.6...
postgres_1 | LOG: database system was shut down at 2017-04-28 14:23:20 UTC
postgres_1 | LOG: MultiXact member wraparound protections are now enabled
postgres_1 | LOG: autovacuum launcher started
postgres_1 | LOG: database system is ready to accept connections
postgres_1 | LOG: incomplete startup packet
web_1 | wait-for-it.sh: postgres:5432 is available after 2 seconds
web_1 | npm info it worked if it ends with ok
web_1 | npm info using npm#4.2.0
web_1 | npm info using node#v7.9.0
web_1 | npm info lifecycle shortlr#0.0.5~prestart: shortlr#0.0.5
web_1 | npm info lifecycle shortlr#0.0.5~start: shortlr#0.0.5
web_1 |
web_1 | > shortlr#0.0.5 start /shortlr
web_1 | > node server.js
web_1 |
web_1 | Listening on http://localhost:4000/
web_1 | Unhandled rejection SequelizeBaseError: connect ECONNREFUSED 127.0.0.1:5432
web_1 | at /shortlr/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:98:20
web_1 | at Connection.<anonymous> (/shortlr/node_modules/pg/lib/client.js:186:5)
web_1 | at emitOne (events.js:96:13)
web_1 | at Connection.emit (events.js:191:7)
web_1 | at Socket.<anonymous> (/shortlr/node_modules/pg/lib/connection.js:86:10)
web_1 | at emitOne (events.js:96:13)
web_1 | at Socket.emit (events.js:191:7)
web_1 | at emitErrorNT (net.js:1283:8)
web_1 | at _combinedTickCallback (internal/process/next_tick.js:80:11)
web_1 | at process._tickCallback (internal/process/next_tick.js:104:9)
postgresql shutdown immediately when i run docker-compose up and any helpful answers will be appreciated.
shubham#shuboy2014:~/shortlr$ docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------
shortlr_postgres_1 /sbin/entrypoint.sh Up 0.0.0.0:5432->5432/tcp
shortlr_web_1 ./wait-for-it.sh postgres: ... Up 0.0.0.0:4000->4000/tcp
Add a link between your web and postgres image like so:
version: '2'
services:
postgres:
restart: always
image: sameersbn/postgresql:9.6-2
ports:
- "5432:5432"
environment:
- DB_USER=shorturl
- DB_PASS=shorturl
- DB_NAME=shorturl
web:
build: .
ports:
- "4000:4000"
volumes:
- .:/shortlr
depends_on:
- postgres
links:
- postgres
command: ["./wait-for-it.sh","postgres:5432", "--", "npm", "start"]
Although you should use networks instead because links is a legacy option. Read the docs on networks here
Related
I have an express API running in a node container and a redis container.
When attempting to connect the node container to redis I am greeted with the following error.
api_1 | events.js:291
api_1 | throw er; // Unhandled 'error' event
api_1 | ^
api_1 |
api_1 | Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
api_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
api_1 | Emitted 'error' event on RedisAdapter instance at:
api_1 | at RedisClient.onError (/usr/src/api/node_modules/socket.io-redis/dist/index.js:65:22)
api_1 | at RedisClient.emit (events.js:314:20)
api_1 | at RedisClient.on_error (/usr/src/api/node_modules/redis/index.js:342:14)
api_1 | at Socket.<anonymous> (/usr/src/api/node_modules/redis/index.js:223:14)
api_1 | at Socket.emit (events.js:314:20)
api_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
api_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
api_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21) {
api_1 | errno: 'ECONNREFUSED',
api_1 | code: 'ECONNREFUSED',
api_1 | syscall: 'connect',
api_1 | address: '127.0.0.1',
api_1 | port: 6379
api_1 | }
Here the code that is establishing the connection:
const redisClient = require("redis").createClient({host: process.env.REDIS_HOST ? process.env.REDIS_HOST : 'localhost'});
I have tried changing 'localhost' to "redis" to "redis://redis:6379". Nothing works.
As I understand it "localhost" in the context of a container refers to its own internal network, however both nodejs and redis are running on the same network which I've specified in my docker compose file.
Docker compose snippet
api:
build: ./api
ports:
- "3004:3004"
volumes:
- type: bind
source: ./api
target: /usr/src/api
working_dir: /usr/src/api
depends_on:
- redis
networks:
- backend
redis:
image: "redis:latest"
ports:
- "6379:6379"
hostname: redis
networks:
- backend
```
What could this be. All the "solutions" that I've managed to find through research don't work for me.
Just to close the question, when trying to connect to localhost inside the container it assumes the container itself and because the node.js container doesnt publish on the redis port the connection is refused. On the other hand because using docker-compose, you get a default docker network binding them together so using the container\service names as internal secured DNS records works.
for a more detailed explenation of the docker networking inner works see this thread
When trying to docker-compose up I get errors from the frontend and backend where they exit with code 1. docker ps shows that the postgres container is still running but the frontend and backend still exit. Using npm start, there is no errors. I don't know if it this helps, but my files do not copy from my src folder to /usr/src/app/ so maybe there is an error with my docker-compose or Dockerfiles.
Docker ps shows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
509208b2243b postgres:latest "docker-entrypoint.s…" 14 hours ago Up 11 minutes 0.0.0.0:5432->5432/tcp example_db_1
docker-compose.yml
version: '3'
services:
frontend:
build: ./frontend
volumes:
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
- ./frontend/build:/usr/share/nginx/html
ports:
- 80:80
- 443:443
depends_on:
- backend
backend:
build: ./backend
volumes:
- ./backend/src:/usr/src/app/src
- ./data/certbot/conf:/etc/letsencrypt
ports:
- 3000:3000
depends_on:
- db
db:
image: postgres:latest
restart: always
environment:
POSTGRES_USER: example
POSTGRES_PASSWORD: example1234
POSTGRES_DB: example
ports:
- 5432:5432
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
# Automatic certificate renewal
# entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
This is what the backend Dockerfile looks like.
FROM node:current-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app/
COPY package*.json /usr/src/app/
RUN npm install
COPY . /usr/src/app/
EXPOSE 3000
ENV NODE_ENV=production
CMD ["npm", "start"]
And the output error:
example_db_1 is up-to-date
Starting example_certbot_1 ... done
Starting example_backend_1 ... done
Starting example_frontend_1 ... done
Attaching to example_db_1, example_certbot_1, example_backend_1, example_frontend_1
backend_1 |
backend_1 | > example-backend#1.0.0 start /usr/src/app
backend_1 | > npx tsc; node ./out/
backend_1 |
certbot_1 | Saving debug log to /var/log/letsencrypt/letsencrypt.log
certbot_1 | Certbot doesn't know how to automatically configure the web server on this system. However, it can still get a certificate for you. Please run "certbot certonly" to do so. You'll need to manually configure your web server to use the resulting certificate.
frontend_1 | 2020/02/13 11:35:59 [emerg] 1#1: open() "/etc/letsencrypt/options-ssl-nginx.conf" failed (2: No such file or directory) in /etc/nginx/conf.d/app.conf:21
frontend_1 | nginx: [emerg] open() "/etc/letsencrypt/options-ssl-nginx.conf" failed (2: No such file or directory) in /etc/nginx/conf.d/app.conf:21
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 | The default text search configuration will be set to "english".
db_1 |
db_1 | Data page checksums are disabled.
db_1 |
db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1 | creating subdirectories ... ok
db_1 | selecting dynamic shared memory implementation ... posix
db_1 | selecting default max_connections ... 100
db_1 | selecting default shared_buffers ... 128MB
db_1 | selecting default time zone ... Etc/UTC
db_1 | creating configuration files ... ok
db_1 | running bootstrap script ... ok
db_1 | performing post-bootstrap initialization ... ok
db_1 | syncing data to disk ... ok
db_1 |
db_1 | initdb: warning: enabling "trust" authentication for local connections
db_1 | You can change this by editing pg_hba.conf or using the option -A, or
db_1 | --auth-local and --auth-host, the next time you run initdb.
db_1 |
db_1 | Success. You can now start the database server using:
db_1 |
db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1 |
db_1 | waiting for server to start....2020-02-12 21:51:40.137 UTC [43] LOG: starting PostgreSQL 12.1 (Debian 12.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-02-12 21:51:40.147 UTC [43] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-02-12 21:51:40.229 UTC [44] LOG: database system was shut down at 2020-02-12 21:51:39 UTC
db_1 | 2020-02-12 21:51:40.240 UTC [43] LOG: database system is ready to accept connections
db_1 | done
db_1 | server started
db_1 | CREATE DATABASE
db_1 |
db_1 |
db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1 |
db_1 | 2020-02-12 21:51:40.606 UTC [43] LOG: received fast shutdown request
db_1 | waiting for server to shut down....2020-02-12 21:51:40.608 UTC [43] LOG: aborting any
active transactions
db_1 | 2020-02-12 21:51:40.614 UTC [43] LOG: background worker "logical replication launcher" (PID 50) exited with exit code 1
db_1 | 2020-02-12 21:51:40.614 UTC [45] LOG: shutting down
db_1 | 2020-02-12 21:51:40.652 UTC [43] LOG: database system is shut down
db_1 | done
db_1 | server stopped
db_1 |
db_1 | PostgreSQL init process complete; ready for start up.
db_1 |
db_1 | 2020-02-12 21:51:40.728 UTC [1] LOG: starting PostgreSQL 12.1 (Debian 12.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-02-12 21:51:40.729 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2020-02-12 21:51:40.729 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2020-02-12 21:51:40.748 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-02-12 21:51:40.788 UTC [61] LOG: database system was shut down at 2020-02-12 21:51:40 UTC
db_1 | 2020-02-12 21:51:40.799 UTC [1] LOG: database system is ready to accept connections
db_1 | 2020-02-13 09:51:41.562 UTC [787] LOG: invalid length of startup packet
db_1 | 2020-02-13 11:09:27.384 UTC [865] FATAL: password authentication failed for user "postgres"
db_1 | 2020-02-13 11:09:27.384 UTC [865] DETAIL: Role "postgres" does not exist.
db_1 | Connection matched pg_hba.conf line 95: "host all all all md5"
db_1 | 2020-02-13 11:32:18.771 UTC [1] LOG: received smart shutdown request
db_1 | 2020-02-13 11:32:18.806 UTC [1] LOG: background worker "logical replication launcher"
(PID 67) exited with exit code 1
db_1 | 2020-02-13 11:32:18.806 UTC [62] LOG: shutting down
db_1 | 2020-02-13 11:32:18.876 UTC [1] LOG: database system is shut down
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2020-02-13 11:33:01.343 UTC [1] LOG: starting PostgreSQL 12.1 (Debian 12.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-02-13 11:33:01.343 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2020-02-13 11:33:01.343 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2020-02-13 11:33:01.355 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-02-13 11:33:01.427 UTC [23] LOG: database system was shut down at 2020-02-13 11:32:18 UTC
db_1 | 2020-02-13 11:33:01.466 UTC [1] LOG: database system is ready to accept connections
example_certbot_1 exited with code 1
example_frontend_1 exited with code 1
backend_1 | Authenticating with database...
backend_1 | internal/fs/utils.js:220
backend_1 | throw err;
backend_1 | ^
backend_1 |
backend_1 | Error: ENOENT: no such file or directory, open '/etc/letsencrypt/live/example.org/privkey.pem'
backend_1 | at Object.openSync (fs.js:440:3)
backend_1 | at Object.readFileSync (fs.js:342:35)
backend_1 | at Object.<anonymous> (/usr/src/app/out/index.js:68:23)
backend_1 | at Module._compile (internal/modules/cjs/loader.js:955:30)
backend_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
backend_1 | syscall: 'open', 811:32)
backend_1 | code: 'ENOENT', loader.js:723:14)
backend_1 | path: '/etc/letsencrypt/live/example.org/s/loader.js:1043:10)privkey.pem'
backend_1 | }
backend_1 | npm ERR! code ELIFECYCLE
backend_1 | npm ERR! errno 1
backend_1 | npm ERR! example-backend#1.0.0 start: `npx tsc; noprivkey.pem'de ./out/`
backend_1 | npm ERR! Exit status 1
backend_1 | npm ERR!
backend_1 | npm ERR! Failed at the example-backend#1.0.0 startde ./out/` script.
backend_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above. script.
backend_1 | here is likely additional logging ou
backend_1 | npm ERR! A complete log of this run can be found in:
backend_1 | npm ERR! /root/.npm/_logs/2020-02-13T11_36_10_3:30Z-debug.log 30Z-debug.log
example_backend_1 exited with code 1
There are no errors with certbot when run outside this project.
Directory structure:
src/
- docker-compose.yml
- init.letsencrypt.sh
- .gitignore
backend
src
- Dockerfile
- package.json
- .gitignore
data
nginx
- app.conf
frontend
src
- Dockerfile
- package.json
- .gitignore
Any help would be appreciated, thanks.
Updated nginx.conf:
server {
listen 80;
server_name example.org;
location / {
root /var/www/html/;
index index.html;
autoindex on;
}
location /frontend {
proxy_pass http://example.org:8080;
try_files $uri /public/index.html;
}
location /backend {
proxy_pass http://example.org:3000;
}
location /db {
proxy_pass http://example.org:5432;
}
}
new error when changing .gitignore:
frontend_1 | 2020/02/13 16:34:58 [emerg] 1#1: cannot load certificate "/etc/letsencrypt/live/example.org/fullchain.pem":
BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/etc/letsencrypt/live/example.org/fullchain.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)
frontend_1 | nginx: [emerg] cannot load certificate "/etc/letsencrypt/live/example.org/fullchain.pem": BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/etc/letsencrypt/live/example.org/fullchain.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)
example_frontend_1 exited with code 1
The setup seems very complicated. My advice for you: Try to reduce the complicated overhead with the certbot as own docker-container.
#docker-compose.yml
version: '3'
services:
frontend:
build: ./frontend
volumes:
- ./data/nginx:/etc/nginx/conf.d
# no source-code of the frontend via volumes -
# only a nginx-image with your source.
# nginx-conf as volume is valid.
ports:
- 8080:80
depends_on:
- backend
backend:
build: ./backend
# dont put your source as volume in,
# your docker-image should contains the whole code
# and no certbot magic here
ports:
- 3000:3000
depends_on:
- db
db:
image: postgres:latest
restart: always
environment:
POSTGRES_USER: example
POSTGRES_PASSWORD: example1234
POSTGRES_DB: example
ports:
- 5432:5432
This is much cleaner and easy to read. Now you should setup a reverse proxy on your hostmachine (easy with nginx). Configure your published ports into your nginx-reverse-proxy (proxy_pass localhost:8080 for your frontend as example).
After that you can install the certbot and obtain your lets-encrypt-certificates. The certbot should discover your nginx-endpoints automatically and can automatic renew your certificates.
I have a docker file and docker compose fiel in my project directory. I am running the docker compose file with the following command
docker-compose up
It builds and runs the different images for the server and database, but I am getting an error that is saying my package.json file is not in the correct directory. I am not sure where it is going wrong.
Here is my docker file
FROM node:10
WORKDIR /app
COPY package.json ./app
RUN npm install
COPY . /app
CMD npm start
EXPOSE 5585
this is my docker compose file
web:
image: node
command: npm start
ports:
- "5585:5588"
links:
- db
working_dir: /app
environment:
SEQ_DB: addidas
SEQ_USER: sdfsdf
SEQ_PW: sdfsdfs
PORT: 4242
DATABASE_URL: postgres://sdfsdf:sdfsdfs#localhost:5432/addidas
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: sdfsdf
POSTGRES_PASSWORD: sdfsdfs
the error that i am getting in my terminal is the following :
Attaching to addidas_db_1, addidas_web_1
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 | The default text search configuration will be set to "english".
db_1 |
db_1 | Data page checksums are disabled.
db_1 |
db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1 | creating subdirectories ... ok
db_1 | selecting default max_connections ... 100
db_1 | selecting default shared_buffers ... 128MB
db_1 | selecting dynamic shared memory implementation ... posix
db_1 | creating configuration files ... ok
db_1 | running bootstrap script ... ok
db_1 | performing post-bootstrap initialization ... ok
db_1 | syncing data to disk ... ok
db_1 |
db_1 | Success. You can now start the database server using:
db_1 |
db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1 |
db_1 |
db_1 | WARNING: enabling "trust" authentication for local connections
db_1 | You can change this by editing pg_hba.conf or using the option -A, or
db_1 | --auth-local and --auth-host, the next time you run initdb.
db_1 | waiting for server to start....2018-11-06 17:38:51.968 UTC [43] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2018-11-06 17:38:51.983 UTC [44] LOG: database system was shut down at 2018-11-06 17:38:51 UTC
db_1 | 2018-11-06 17:38:51.987 UTC [43] LOG: database system is ready to accept connections
db_1 | done
db_1 | server started
db_1 | CREATE DATABASE
db_1 |
db_1 |
db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1 |
db_1 | waiting for server to shut down...2018-11-06 17:38:52.438 UTC [43] LOG: received fast shutdown request
db_1 | .2018-11-06 17:38:52.441 UTC [43] LOG: aborting any active transactions
db_1 | 2018-11-06 17:38:52.443 UTC [43] LOG: background worker "logical replication launcher" (PID 50) exited with exit code 1
db_1 | 2018-11-06 17:38:52.444 UTC [45] LOG: shutting down
db_1 | 2018-11-06 17:38:52.459 UTC [43] LOG: database system is shut down
db_1 | done
db_1 | server stopped
db_1 |
db_1 | PostgreSQL init process complete; ready for start up.
db_1 |
db_1 | 2018-11-06 17:38:52.556 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2018-11-06 17:38:52.556 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2018-11-06 17:38:52.560 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2018-11-06 17:38:52.575 UTC [61] LOG: database system was shut down at 2018-11-06 17:38:52 UTC
db_1 | 2018-11-06 17:38:52.580 UTC [1] LOG: database system is ready to accept connections
db_1 | 2018-11-06 17:46:15.922 UTC [1] LOG: received smart shutdown request
db_1 | 2018-11-06 17:46:15.926 UTC [1] LOG: background worker "logical replication launcher" (PID 67) exited with exit code 1
db_1 | 2018-11-06 17:46:15.928 UTC [62] LOG: shutting down
db_1 | 2018-11-06 17:46:15.944 UTC [1] LOG: database system is shut down
db_1 | 2018-11-06 17:46:19.284 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2018-11-06 17:46:19.284 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2018-11-06 17:46:19.288 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2018-11-06 17:46:19.309 UTC [24] LOG: database system was shut down at 2018-11-06 17:46:15 UTC
db_1 | 2018-11-06 17:46:19.316 UTC [1] LOG: database system is ready to accept connections
web_1 | npm ERR! path /app/package.json
web_1 | npm ERR! code ENOENT
web_1 | npm ERR! errno -2
web_1 | npm ERR! syscall open
web_1 | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
web_1 | npm ERR! enoent This is related to npm not being able to find a file.
web_1 | npm ERR! enoent
web_1 |
web_1 | npm ERR! A complete log of this run can be found in:
web_1 | npm ERR! /root/.npm/_logs/2018-11-06T17_47_14_825Z-debug.log
addidas_web_1 exited with code 254
You are not using your docker image your docker-compose.yml.
You should point to your Dockerfile:
web:
build: ./path/to/Dockerfile
There is also some mistakes with your configuration. You should share the containers (your web server and the database) on the same network to be able to access the database from the web server.
networks:
mynetwork:
driver: bridge
web:
build: ./path/to/Dockerfile
networks:
- mynetwork
links:
- db
environment:
SEQ_DB: addidas
SEQ_USER: sdfsdf
SEQ_PW: sdfsdfs
PORT: 4242
DATABASE_URL: postgres://sdfsdf:sdfsdfs#db:5432/addidas
db:
image: postgres
ports:
- "5432:5432"
networks:
- mynetwork
environment:
POSTGRES_USER: sdfsdf
POSTGRES_PASSWORD: sdfsdfs
I have created my react app with Node.js and postgresql and I deployed in google cloud. I created a docker image of postgres and nodejs and I uploaded images to docker hub. From gcloud I accessing Those images.
This is my docker-compose-production.yml file.
version: '2.0'
services:
postgres:
image : mycompany/myapp:pglatest
restart : always
volumes:
- ./backdata/databackups:/var/lib/postgresql/backdata
ports:
- "5433:5432"
backend:
image: mycompany/myapp:nodelatest7
command: npm run start
ports:
- "5001:5000"
depends_on:
- postgres
environment:
POSTGRES_URL: postgresql://postgres:root#postgres:5432/db_mydb
DEBUG: hms-backend:*
when I run command
sudo docker-compose -f docker-compose-production.yml up --build -d
2 images are created.
after that I have run tail command
sudo docker-compose -f docker-compose-production.yml logs -t backend
I'm getting error as
backend_1 | 2018-09-15T09:12:18.926001351Z REST API listening on port 5000
backend_1 | 2018-09-15T09:12:18.937246598Z error { Error: connect ECONNREFUSED 192.168.80.2:5432
backend_1 | 2018-09-15T09:12:18.937268668Z at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
backend_1 | 2018-09-15T09:12:18.937280934Z errno: 'ECONNREFUSED',
backend_1 | 2018-09-15T09:12:18.937283960Z code: 'ECONNREFUSED',
backend_1 | 2018-09-15T09:12:18.937286817Z syscall: 'connect',
backend_1 | 2018-09-15T09:12:18.937289488Z address: '192.168.80.2',
backend_1 | 2018-09-15T09:12:18.937292260Z port: 5432 }
How to solve this problem
For me your postgres url is wrong : postgresql://postgres:root#postgres:5432/db_mydb
It should be postgresql://postgres:root#postgres:5433/db_mydb since the postgres "exposed" port in 5433
Hum, by i think you should add "container_name" in the docker-compose
services:
postgres:
container_name: my_postgres
and use this name for the "adress" of your postgres
postgresql://my_postgres:root#postgres:5433/db_mydb
I try to set up Docker with my node.js app which uses sequelize to connect to postgres.
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASS,
{
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: 'postgres',
},
);
In my .env file I declare
DB_HOST=postgres (which is the name of the service declared in the docker-conpose.yml) and DB_PORT=5432 among all the other environment variables.
My Dockerfile looks as follows:
FROM node:8.6.0
# Working directory for application
WORKDIR /usr/src/app
EXPOSE 8080
COPY . /usr/src/app
# In this file I create a user and a DB and give him the privlages
ADD init.sql /docker-entrypoint-initdb.d/
RUN npm install
And my docker-compose.yml looks as follows:
version: "2"
services:
postgres:
image: "postgres:9.4"
restart: always
ports:
- "5432:5432"
env_file:
- .env
node:
build: .
ports:
- "8080:8080"
depends_on:
- postgres
command: ["npm", "start"]
When I docker-compose up I get the error that the Sequelize is not able to connect to the DB.
Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.2:5431
Can someone help me with this error?
all the docker logs:
WARNING: Image for service node was built because it did not already exist. To rebuild this image you must use docker-compose build or docker-compose up --build.
Creating graphqlpostgrestemplate_postgres_1 ...
Creating graphqlpostgrestemplate_postgres_1 ... done
Creating graphqlpostgrestemplate_node_1 ...
Creating graphqlpostgrestemplate_node_1 ... done
Attaching to graphqlpostgrestemplate_postgres_1, graphqlpostgrestemplate_node_1
postgres_1 | The files belonging to this database system will be owned by user "postgres".
postgres_1 | This user must also own the server process.
postgres_1 |
postgres_1 | The database cluster will be initialized with locale "en_US.utf8".
postgres_1 | The default database encoding has accordingly been set to "UTF8".
postgres_1 | The default text search configuration will be set to "english".
postgres_1 |
postgres_1 | Data page checksums are disabled.
postgres_1 |
postgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1 | creating subdirectories ... ok
postgres_1 | selecting default max_connections ... 100
postgres_1 | selecting default shared_buffers ... 128MB
postgres_1 | selecting dynamic shared memory implementation ... posix
postgres_1 | creating configuration files ... ok
postgres_1 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
postgres_1 | initializing pg_authid ... ok
postgres_1 | initializing dependencies ... ok
postgres_1 | creating system views ... ok
node_1 | npm info it worked if it ends with ok
node_1 | npm info using npm#5.3.0
node_1 | npm info using node#v8.6.0
postgres_1 | loading system objects' descriptions ... ok
node_1 | npm info lifecycle graphql-postgres-template#1.0.0~prestart: graphql-postgres-template#1.0.0
node_1 | npm info lifecycle graphql-postgres-template#1.0.0~start: graphql-postgres-template#1.0.0
node_1 |
node_1 | > graphql-postgres-template#1.0.0 start /usr/src/app
node_1 | > nodemon --exec babel-node index.js
node_1 |
postgres_1 | creating collations ... ok
postgres_1 | creating conversions ... ok
postgres_1 | creating dictionaries ... ok
postgres_1 | setting privileges on built-in objects ... ok
postgres_1 | creating information schema ... ok
postgres_1 | loading PL/pgSQL server-side language ... ok
node_1 | [nodemon] 1.12.1
node_1 | [nodemon] to restart at any time, enter rs
node_1 | [nodemon] watching: .
node_1 | [nodemon] starting babel-node index.js
postgres_1 | vacuuming database template1 ... ok
postgres_1 | copying template1 to template0 ... ok
postgres_1 | copying template1 to postgres ... ok
postgres_1 | syncing data to disk ... ok
postgres_1 |
postgres_1 | Success. You can now start the database server using:
postgres_1 |
postgres_1 | postgres -D /var/lib/postgresql/data
postgres_1 | or
postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1 |
postgres_1 |
postgres_1 | WARNING: enabling "trust" authentication for local connections
postgres_1 | You can change this by editing pg_hba.conf or using the option -A, or
postgres_1 | --auth-local and --auth-host, the next time you run initdb.
postgres_1 | ****************************************************
postgres_1 | WARNING: No password has been set for the database.
postgres_1 | This will allow anyone with access to the
postgres_1 | Postgres port to access your database. In
postgres_1 | Docker's default configuration, this is
postgres_1 | effectively any other container on the same
postgres_1 | system.
postgres_1 |
postgres_1 | Use "-e POSTGRES_PASSWORD=password" to set
postgres_1 | it in "docker run".
postgres_1 | ****************************************************
postgres_1 | waiting for server to start....LOG: could not bind IPv6 socket: Cannot assign requested address
postgres_1 | HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
postgres_1 | LOG: database system was shut down at 2017-10-10 12:17:15 UTC
postgres_1 | LOG: MultiXact member wraparound protections are now enabled
postgres_1 | LOG: database system is ready to accept connections
postgres_1 | LOG: autovacuum launcher started
postgres_1 | done
postgres_1 | server started
postgres_1 | ALTER ROLE
postgres_1 |
postgres_1 |
postgres_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres_1 |
postgres_1 | waiting for server to shut down....LOG: received fast shutdown request
postgres_1 | LOG: aborting any active transactions
postgres_1 | LOG: autovacuum launcher shutting down
postgres_1 | LOG: shutting down
postgres_1 | LOG: database system is shut down
node_1 | Tue, 10 Oct 2017 12:17:16 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules/sequelize/lib/sequelize.js:236:13
node_1 | WARNING: No configurations found in configuration directory:/usr/src/app/config
node_1 | WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment.
node_1 | Tue, 10 Oct 2017 12:17:17 GMT body-parser deprecated undefined extended: provide extended option at index.js:53:30
node_1 | Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.2:5431
node_1 | at connection.connect.err (/usr/src/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:96:24)
node_1 | at Connection.connectingErrorHandler (/usr/src/app/node_modules/pg/lib/client.js:123:14)
node_1 | at emitOne (events.js:115:13)
node_1 | at Connection.emit (events.js:210:7)
node_1 | at Socket. (/usr/src/app/node_modules/pg/lib/connection.js:71:10)
node_1 | at emitOne (events.js:115:13)
node_1 | at Socket.emit (events.js:210:7)
node_1 | at emitErrorNT (internal/streams/destroy.js:64:8)
node_1 | at _combinedTickCallback (internal/process/next_tick.js:138:11)
node_1 | at process._tickDomainCallback (internal/process/next_tick.js:218:9)
node_1 | [nodemon] clean exit - waiting for changes before restart
postgres_1 | done
postgres_1 | server stopped
postgres_1 |
postgres_1 | PostgreSQL init process complete; ready for start up.
postgres_1 |
postgres_1 | LOG: database system was shut down at 2017-10-10 12:17:16 UTC
postgres_1 | LOG: MultiXact member wraparound protections are now enabled
postgres_1 | LOG: database system is ready to accept connections
postgres_1 | LOG: autovacuum launcher started
To your Docker-compose add link configuration option in your service node point to service postgres something like this:
node:
links:
- postgres
Then you can connect to postgresdb point with name service postgres