Accessing Python WebApp in Docker Connection Refused - docker

I have a web app in python3. I'am trying to access the web app from another Machine on my network (cloud environment). The web app works.
The Dockerfile is as follows:
FROM python:3.8.0-slim as builder
RUN apt-get update \
&& apt-get install gcc -y \
&& apt-get clean
COPY app/requirements.txt /app/requirements.txt
WORKDIR app
RUN pip install --upgrade pip && pip3 install --user -r requirements.txt
COPY ./app /app
# Production image
FROM python:3.8.0-slim as app
COPY --from=builder /root/.local /root/.local
COPY --from=builder /app /app
WORKDIR app
ENV PATH=/root/.local/bin:$PATH
EXPOSE 5000
ENTRYPOINT gunicorn main:app -b 0.0.0.0:5000
$docker build -t simu .
$docker run --name simu --detach -p 5000:5000 -it simu:v1
11a8e9e2f7f2d70d39cac2b3e2a14c25ae2bf9536db005dee1d473aa588139ae
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11a8e9e2f7f2 simu:v1 "/bin/sh -c 'gunicor…" 3 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp simu
$curl -X POST {} http://localhost:5000
curl: (3) <url> malformed
curl: (56) Recv failure: Connection reset by peer
Why I receive that error?
UPDATE
If I run the container as
/usr/bin/docker run --name simu --detach --network host -e REGISTRAR_URL=http://localhost:8500 -it simu:v1
The service is accessible from the same machine:
curl -X POST {} http://localhost:5000
curl: (3) <url> malformed
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
But it is not from another machine on the same LAN:
$curl -X POST http://algo:5000/generate-prod
curl: (7) Failed connect to algo:5000; Connection refused
I have stopped firewalld also.
If I run the application without the container everything works, so I think there must be something in the neworking with docker which is not set correctly.

You expose the port but you do not publish the port. Check this post on the difference. You need to add -p 5000:5000 to the docker run command.

Related

port mapping -p 8080:8080 vs --net=host

Dockerfile
FROM ubuntu:20.04
# Setup
RUN apt-get update && apt-get install -y unzip xz-utils git openssh-client curl python3 && apt-get upgrade -y && rm -rf /var/cache/apt
# Install Flutter
RUN git clone https://github.com/flutter/flutter.git /usr/local/flutter
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"
RUN flutter channel master
RUN flutter upgrade
RUN flutter config --enable-web
RUN flutter doctor -v
# Copy files to container and get dependencies
COPY . /usr/local/bin/app
WORKDIR /usr/local/bin/app
RUN flutter pub get
RUN flutter build web
# Document the exposed port and start server
EXPOSE 8080
RUN chmod +x /usr/local/bin/app/server/server.sh
ENTRYPOINT [ "/usr/local/bin/app/server/server.sh" ]
Entrypoint server.sh file
#!/bin/bash
cd build/web/
python3 -m http.server 8080
I build an image - docker build --network=host --tag image1 .
Then I try to run it:
docker run -d -p 8080:8080 image1 -- doesnt work. no error but just doesnt load
docker run -d image1 -- doesnt work. no error but just doesnt load
docker run -d --net=host image1 -- works !!
Why does -p 8080:8080 not work whereas --net=host work ?
How are you trying to access your app? At port 8000 or 8080? Your title and the command you posted doesn't seem to match. Are you trying to map 8080 on your machine to 8080 in the app? If so, you have a typo in your command. Your command is mapping 8000 to 8080 and I'm guessing you're then trying to access it at localhost:8080 and encountering nothing.
I think it should just be docker run -d -p 8080:8080 image and then you should be able access it at localhost:8080 just fine.

Im having problem building docker for golang api

I'm new to docker.
I'm trying to implement RESTfull api in go using echo server. My code works fine when I run main.go but i cant run it using docker.
this is my echo server:
r := router.Router()
r.Logger.Fatal(r.Start("localhost:8080"))
and this is my Dockerfile:
FROM golang:latest AS build
ENV GO111MODULE=on \
CGO_ENABLED=1
#Maintainer info
LABEL maintainer="Saman Hoseini"
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o main .
#this step is for CGO libraries
RUN ldd main | tr -s '[:blank:]' '\n' | grep '^/' | \
xargs -I % sh -c 'mkdir -p $(dirname ./%); cp % ./%;'
RUN mkdir -p lib64 && cp /lib64/ld-linux-x86-64.so.2 lib64/
#Second stage of build
FROM alpine:latest
RUN apk update && apk --no-cache add ca-certificates \
sqlite
COPY --from=build /app ./
EXPOSE 8080
ENTRYPOINT ["./main"]
After build when i run the container i face curl failure:
$ docker run -d -p 8080:8080 my-docker
$ curl http://localhost:8080
curl: (56) Recv failure: Connection reset by peer
how can i fix this problem ?
You need to run your application on the external port of your container.
r := router.Router()
r.Logger.Fatal(r.Start(":8080"))
This happens because the EXPOSE 8080 command forwards the application port open on the external port of the container (not internal, like localhost).
After this, command docker run -d -p 8080:8080 my-docker, more precisely a parameter -p, forwards external port from your container to external port on your machine.

