I have the staging and production environments on same VPS, so I can not use same port for these environments.
According the following docker-copmpose.yml, first we need to start the Database service, when run the migrations
by sh -c "npm run "Database migration"
, when start the web application by node FrontServerEntryPoint.js --environment staging
:
version: "3.5"
services:
front_server:
image: example-front_server-staging
container_name: Example-FrontServer-Staging
build:
context: .
dockerfile: "FrontServer.Dockerfile"
ports: [ "442:442" ]
depends_on: [ Database ]
command: sh -c "npm run "Database migration" && node FrontServerEntryPoint.js --environment staging"
Database:
image: postgres:16.2
container_name: Example-Database-Staging
ports: [ "5431:5432" ]
environment:
- TZ="Asia/Tokyo"
- POSTGRES_PASSWORD=EXAMPLE-NOTHING_TO_STEAL
volumes:
- DatabaseData:/data/example.jp
volumes:
DatabaseData:
name: Example-DatabaseData-Staging
driver: local
Although the FrontServer.Dockerfile
does not matter for this question, I’ll post it to make you sure the it is so:
FROM node:22-alpine
WORKDIR /var/www/example.com
COPY . /var/www/example.com
RUN npm install --no-package-lock
When I have run the migrations, I got the following error:
Example-Production-FrontServer | Error: connect ECONNREFUSED 172.18.0.2:5431
Example-Production-FrontServer | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16) {
Example-Production-FrontServer | errno: -111,
Example-Production-FrontServer | code: 'ECONNREFUSED',
Example-Production-FrontServer | syscall: 'connect',
Example-Production-FrontServer | address: '172.18.0.2',
Example-Production-FrontServer | port: 5431
Example-Production-FrontServer | }
I can’t publish the IP address of my VPS, but connection from the local machine via this IP address and 5431 works fine:
I to change ports: [ "5431:5432" ]
to ports: [ "5432:5432" ]
everything will work, but as I have said previously, it will not the solution in my case.
Looks like I have not understood the meaning of ports: [ "5431:5432" ]
. The connection for the first port will be available from the outside, but don’t the second one actual only for Example-Database-Staging
container?