Docker-Compose publishing ports on Host computer Nodejs + Express - node.js

I have built a docker-compose file and want to access my nodejs app on localhost:3000 from my host computer but publishing the ports doesn't seem to be working.
When I run compose-up everything seems to be working fine and I get the confirmation Listening on port 3000. However when I go to localhost:3000 from a browser as well as curl I get a not found or timeout response.
Am I missing something here?
My NodeJS server:
var server = app.listen( process.env.PORT || 3000, function(){
console.log('Listening on port ' + server.address().port);
});
My Docker-Compose.yml file:
version: "3"
services:
api:
image: baum-test:v0
ports:
- "3000:3000"
networks:
- webnet
mongodb:
image: mongo:latest
ports:
- "27017:27017"
volumes:
- ./data:/data
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
networks:
webnet:

If you are running this using docker stack deploy command then you can "constraints: [node.role == manager]" in your docker-compose.yml. If you used docker-compose up command to get the composition up then you are using Swarm features of the compose file but on docker-compose. Remove the below section completely
deploy:
placement:
constraints: [node.role == manager]

Since I am running docker tools for windows (the old version without hyper-v) I had to manually declare the Docker VM Image IP address as it wasn't binding to localhost.

Related

Docker port issue

I am developing an application which runs on docker containers. I have two node js applications where one is running on port number 5000 and another on 8888 in the docker. I would like to send http request to the node app's route which runs on port 8888 from node app 5000. but it is not working. but when I tried to access the same api end point of port 8888 application it is working fine on browser as well as a none dockerize node js app. can anyone help me to resolve the issue? below is my docker-compose.yml file
version: "3.8"
services:
node-sdc-service:
build:
context: .
dockerfile: Dockerfile-dev
environment:
CHOKIDAR_USEPOLLING: 'true'
container_name: node-sdc
tty: true
#restart: always
ports:
- "0.0.0.0:3000:3000"
- "0.0.0.0:4000:4000"
- "0.0.0.0:5000:5000"
- "0.0.0.0:8000:80"
volumes:
- .:/usr/src/app
yolov5-service:
build:
context: .
dockerfile: Dockerfile-yolo
environment:
CHOKIDAR_USEPOLLING: 'true'
container_name: yolo
tty: true
#restart: always
ports:
- "0.0.0.0:8888:5000"
volumes:
- .:/usr/src/app/server
- ./training_data:/usr/src/coco
- ./yolo_runs:/usr/src/app/runs
mongo-sdc-service:
# image: mongo:4.2-bionic
image: mongo:5.0-focal
# restart: always
container_name: mongo-sdc
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 1004
MONGO_INITDB_DATABASE: sdc
volumes:
- mongo-sdc-storage:/data/db
ports:
- 27020:27017
volumes:
mongo-sdc-storage:
You should expose both port in Dockerfile
EXPOSE 5000
EXPOSE 8888
In docker-compose file use this config
ports:
- target: 5000
published: 5000
protocol: tcp
mode: host
- target: 8888
published: 8888
protocol: tcp
mode: host
Each container is added to the same docker network from your compose. Now since it resides on the same network the PORT that you need to query is 5000 from within your node-sdc-service and not 8888. more information can be found here
https://docs.docker.com/compose/networking/#links
The issue is sorted by calling the 8888 port with local wifi ip

Azure web app multi container (MEAN app), what is the URL to connect to node backend container from front end container?

