Docker: connect ECONNREFUSED - docker

I have docker running. There are no errors in the log file.
But when I try to execute a simple request via Postman
POST -> http://ip:3200/api/login
I get an error:
Error: connect ECONNREFUSED ip:3200
What did I do wrong?
docker ps:
CONTAINER_ID 84b2968aa424
IMAGE top-api:latest
COMMAND "docker-entrypoint.s"
PORTS 0.0.0.0:3200->3200/tcp
NAMES top-api
Dockerfile:
FROM node:12-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
RUN npm add sharp
COPY . .
COPY ./dist ./dist
CMD ["npm", "run", "start:dev"]
docker-compose.yml:
version: '3'
services:
top.api:
image: top-api:latest
container_name: top-api
restart: always
ports:
- 3200:3200
volumes:
- ./.env.dev:/app/.env.dev

From your host you have to connect to: localhost:3200. So when using postman, target localhost:3200.

You're probably listening on 127.0.0.1, which is not publicly exposed (see https://pythonspeed.com/articles/docker-connection-refused/ for diagrams).
Try configuring your webserver to listen on 0.0.0.0.

You can try to expose the port 3200 by adding the following line to your Dockerfile:
EXPOSE 3200

Related

Meaning of PORTS column of Docker container ls

I'm getting this value on the PORTS column of a docker container ls entry (a container for a react app served by Nginx):
PORTS
80/tcp, 0.0.0.0:40000->40000/tcp, :::40000->40000/tcp
I know the second part is IPv4 mapping and the third is IPv6. I don't understand the meaning of the 80/tcp, but I think it's what really makes the app accessible from the internet, because if I use mapping "80:80" it works, but now with "40000:40000" it doesn't.
My project has a structure like this, so it can build multiple projects at once with compose:
|
|- client (a React app)
| |- Dockerfile-client
|- .env.prod
|- docker-compose.yml
The docker-compose.yml looks like this:
version: '3.7'
services:
client:
build:
dockerfile: ./client/Dockerfile-client
context: ./ # so the .env.prod file can be used by React
container_name: client
env_file:
- .env.prod # enabled to apply CLIENT_PORT var to Dockerfile-client
ports:
- "${CLIENT_PORT}:${CLIENT_PORT}"
# others
All the variables are defined in .env.prod (CLIENT_PORT is 40000), and I run compose like `docker compose --env-file .env.prod up", and it doesn't bring errors.
Here's the Dockerfile that builds the client container:
# build env
FROM node:13.13-alpine as build
WORKDIR /app
COPY ./client/package*.json ./
RUN npm ci
COPY ./client/ ./
COPY ./.env.prod ./
RUN mv ./.env.prod ./.env
RUN npm run build
# production env
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE ${CLIENT_PORT}
CMD ["nginx", "-g", "daemon off;"]
It would all work fine if I mapped "80:80", but my problem is how is that 80/tcp there in the ls output when there's no "80" to be seen in the files? Might it be because of Nginx?
80/tcp is from nginx which listens by default on this port.
Correct port mapping in this case will be 4000:80
OR
If you want nginx to listen on other port like 4000 update listen parameter in nginx.conf file to that port
http {
server {
listen 4000;
}
}
And then use port mapping as 4000:4000

How to put an existing container in docker-compose?

I already have a vue application containerized and running, but how do I put it in docker compose?
Dockerfile:
FROM node:14
WORKDIR /app
RUN npm install #babel/core #babel/node #babel/preset-env nodemon express axios cors mongodb
COPY . .
EXPOSE 4200
CMD ["npm", "run", "serve"]
So when I try to put port "4200" it says that I have already the same port running, so how do I put that container inside whole app which will store multiple containers?
This is my docker-compose try:
version: '3.8'
services:
posts:
build: ./posts
ports:
- "4200:4200"
So this is the visualisation of something that I want to do:
Change host port in compose
For example:
ports:
- "4201:4200"

unable to access docker container port from localhost when using docker-compose

I have a very basic node/express app with a dockerfile and a docker-compose file. When I run the docker container using
docker run -p 3000:3000 service:0.0.1 npm run dev
I can go to localhost:3000 and see my service. However, when I do:
docker-compose run server npm run dev
I can't see anything on localhost:3000, below are my files:
Dockerfile
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
docker-compose.yml
version: "3.7"
services:
server:
build: .
ports:
- "3000:3000"
image: service:0.0.1
environment:
- LOGLEVEL=debug
depends_on:
- db
db:
container_name: "website_service__db"
image: postgres
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=website_service
also, everything is working fine from the terminal/docker side - no errors and services are running fine, i just cant access the node endpoints
tl;dr
docker-compose run --service-ports server npm run dev
// the part that changed is the new '--service-ports' argument
the issue was a missing docker-compose run argument --service-ports:
from these docs:
The second difference is that the docker-compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service’s ports to be created and mapped to the host, specify the --service-ports flag:

how to access node api which is running as docker container

I have built a docker-compose file for my node js application that has been dockerized, But I don't know how to make the API call to that node js app which is running as a docker container, Please help me with this concern.
My DockerFile:
FROM node:10.15-slim
ENV NODE_ENV=production
WORKDIR /app
COPY package.json package-lock*.json ./
RUN npm install && npm cache clean --force
COPY . .
CMD ["node", "./bin/www"]
My Docker-compose file:
version: '2.4'
services:
express:
build:
context: .
dockerfile: Dockerfile
command: /app/node_modules/.bin/nodemon ./bin/www
ports:
- 3000:3000
volumes:
- .:/app
environment:
- DEBUG=sample-express:*
- NODE_ENV=development
You'll need to expose the port from docker on which your application is running.
Let's say your application is running on port 8080 inside docker, here's how you can expose that specific port:
EXPOSE 8080
Then you'll need to map the port exposed by docker tthato your local port. Here's how you can do it in docker:
docker run -p 49160:8080 -d docker_image
And if you're working with docker-compose, you'll do it like this:
version: '3'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile
image: nodejs
container_name: nodejs
ports:
- "8080:8080"
UPDATE
Let's say you want to send /api requests to back-end server. This is how you'll do it in nginx conf:
server {
listen 80
location /api {
proxy_pass http://backend:8080/;
}
}
I hope it helps.

How do I configure docker compose to expose ports correctly?

I'm using docker and docker compose to run a clojure and a node app, alongside postgres.
The project is contained in the following folder structure.
project/
-- app/
-- -- Dockerfile
-- frontend/
-- -- /Dockerfile
-- docker-compose.yml
The app/Dockerfile looks like so...
FROM clojure:latest
COPY . /usr/src/app
WORKDIR /usr/src/app
EXPOSE 9000
CMD ["lein", "run", "migrate", "&&","lein", "run"]
The frontend/Dockerfile looks like so ...
FROM node:5
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN npm install
EXPOSE 8080
CMD ["npm", "start"]
And lastly the docker-compose.yml looks like...
frontend:
image: bradcypert/node
volumes:
- ./frontend:/usr/src/frontend
ports:
- "8080:8080"
backend:
image: bradcypert/clojure
volumes:
- ./app:/usr/src/backend
ports:
- "9000:9000"
links:
- postgres
postgres:
image: postgres
ports:
- "5432:5432"
backend is failing for a separate reason, but the frontend seems to be running successfully, that being said, I'm unable to hit localhost:8080 and see the app. What do I need to do make this happen?
Thanks in advance.
Just to clarify, the command being run is docker-compose up
With boot2docker (on Mac or Windows), to access any port from localhost, you have to configure your VirtualBox VM in order to port-forward that port from the VM into the host.
Your port mappings are correct, but you still need to make visible to your host (Mac) the one port you want to access from localhost (your Mac).
See for instance "Using boot2docker to run Docker on a Mac or Windows" from Andrew Odewahn:
That way, you don't have to find out what the IP of your machine is.
(Which you can see with docker-machine ls followed by docker-machine ip <name>)

Resources