Running docker container not accessible from host (localhost:8081) - docker

Using Ubuntu.
Based on this guide:
https://www.freecodecamp.org/news/how-to-use-routing-in-vue-js-to-create-a-better-user-experience-98d225bbcdd9/
I have created a minimal vuejs project with below project structure:
https://github.com/dev-samples/samples/tree/master/vuejs-001
frontend-router/
build/
config/
src/
static/
test/
build.sh
Dockerfile.dev
package-lock.json
package.json
Where:
Dockerfile.dev
FROM node:10
RUN apt install curl
RUN mkdir /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /app/package.json
# make the 'app' folder the current working directory before running npm install
WORKDIR /app
RUN npm install
CMD [ "npm", "run", "dev" ]
I am building the image and running the container from that image with:
docker build -t frontend-router-image -f Dockerfile.dev .
docker rm -f frontend-router-container
docker run -it -p 8081:8080 -v ${PWD}:/app/ -v /app/node_modules --name frontend-router-container frontend-router-image
which gives:
DONE Compiled successfully in 1738ms 3:49:45 PM
I Your application is running here: http://localhost:8080
Since I add -p 8081:8080 to docker run command I would expect that I can access the application from my host browser on:
http://localhost:8081/
but it just gives below error:
I works fine when I run it with vanilla npm from my host. But why can't I access the application when its run from inside a docker container?
Source code here:
https://github.com/dev-samples/samples/tree/master/vuejs-001
As suggested below I have tried:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e011fb9e39e8 frontend-router-image "docker-entrypoint.s…" 12 seconds ago Up 9 seconds 0.0.0.0:8081->8080/tcp frontend-router-container
$ docker run -it --rm --net container:frontend-router-container nicolaka/netshoot ss -lnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:8080 0.0.0.0:*
For comparison this project works fine:
https://github.com/dev-samples/samples/tree/master/vuejs-002
Meaning that when I run a container I can access the web application on my host browser on localhost:8081

Based on this:
https://github.com/webpack/webpack-dev-server/issues/547
and:
https://dev.to/azawakh/don-t-forget-to-give-host-0-0-0-0-to-the-startup-option-of-webpack-dev-server-using-docker-1483
https://pythonspeed.com/articles/docker-connection-refused/
It works if I change:
host: 'localhost', // can be overwritten by process.env.HOST
to:
host: '0.0.0.0', // can be overwritten by process.env.HOST
in the file: /frontend-router/config/index.js

When you have connection reset it means usually that nobody is listen on the port .
It seems you are listening on localhost , you must
listening on 0.0.0.0 when you are in the docker .
in your file config/index.js , host is localhost , you must remove the host directive
If you listening on 127.0.0.1or localhost , you are listening on local network , so
inside the container , the web server can be accessed only by local process .
Another source of problems you can have , you are connecting to the wrong port .
if you run with docker run -it -p 8081:8080 you must acces to http://localhost:8081/
see
Publish or expose port (-p, --expose)
from https://docs.docker.com/engine/reference/commandline/run/

Related

I can't get access to an exposed port in docker

I'm using Ubuntu 20.04 and running Python 3.8. Here is my dockerfile:
FROM python:3.8
WORKDIR /usr/src/flog/
COPY requirements/ requirements/
RUN pip install -r requirements/dev.txt
RUN pip install gunicorn
COPY flog/ flog/
COPY migrations/ migrations/
COPY wsgi.py ./
COPY docker_boot.sh ./
RUN chmod +x docker_boot.sh
ENV FLASK_APP wsgi.py
EXPOSE 5000
ENTRYPOINT ["./docker_boot.sh"]
and my docker_boot.sh
#! /bin/sh
flask deploy
flask create-admin
flask forge
exec gunicorn -b 0.0.0.0:5000 --access-logfile - --error-logfile - wsgi:app
I ran docker run flog -d -p 5000:5000 in my terminal. And I couldn't get my app working by typing localhost:5000 but it worked quite well when I typed 172.17.0.2:5000 (the docker machine's ip address). But I want the app to run on localhost:5000.
I'm sure there is nothing wrong with the requirements/dev.txt and the code because it works well when I run flask run directly in my terminal.
Edit on 2021.3.16:
Add docker ps information when docker run flog -d -p 5000:5000 is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff048e904183 flog "./docker_boot.sh -d…" 8 seconds ago Up 6 seconds 5000/tcp inspiring_kalam
It is strange that there's no mapping of the hosts. I'm sure the firewall is off.
Can anyone help me? Thanks.
Use docker run -d -p 0.0.0.0:5000:5000 flog.
The arguments and the flags that are after the image name are passed as arguments to the entrypoint of the container created from that image.
Run docker ps and you need to see something like
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
565a97468fc7 flog "docker_boot.sh" 1 minute ago Up 1 minutes 0.0.0.0:5000->5000/tcp xxxxxxxx_xxxxxxx

Google Compute Engine Container Port Closed

I added a firewall rule to open port 8080. If I click the SSH button in the GCE console, and run on the host shell:
nc -l -p 8080 127.0.0.1
I can detect the opened port. If I then go to the container's shell with:
docker run --rm -i -t <image> /bin/sh
and run the same netcat command, I can't detect the open port.
I went down this troubleshooting route because I couldn't connect to a node:alpine container running the ws npm for a demo websocket server. Here is my dockerfile:
# specify the node base image with your desired version node:<version>
FROM node:alpine
# replace this with your application's default port
EXPOSE 8080
WORKDIR /app
RUN apk --update add git
docker run --rm -i -t -p 8080:8080 <image> /bin/sh
per Google Compute Engine Container Port Closed

