Not being able to access webapp from host in Docker - 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

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.

Server Error (500) when starting my dockerized laravel app

I have created a laravel app and then created a Dockerfile:
FROM php:7.4-cli
RUN apt-get update -y && apt-get install -y libmcrypt-dev
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo
#mbstring
WORKDIR /app
COPY . /app
RUN composer install
EXPOSE 8000
CMD php artisan serve --host=0.0.0.0 --port=8000
Then I ran sudo docker build -t myApp . and sudo docker run -it -p 8000:8000 news-organizer. Everything worked fine.
When I copy this folder (with Dockerfile) to another location and run composer update --ignore-platform-reqs, sudo docker build --no-cache -t theApp . and sudo docker run -it -p 8888:8888 theApp the Web App starts. When I enter 127.0.0.1:8888 I get the 500 Error.
I already set all rights to sudo chmod 755 -R <myLaravelFolder>. I also tried setting different port numbers. The Dockerfile of the new folder is:
FROM php:7.4-cli
RUN apt-get update -y && apt-get install -y libmcrypt-dev
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo
#mbstring
WORKDIR /app
COPY . /app
RUN composer install
EXPOSE 8888
CMD php artisan serve --host=0.0.0.0 --port=8888
I just can't find a way to fix the 500 Error. What can I do?
My basic idea is: Creating a Laravel Web-App. Then creating a Dockerfile and upload it somewhere. Then others can download the Web App, install Docker, and run sudo docker build -t <myImage> . and sudo docker run -it -p 8000:8000 <myImage>. With that they can use my Laravel App on their computers as docker container.
I run xubuntu 20.04 as OS.
Thank you for hinting at the logs. Eventually, I found that I had to generate the application key. It can be done via php artisan key:generate.

Accessing Python WebApp in Docker Connection Refused

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.

uwsgi-nginx in docker not works

I have Dockerfile like:
FROM python:3.6.5-jessie
MAINTAINER twitter myname
RUN apt-get update
RUN apt-get install -y git
RUN apt-get install -y nginx
RUN pip install --upgrade pip
RUN git clone https://github.com/hongmingu/requirements
RUN pip install -r /requirements/requirements_django.txt
RUN apt-get install -y vim
RUN mkdir -p /uwsgi_log
RUN git clone https://github.com/hongmingu/smaple_django
RUN apt-get install -y nginx
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./uwsgi.ini /uwsgi.ini # it runs in daemonized mode
# These files are just setting files. nginx get request at port 8000 and uwsgi runs django project.
RUN uwsgi --ini /uwsgi.ini
RUN service nginx restart
CMD ["python3"]
I think these 2 lines not work:
RUN uwsgi --ini /uwsgi.ini
RUN service nginx restart
Because When I build it and run it with linux command: sudo docker run --rm -it -p 8080:8000 hongmingu/smaple:0.1 /bin/bash my 127.0.0.1:8080 does not work. But, When I attach container and type command manually like, uwsgi --ini /uwsgi.ini and service nginx restart, It works well.
So, Is it impossible to run uwsgi, nginx in Dockerfile?
I want to do it so that I hope I don't need to run uwsgi and nginx manually.
Where did I make fault? Is there any good way to do this?
This docker image(hongmingu/smaple:0.1) is here: https://cloud.docker.com/u/hongmingu/repository/docker/hongmingu/smaple
You misunderstood the RUN instruction
The RUN instruction will execute any commands in a new layer on top of
the current image and commit the results
It's used to build your image, it is not docker run which executes the command in the container.
The solutions involves to execute those 2 lines in the CMD or ENTRYPOINT with a shell script. uwsgi has also to be daemonized. Checkout this image https://github.com/tiangolo/uwsgi-nginx-docker

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