cannot connect node container to redis container

I'm trying to dockerize an existing app using Angular for the frontend, node.js as a backend and Postgres as DB. I've created a network and tried to run the containers one by one without the DB for now but I get an error with the node.js backend.
I've built the backend image with this Dockerfile:
FROM node:10.17.0-alpine
WORKDIR /app
COPY package*.json ./
RUN npm i
COPY . .
EXPOSE 3000
CMD node --experimental-worker backend.js
and the frontend one with this:
FROM node:10.17
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json /app/package.json
RUN npm install
RUN npm install -g #angular/cli#7.3.9
COPY . /app
EXPOSE 4200
CMD ng serve --host 0.0.0.0
I've built the images and I've started the containers with:
docker container run --network my-network -it -p 4200:4200 frontend
docker container run --network my-network -it -p 3000:3000 backend
docker container run --network my-network -it --name my-redis -p 6379:6379 redis
but the backend relies on redis to start so it fails with the error:
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
I've tried:
docker container run --network my-network --link my-redis:redis -it -p 3000:3000
but it doesn't help at all. I'm sorry but I am very new to Docker so any clarification would be useful.
Your backend service is trying to connect to 127.0.0.1:6379, however it should be like my-redis:6379.
Basically, you need to inject the redis host to your backend service. There is multiple ways to do so, but the most common way is to read it from ENV variable (e.g. REDIS_HOST=my-redis)

Not being able to access webapp from host in Docker

I have a simple webproject which I want to "Dockerize" but I keep failing at exposing the webapp to host.
My Dockerfile looks like:
FROM debian:jessie
RUN apt-get update -y && \
apt-get install -y python-pip python-dev curl && \
pip install --upgrade pip setuptools
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY . /app
WORKDIR /app/web
And requirements.txt looks like:
PasteScript==2.0.2
Pylons==1.0.2
The web directory was built using:
paster create --template=pylons web
And finally start_server.sh:
#!/bin/bash
paster serve --daemon development.ini start
Now I am able to build with :
docker build -t webapp .
And then run command:
docker run -it -p 5000:5000 --name app webapp:latest /bin/bash
And then inside the docker container I run:
bash start_server.sh
which successfully starts the webapp on port 5000 and if I curl inside docker container I get expected response. Also the container is up and running with the correct port mappings:
bc6511d584ae webapp:latest "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp app
Now if I run docker port app it looks fine:
5000/tcp -> 0.0.0.0:5000
However I cannot get any response from server from host with :
curl localhost:5000
I have probably misunderstood something here but it seems fine to me.
In your dockerfile you need to add EXPOSE 5000 your port mapping is correct think of it as opening the port on your container and then you map it with localhost with the -p
Answer in the comment
when you make_server bind to 0.0.0.0 instead of localhost

Docker-machine Port Forwarding on Windows not working

I'm attempting to access my django app running within Docker on my windows machine. I'm using docker-machine. I've been taking a crack at this for hours now.
Here's my Dockerfile for my django app:
FROM python:3.4-slim
RUN apt-get update && apt-get install -y \
gcc \
gettext \
vim \
curl \
postgresql-client libpq-dev \
--no-install-recommends && rm -rf /var/lib/apt/lists/*
EXPOSE 8000
WORKDIR /home/
# add app files from git repo
ADD . server/
WORKDIR /home/server
RUN pip install -r requirements.txt
CMD ["python", "manage.py", "runserver", "8000"]
So that should be exposing (at least in the container) port 8000.
When I use the command docker-machine ip default I am given the IP 192.168.99.101. I go to that IP on port 8000 but get no response.
I went into the VirtualBox to see if forwarding those ports would work. Here is the configuration:
I also tried using 127.0.0.1 as the Host IP. I also tried disabling the windows firewall.
Here's my command for starting the container:
docker run --rm -it -p 8000:8000 <imagename>
I am at a loss on why I am unable to connect on that port. When I run docker-machine ls the url it gives me is tcp://192.168.99.101:2376 and when I go to that it gives me some kind of file back, so I know the docker-machine is active on that port.
Also when I run docker ps I get this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c00cc28a2bd <image name> "python manage.py run" 7 minutes ago Up 7 minutes 0.0.0.0:8000->8000/tcp drunk_knuth
Any help would be greatly appreciated.
The issue was that the server was running on 127.0.0.1 when it should have been running on 0.0.0.0.
I changed the CMD line in the Dockerfile from
CMD ["python", "manage.py", "runserver", "8000"]
to
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
and it now works.

Resources