Docker compose connection refused - node.js

I am getting a 'connection refused' error when trying to hit my NodeJS server running in a Docker container. If I try to cURL the server from the host machine, I get the error "curl: (56) Recv failure: Connection reset by peer".
When I run sudo docker-compose up -d it starts up my services and running sudo docker ps -a shows the following:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e56b30b1de9c mongo:4.1.8-xenial "docker-entrypoint.s…" About a minute ago Up About a minute 27017/tcp db
0d82b0a881e5 nodejs "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3000->3000/tcp nodejs
The containers are running on a server with IP '192.168.0.24' so I try to hit an endpoint in my 'nodejs' app via '192.168.0.24:3000/items' but this results in a connection refused error.
docker-compose.yml
version: '3'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile
image: nodejs
container_name: nodejs
restart: unless-stopped
env_file: .env
environment:
- MONGO_USERNAME=$MONGO_USERNAME
- MONGO_PASSWORD=$MONGO_PASSWORD
- MONGO_HOSTNAME=db
- MONGO_PORT=$MONGO_PORT
- MONGO_DB=$MONGO_DB
ports:
- "3000:3000"
volumes:
- .:/home/node/app
- /home/node/app/node_modules
networks:
- app-network
command: ./wait-for.sh db:27017 -- /home/node/app/node_modules/.bin/nodemon app.js
db:
image: mongo:4.1.8-xenial
container_name: db
restart: unless-stopped
env_file: .env
environment:
- MONGO_INITDB_ROOT_USERNAME=$MONGO_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASSWORD
volumes:
- dbdata:/data/db
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
dbdata:
node_modules:
Dockerfile
FROM node:10-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 3000
CMD [ "npm", "app.js" ]

Related

AWS EC2 dockerized Nodejs app to listen over ssl (443)

I have a nodejs api dockerized running on an EC2 instance. The app itself is running inside the container on port 5000 and is mapped via docker-compose to 5000:5000.
I actually wanted the API to listen on the port I can access via https so I tried mapping the ports in the docker-compose like this "443:5000". I set an inbound rule in the EC2 security group for SSL allow access from anywhere but when I hit the IP or DNS name into my browser it is not responding.
# Image source
FROM node:14-alpine
# Docker working directory
WORKDIR /app
# Copying file into APP directory of docker
COPY ./package.json /app/
RUN apk update && \
apk add git
# Then install the NPM module
RUN yarn install
# Copy current directory to APP folder
COPY . /app/
EXPOSE 5000
CMD ["npm", "run", "start:dev"]
Is there anything I was missing?
version: "3"
services:
nftapi:
env_file:
- .env
build:
context: .
ports:
- '443:5000'
depends_on:
- postgres
volumes:
- .:/app
- /app/node_modules
networks:
- postgres
postgres:
container_name: postgres
image: postgres:latest
ports:
- "5432:5432"
volumes:
- /data/postgres:/data/postgres
env_file:
- docker.env
networks:
- postgres
pgadmin:
links:
- postgres:postgres
container_name: pgadmin
image: dpage/pgadmin4
ports:
- "8080:80"
volumes:
- /data/pgadmin:/root/.pgadmin
env_file:
- docker.env
networks:
- postgres
networks:
postgres:
driver: bridge

Flask App runs locally but errors out on DigitalOcean

The following is my docker-compose.yml file. It has to do with my docker-compose.yml file.. I think. Like I said, the app builds locally but when I run "docker-compose up -d" in my DigitalOcean Droplet I get this error.
ERROR: Couldn't find env file: /root/.env
The following is my docker-compose.yml file.
version: '2'
services:
postgres:
image: 'postgres:9.5'
container_name: postgress
env_file:
- '.env'
volumes:
- 'postgres:/var/lib/postgresql/data'
ports:
- '5432:5432'
networks:
- db_nw
redis:
image: 'redis:3.0-alpine'
container_name: redis
command: redis-server --requirepass pass123456word
volumes:
- 'redis:/var/lib/redis/data'
ports:
- '6379:6379'
website:
restart: always
build: .
container_name: website
command: >
gunicorn -c "python:config.gunicorn" --reload "app.app:create_app()"
env_file:
- '.env'
volumes:
- '.:/app'
ports:
- 8000:8000
expose:
- 8000
networks:
- db_nw
- web_nw
depends_on:
- postgres
links:
- celery
- redis
- postgres
celery:
build: .
container_name: celery
command: celery worker -B -l info -A app.blueprints.contact.tasks
env_file:
- '.env'
volumes:
- '.:/app'
nginx:
restart: always
build: ./nginx
image: 'nginx:1.13'
container_name: nginx
volumes:
- /www/static
- .:/app
ports:
- 80:80
networks:
- web_nw
links:
- website
depends_on:
- website
networks:
db_nw:
driver: bridge
web_nw:
driver: bridge
volumes:
postgres:
redis:
My dockerfile
FROM python:3.7.5-slim-buster
RUN apt-get update \
&& apt-get install -qq -y \
build-essential libpq-dev --no-install-recommends
ENV INSTALL_PATH /app
RUN mkdir -p $INSTALL_PATH
WORKDIR $INSTALL_PATH
COPY requirements.txt requirements.txt
RUN pip install --upgrade pip -r requirements.txt
COPY . .
RUN pip install --editable .
CMD gunicorn -c "python:config.gunicorn" "app.app:create_app()"
Is something wrong with my volumes in my docker-compose.yml file? or am I doing something weird in my Dockerfile with the ENV to where its hard coded to a local machine rather than the "root" directory on DigitalOcean?F
I'm new to hosting docker images so this is my first go at something like this. Thanks!
When you access a Droplet, you're generally running as root.
You appear to have copied the docker-compose.yml correctly to the Droplet but you have not copied the .env file on which it depends to the Droplet's /root/.env.
If you copy the .env file to /root/.env on the Droplet, it should work.