Trying to learn to deploy angular app to azure web app using multi-container, the frontend loads fine but cant connect to the backend node container, I want to add the url of the node backend to my angular frontend but i cant figure out what it is. I've tried https://rojesh.azure.io:3000, https://rojesh.azurewebsites.net:3000, http://server:3000 and more but nothing seems to work. Website Hostname: https://rojesh.azurewebsites.net and the acr name is rojesh.azurecr.io which has 3 images. This is my config file for compose in azure:
version: '3.3'
services:
db:
image: rojesh.azurecr.io/db:latest
ports:
- "27017:27017"
restart: always
networks:
- app-network
server:
image: rojesh.azurecr.io/server:latest
depends_on:
- db
ports:
- "3000:3000"
restart: always
networks:
- app-network
app:
depends_on:
- server
image: rojesh.azurecr.io/app:latest
environment:
NGINX_HOST: rojesh.azurewebsites.net
NGINX_PORT: 80
ports:
- "80:80"
restart: always
networks:
- app-network
networks:
app-network:
driver: bridge
The app works fine locally using docker compose which is:
version: '3.9'
services:
docker-app:
build:
context: app
dockerfile: Dockerfile.dev
ports:
- '4200:4200'
volumes:
- ./app/src:/app/src
docker-server:
build:
context: server
dockerfile: Dockerfile
environment:
PORT: 3000
MONGODB_URI: mongodb://mongo:27017/rojesh
JWT_SECRET: secret
ports:
- 3000:3000
depends_on:
- mongo
volumes:
- ./server:/server
mongo:
container_name: mongo-server
image: mongo:latest
ports:
- 27017:27017
Thanks # ajkuma-msft. Azure App Service only exposes ports 80 and 443. Yes, incoming requests from client would just be over 443/80 which should be mapped to the exposed port of the container.
App Service will attempt to detect which port to bind to your container. If you want to bind to your container the WEBSITES_PORT app setting and configure it with a value of the port.
Web App for Containers currently allows you to expose only one port to the outside world. The container can only listen for HTTP requests on a single port.
From Docker compose configuration stand-point : Ports other than 80 and 8080 are ignored.
Refer Docker Compose options lists shows supported and unsupported Docker Compose configuration options.
Refer here

Can't access MongoDB container from NodeJS App

I'm running an instance of a web application in my Docker container and am also running a MongoDB container so when I launch the web app I can easily connect to the DB on the app's connection page.
The issue is that I'm not sure how to reach the Mongo container from my web app and am not sure if my host/port connection info is correct.
My Docker Setup
As you can see the container is up and running with both mongo and web app services running without errors
I build the two through docker-compose.yml
version: "3.3"
services:
web:
image: grafana-asw-v3
container_name: grafana-asw-v3
restart: always
build: .
ports:
- "13000:3000"
volumes:
- grafana-storage:/var/lib/grafana
stdin_open: true
tty: true
db:
container_name: mongo
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- grafana-mongo-db:/var/lib/mongo
ports:
- "27018:27017"
volumes:
grafana-mongo-db: {}
grafana-storage: {}
Issue
With everything up and running I'm attempting to connect through the web app, but I seem to be using the wrong connection info...
I assumed to use "hostMachine:port" (roxane:27018), but it's not connecting. Is there something I overlooked here?
There were two changes I had to make to fix this issue:
Modify the bind_ip in mongod.conf via making this change to my docker-compose file
db:
container_name: mongo
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- grafana-mongo-db:/var/lib/mongo
ports:
- "27018:27017"
command: mongod --bind_ip 0.0.0.0
I needed to refer to the IP address instead of the hostname in the cli in my we application. (Thanks to this answer for help with this one)
Short answer
db service is in the same network than web service not in host network.
As you named your services via container_name you shoud be able to use the connection string mongodb://mongo:27017
Explanation
By default, docker containers run under a bridge network allowing them to communicate without viewing your host network.
When using ports in a compose file, you define that you want to map an internal port of the container to the host port
"27018:27017" => I want to expose the container port number 27017 to the host port number 27018.
As a result, you could expose your web frontend without exposing your mongo service :
version: "3.3"
services:
web:
image: grafana-asw-v3
container_name: grafana-asw-v3
restart: always
build: .
ports:
- "13000:3000"
volumes:
- grafana-storage:/var/lib/grafana
stdin_open: true
tty: true
db:
container_name: mongo
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- grafana-mongo-db:/var/lib/mongo
volumes:
grafana-mongo-db: {}
grafana-storage: {}

