Getting Docker image and Docker-Compose file to work correctly - node.js

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

Related

PostgreSQL not working on docker when volume is initialized

I am using windows docker
my docker-compose file is as shown below:
version: '3.5'
services:
postgres:
container_name: postgres_container
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-root}
PGDATA: /data/postgres
volumes:
- ./postgres-data:/var/
ports:
- "5432:5432"
restart: unless-stopped
When i build it i get following error log and container exits
Attaching to postgres_container
postgres_container | The files belonging to this database system will be owned by user "postgres".
postgres_container | This user must also own the server process.
postgres_container |
postgres_container | The database cluster will be initialized with locale "en_US.utf8".
postgres_container | The default database encoding has accordingly been set to "UTF8".
postgres_container | The default text search configuration will be set to "english".
postgres_container |
postgres_container | Data page checksums are disabled.
postgres_container |
postgres_container | fixing permissions on existing directory /data/postgres ... ok
postgres_container | creating subdirectories ... ok
postgres_container | selecting dynamic shared memory implementation ... posix
postgres_container | selecting default max_connections ... 100
postgres_container | selecting default shared_buffers ... 128MB
postgres_container | selecting default time zone ... Etc/UTC
postgres_container | creating configuration files ... ok
postgres_container | running bootstrap script ... ok
postgres_container | performing post-bootstrap initialization ... ok
postgres_container | syncing data to disk ... ok
postgres_container |
postgres_container |
postgres_container | Success. You can now start the database server using:
postgres_container |
postgres_container | pg_ctl -D /data/postgres -l logfile start
postgres_container |
postgres_container | initdb: warning: enabling "trust" authentication for local connections
postgres_container | You can change this by editing pg_hba.conf or using the option -A, or
postgres_container | --auth-local and --auth-host, the next time you run initdb.
postgres_container | waiting for server to start....2020-04-17 13:18:31.599 UTC [47] LOG: starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_container | 2020-04-17 13:18:31.607 UTC [47] LOG: could not bind Unix address "/var/run/postgresql/.s.PGSQL.5432": Input/output error
postgres_container | 2020-04-17 13:18:31.607 UTC [47] HINT: Is another postmaster already running on port 5432? If not, remove socket file "/var/run/postgresql/.s.PGSQL.5432" and retry.
postgres_container | 2020-04-17 13:18:31.607 UTC [47] WARNING: could not create Unix-domain socket in directory "/var/run/postgresql"
postgres_container | 2020-04-17 13:18:31.607 UTC [47] FATAL: could not create any Unix-domain sockets
postgres_container | 2020-04-17 13:18:31.610 UTC [47] LOG: database system is shut down
postgres_container | stopped waiting
postgres_container | pg_ctl: could not start server
postgres_container | Examine the log output.
postgres_container |
postgres_container | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_container |
postgres_container | 2020-04-17 13:18:32.246 UTC [1] LOG: starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_container | 2020-04-17 13:18:32.246 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_container | 2020-04-17 13:18:32.246 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_container | 2020-04-17 13:18:32.255 UTC [1] LOG: could not bind Unix address "/var/run/postgresql/.s.PGSQL.5432": Input/output error
postgres_container | 2020-04-17 13:18:32.255 UTC [1] HINT: Is another postmaster already running on port 5432? If not, remove socket file "/var/run/postgresql/.s.PGSQL.5432" and retry.
postgres_container | 2020-04-17 13:18:32.255 UTC [1] WARNING: could not create Unix-domain socket in directory "/var/run/postgresql"
postgres_container | 2020-04-17 13:18:32.255 UTC [1] FATAL: could not create any Unix-domain sockets
postgres_container | 2020-04-17 13:18:32.259 UTC [1] LOG: database system is shut down
postgres_container exited with code 1
I checked 5432 port its open and no process is using it.
when i remove volume from my docker-compose.yml file it works
perfectly
the volume i am using ./postgres-data is the local directory on my system i want to map it to the PostgreSQL container to restore database.
You are using docker on Windows and mounting the directory where the socket will be created (/var) as volume but windows filesystem doesn't support unix sockets.
Change the configuration in order to:
leave the unix socket (/var/run/postgresql/...) inside the docker without mounting as volume
mount data directory as volume