“docker-compose up” Error : Cannot start service fetcher: OCI runtime create failed: container_linux.go:344

I'm running docker on a ubuntu server. For a while everything was working well, then the docker stoped and when I tried to put it up again i had this error :
ERROR: Service 'back' failed to build: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown
Here is my docker-compose.yml
version: "2"
services:
back:
build: ./crypto_feed_back
restart: always
command: node server.js
volumes:
- .:/usr/app/
front:
build: ./crypto_feed_front
restart: always
command: npm start
volumes:
- .:/usr/app/
depends_on:
- back
fetcher:
build: ./crypto_feed_fetch
restart: always
command: /usr/sbin/crond -l 2 -f
depends_on:
- postgres
postgres:
image: postgres:12-alpine
restart: always
environment:
POSTGRES_USER: crypto_feed
POSTGRES_DB: crypto_feed
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "5432:5432"
caddy:
image: elswork/arm-caddy:1.0.0
restart: always
environment:
ACME_AGREE: "true"
volumes:
- ./caddy/Caddyfile:/etc/Caddyfile
- ./caddy/certs:/root/.caddy
ports:
- 80:80/tcp
- 443:443/tcp
depends_on:
- front
And back's Dockerfile :
FROM node:alpine
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install --production
# If you are building your code for production
# RUN npm ci --only=production
COPY ./api/ ./api/
COPY ./server.js .
EXPOSE 3001
CMD npm start
If someone has an idea on how I can solve this, it would be really nice !

How to compose a docker-compose to run npm natively and mongo in a docker

I am trying to run node natively but run mongoDB in a container, just can't figure out the best way to do it.
Should run a command in the api dir and start npm?
I am trying to be able to deploy the whole application with a docker-compose up
Here are my files:
docker-compose.yml
/api
Dockerfile
package.json
/db
This is the docker-compose.yml
version: '3'
services:
api:
container_name: fl_api
command: cd api && npm start
ports:
- "3002:3002"
environment:
DB_PORT: "27017"
DB_HOST: mongo
PORT: "3002"
networks:
- api_net
mongo:
container_name : fl_mongodb
image: mongo:4.0
volumes:
- ./db/mong-vol:/data/db
networks:
- api_net
expose:
- "27017"
healthcheck:
test: echo 'db.stats().ok' | mongo localhost:27017/zenbrain --quiet
interval: 5s
timeout: 5s
retries: 12
networks:
api_net:
driver: bridge
This is the api/Dockerfile
FROM node:6
WORKDIR /food-license-backend
COPY package.json /food-license-backend
RUN npm install
COPY . /food-license-backend
EXPOSE 3002
CMD ["npm", "run", "start"]

Identical Docker images with different containers and ports not accessible

I'm running Docker host on my Windows dev machine and have 2 identifcal images exposing different ports (3000, 3001). Using the following docker-compose I build and run the containers but the container on port 3001 isn't available via localhost or my IP address.
DockerFile
FROM mhart/alpine-node:8
# Create an app directory (in the Docker container)
RUN mkdir -p /testdirectory
WORKDIR /testdirectory
COPY package.json /testdirectory
RUN npm install --loglevel=warn
COPY . /testdirectory
EXPOSE 3000
CMD ["node", "index.js"]
DockerFile
FROM mhart/alpine-node:8
# Create an app directory (in the Docker container)
RUN mkdir -p /test2directory
WORKDIR /test2directory
COPY package.json /test2directory
RUN npm install --loglevel=warn
COPY . /test2directory
EXPOSE 3001
CMD ["node", "index.js"]
Docker-Compose file
version: '3'
services:
testdirectory:
container_name: testdirectory
environment:
- DEBUG=1
- NODE_ENV=production
- NODE_NAME=testdirectory
- NODE_HOST=localhost
- NODE_PORT=3000
- DB_HOST=mongodb://mongo:27017/testdirectory
- DB_PORT=27017
build:
context: ./test-directory
volumes:
- .:/usr/app/
- /usr/app/node_modules
ports:
- "3000:3000"
depends_on:
- mongodb
command: npm start
test2directory:
container_name: test2directory
environment:
- DEBUG=1
- NODE_ENV=production
- NODE_NAME=test2directory
- NODE_HOST=localhost
- NODE_PORT=3001
- DB_HOST=mongodb://mongo:27017/test2directory
- DB_PORT=27017
build:
context: ./test2-directory
volumes:
- .:/usr/app/
- /usr/app/node_modules
ports:
- "3001:3001"
depends_on:
- mongodb
command: npm start
mongodb:
image: mongo:3.4.4
container_name: mongo
ports:
- 27017:27017
volumes:
- /data/db:/data/db
Is there any obvious I'm missing as when I run
docker container port test2directory
it returns
3001/tcp -> 0.0.0.0:3001
Found the problem! Setting the HOST to localhost in the container caused the problem and changing it to 0.0.0.0 got it working.

Resources