I can not access by public IP to my Docker Node.js container

I have a Docker Node.js container listening port 4757 with NET library, using my Public IP.
server listening on {"address":"144.xxx.xxx.66","family":"IPv4","port":4757}
When I access from my computer I do not have problem, but If I try to access to my mobile or other device the connection is refused (ERR_ADDRESS_UNREACHABLE).
I have Linux firewall disabled.
If I do a ping to my Public IP it working correctly.
I have the port 4757 open in my router ZTE (Yoigo) with LAN Host 192.168.1.128 TCP and UPD
The application worked correctly by docker-composer in other laptop, the problem is in my new laptop. It is my local.yml
version: '3'
volumes:
local_postgres_data: {}
local_postgres_data_backups: {}
services:
django: &django
build:
context: .
dockerfile: ./compose/local/django/Dockerfile
image: hegeo_local_django
depends_on:
- postgres
volumes:
.:/app
env_file:
- ./.envs/.local/.django
- ./.envs/.local/.postgres
ports:
- "8000:8000"
command: /start
networks:
default:
ipv4_address: 144.xxx.xxx.3
node:
build:
context: .
dockerfile: ./compose/production/nodejs/Dockerfile
image: hegeo_node
depends_on:
- postgres
links:
- postgres
ports:
- "4757:4757"
networks:
default:
ipv4_address: 144.xxx.xxx.66
postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
image: hegeo_production_postgres
volumes:
- local_postgres_data:/var/lib/postgresql/data
- local_postgres_data_backups:/backups
env_file:
- ./.envs/.local/.postgres
networks:
default:
ipv4_address: 144.xxx.xxx.4
networks:
default:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "false"
ipam:
driver: default
config:
- subnet: 144.xxx.xxx.0/24
I think that I have to use "docker run" but not working neither.
Docker - Make docker containers use my public ip
EDIT:
The point is, I used a Network "Driver" because I receive traces from a geolocation device and I need parser this trace with Node.js and send it to Django. If I not set a Network and assign a ipv4_address to Django the connection between Django and Node.js not working.
You mentioned that you have the port open and forwarded from your router to your laptop.
your docker yaml doesn't need to know about the public IP, so you should remove the network settings and go for some thing like:
Internet > (public IP) Router (NAT to Local Docker host machine IP) > Docker host laptop (with firewall off, or port open) > docker engine (port is mapped from host to container) > your Node.js script listening to on the port.

Connect nodejs App in one docker container to mongodb in another on Docker Swarm

I have setup a docker stack (see docker-compose.yml) below.
It has 2 Services:
- A nodejs (loopback) service running in one (or more) containers.
- A mongodb running in another container.
What HOST do I use in my node application to allow it to connect to the mongodb in another container (currently on same node, but could be on different node if I setup a swarm).
I have tried bridge, webnet, hosts IP etc.. but no luck.
Note: I am passing the host into the nodejs app with environment variable "MONGODB_SERVICE_SERVICE_HOST".
Thanks in advance!!
version: "3"
services:
web:
image: myaccount/loopback-app:latest
deploy:
replicas: 2
restart_policy:
condition: on-failure
ports:
- "8060:3000"
environment:
MONGODB_SERVICE_SERVICE_HOST: "webnet"
depends_on:
- mongo
networks:
- webnet
mongo-database:
image: mongo
ports:
- "27017:27017"
volumes:
- "/Users/jason/workspace/mongodb/db:/data/db"
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
networks:
webnet:
webnet is not the host. This is mongo-database.
So change webnet to mongo-database.
ENV MONGO_URL "mongodb://containerName:27017/dbName"
To check mongo-database communication, enter into the nodejs container, and try to ping mongo-database :
ping mongo-database
If it works, you know that your server can communicate with your mongo instance.

Resources