Docker-compose up exited with code 1 but successful docker-compose build

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.

Node Postgres Docker

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

Postgres Shutdown Immediately after docker compose up?

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

Create Docker image for NodeJS + PostgreSQL web application

I've been reading Docker's documentation, but I can't get around creating an image that will work.
I have a NodeJS application that uses PostgreSQL as database:
var connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/db';
var pg = require('pg');
var pgp = require('pg-promise')();
var db = pgp(connectionString);
db.func('some_storedProcedure').then(//...)
//...
I first created a Dockerfile according to Node's documentation for it:
FROM node:argon
# Create app directory
RUN mkdir -p /app
WORKDIR /app
# Install app dependencies
COPY package.json /app
RUN npm install
# Bundle app source
COPY . /app
EXPOSE 5000
CMD [ "npm", "start" ]
I then followed this post regarding connecting the database to it with docker-compose. the docker-compose.yml file looks like:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/app
links:
- db
environment:
DATABASE_URL: postgres://myuser:mypass#db:5432/db
db:
image: postgres
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypass
This is (some) of what is returned when I run docker-compose up with these files, after creating the image.
npm info ok
---> 87dbbae35721
Removing intermediate container c73f826a0b3d
Step 6 : COPY . /app
---> ec56bfc11d3c
Removing intermediate container 745ddf82d742
Step 7 : EXPOSE 5000
---> Running in b2be5aecd9d6
---> a7d126a7ea5e
Removing intermediate container b2be5aecd9d6
Step 8 : CMD npm start
---> Running in 0379d512c688
---> 266517f47311
Removing intermediate container 0379d512c688
Successfully built 266517f47311
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Starting imagename_db_1
Creating imagename_web_1
Attaching to imagename_db_1, imagename_web_1
web_1 | npm info it worked if it ends with ok
web_1 | npm info using npm#2.15.1
web_1 | npm info using node#v4.4.3
web_1 | npm info prestart SharedServer#5.8.0
web_1 | npm info start SharedServer#5.8.0
web_1 |
web_1 | > SharedServer#5.8.0 start /app
web_1 | > node index.js
web_1 |
web_1 | Wed, 27 Apr 2016 00:41:19 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at index.js:13:9
web_1 | Wed, 27 Apr 2016 00:41:19 GMT body-parser deprecated undefined extended: provide extended option at node_modules/body-parser/index.js:105:29
web_1 | Node app is running on port 5000
db_1 | LOG: database system was interrupted; last known up at 2016-04-25 00:17:59 UTC
db_1 | LOG: database system was not properly shut down; automatic recovery in progress
db_1 | LOG: invalid record length at 0/17076E8
db_1 | LOG: redo is not required
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: autovacuum launcher started
When I access http://localhost:5000, I see the web application running, but whenever I fire up something that tries to access the database, I get a HTTP 500 error with the following body
code: "28P01"
file: "auth.c"
length: 98
line: "285"
name: "error"
routine: "auth_failed"
severity: "FATAL"
What am I doing wrong? I'm not sure I understand what I'm doing with Docker, and the only thing I have for documentation are simple recipes to build specific environments (or at least, that's what I've understood)
Thanks.
Check your pg_hba.conf in $PGDATA allows connections from node.js.
By default the pg_hba.conf is like so:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
This is fine for your standard psql connectivity via the OS owner as it allows local connections trusted for localhost IP address 127.0.0.1. However if you have an IP address set on the server, which I'm guessing you do then you need to allow an entry for that because in your node.js configuration you're referring to a host of "DB". So ping "DB" and add that IP address:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
host db myuser <ip-for-db-host> md5
Once you've changed that file you will need to perform a pg_ctl reload.

Resources