PHP cannot resolve hostname

I'm learning docker for my first project using Slim framework (PHP). I created a dockerfile to build an image with all the source code in it. When I run the image with the run command it shows me
<b>Warning</b>: Unknown: php_network_getaddresses: getaddrinfo failed: Name does not resolve in <b>Unknown</b> on line <b>0</b><br />
[Sat Jun 15 09:41:14 2019] Failed to listen on 127.0.0.1:8080 (reason: php_network_getaddresses: getaddrinfo failed: Name does not resolve)
Dockerfile looks like:
FROM php:7-alpine
COPY . /var/www
WORKDIR /var/www
CMD [ "php", "-S 127.0.0.1:8080 -t public" ]
Docker run command is:
sudo docker run -it --rm --network="host" --expose 8080 --name cm2 collection_manager_1
Don't know how I can fix this. Can someone help me?
The CMD syntax seems to have some problem. All the arguments in the command should be comma separated and inside the double quotes.
I made a slight change in Dockerfile and it worked.
FROM php:7-alpine
COPY . /var/www
WORKDIR /var/www
CMD [ "php", "-S", "0.0.0.0:8080", "-t", "html" ]
docker build -t testimage:v1 .
[mchawre#jumphost try]$ docker run -it --rm --network="host" --expose 8080 --name testrun testimage:v1
PHP 7.3.6 Development Server started at Sat Jun 15 11:50:19 2019
Listening on http://0.0.0.0:8080
Document root is /var/www/html
Press Ctrl-C to quit.
NOTE: Change 127.0.0.1 to 0.0.0.0 so that you can hit the php using public/private ip of your machine rather than just localhost.

Revel and Docker container

I am attempting to create a docker container that contains the revel skeleton app. Everything seems to build OK and the container is created but when I go to localhost:9000 in my browser nothing comes up.
To make sure my environment is working properly I created a simple hello world go app and created a docker container for it. It worked OK using the same port 9000. This makes me think that there is something not configured properly in my dockerfile.
Dockerfile:
#Compile stage
FROM golang:1.11.4-alpine3.8 AS build-env
ENV CGO_ENABLED 0
RUN apk add --no-cache git
ADD . /go/src/revelapp
# Install revel framework
RUN go get -u github.com/revel/revel
RUN go get -u github.com/revel/cmd/revel
#build revel app
RUN revel build revelapp app dev
# Final stage
FROM alpine:3.8
EXPOSE 9000
WORKDIR /
COPY --from=build-env /go/app /
ENTRYPOINT /run.sh
Docker command used:
docker build -t revelapp . && docker run -p 9000:9000 --name revelapp revelapp
After command is executed and container is created the console shows:
INFO 17:25:01 app run.go:32: Running revel server
INFO 17:25:01 app plugin.go:9: Go to /#tests to run the tests.
Revel engine is listening on.. localhost:9000
When I go to localhost:9000 I would expect to see the text It Works!
You're listening on localhost:9000, so 127.0.0.1 points to your container and not your local machine.
You have two solutions to make it work:
Listen on 0.0.0.0:9000
Use --network="host" in your docker run command: 127.0.0.1 in your docker container will point to your docker host.

Can't access docker containers from host

I have a simple image for a Rails service with the following Dockerfile:
FROM ruby:2.4.4
MAINTAINER sadzid.suljic#gmail.com
RUN apt-get update && apt-get install -y \
build-essential \
nodejs
RUN mkdir /app
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install
COPY . ./
EXPOSE 3000
CMD ["rails", "s", "-p", "3000"]
I built the image and ran the container with these commands:
docker build -t chat/users .
docker run -P --name users_service chat/users
I have this output on the host:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23716591e656 chat/users "rails s -p 3000" 6 minutes ago Up 5 minutes 0.0.0.0:32774->3000/tcp users_service
$ lsof -n -i :32774 | grep LISTEN
com.docke 32891 ssuljic 18u IPv4 0x41c034d5d4627f5f 0t0 TCP *:filenet-re (LISTEN)
com.docke 32891 ssuljic 19u IPv6 0x41c034d5d3beb9b7 0t0 TCP [::1]:filenet-re (LISTEN)
$ curl localhost:32774
curl: (52) Empty reply from server
When I run curl localhost:3000 inside the container I get the proper response from my API.
Does anyone know why I can't access the container from my host?
I'm using Docker for Mac with this version:
$ docker -v
Docker version 18.03.1-ce, build 9ee9f40
Some versions of Rails bind to localhost by default, which explains why you can access from within the container but not from the host (it’s viewed as a different host).
Adding -b 0.0.0.0 to the CMD instruction should solve the problem.
Try:
docker run -p 3000:3000 --name users_service chat/users
This will map 3000 to 3000
http://localhost:3000
Looking into this more I think this was either and chrome issue or network issue as I was having the same issue:
Here is how I resolved it:
Make sure your /etc/hosts file has 127.0.0.1 localhost (more than likely it's already there)
Cleared Cookies and Cached files
Cleared host cache
Go to:chrome://net-internals/#dns click Clear Host Cache
Restarted chrome
Reset Network Adapter
Note: This was unintentional so not sure if it was part of the fix or not, but wanted to include it in case.
Unfortunately I'm not sure which step fixed the problem

Resources