Can not connect to docker container(python server) with Dockerfile - python

I am studying the docker.
I using docker and using Dockerfile to run a python server.
This my python name app.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return "hello docker"
if __name__ == '__main__':
app.run()
app.run(host="0.0.0.0", port=5000)
I am running in the VirtualBox and use centos7.
If I don't add
host="0.0.0.0", port=5000
I can not connect to http://192.168.1.6:5000/ (This is my virtual IP)
but now when I using Dockerfile and run this command, I can't connect to the server.
This is my Dockerfile :
FROM python:2.7
LABEL maintainer ="me <me#gmail.com>"
RUN pip install flask
COPY app.py /app/
WORKDIR /app
EXPOSE 5000
CMD ["python","app.py"]
And I inspect the container
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
It is succeed. But I cant connect to http://192.168.1.6:5000/
I also set the firewall to open port 5000 and reload.
Why can I connect to my VM?

EXPOSE doesn't actually publish the port. You should run with the -p option in order to publish and map the port:
docker run --detach -p 5000:5000 <image>

Related

Refused to connect local ip address by using docker while running ok without docker [duplicate]

This question already has answers here:
Deploying a minimal flask app in docker - server connection issues
(8 answers)
Closed 7 months ago.
[Editted]
I am trying to dockerize a simple flask project. I kept getting this error when I run the project using docker. On the other hand the project works just fine when I run it normally using python3 test-flask.py
Docker File
FROM python:3.9.6-slim
# https://www.lifewithpython.com/2021/05/python-docker-env-vars.html
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONUTF8=1 \
PIP_DISABLE_PIP_VERSION_CHECK=on
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
curl \
wget \
unzip \
jq \
ffmpeg \
libsndfile1-dev \
&& rm -rf /var/lib/apt/lists/*
RUN wget "https://download.pytorch.org/whl/cpu/torch-1.10.2%2Bcpu-cp39-cp39-linux_x86_64.whl" \
&& pip install torch-1.10.2+cpu-cp39-cp39-linux_x86_64.whl
RUN mkdir app
WORKDIR /app
COPY README.md .
COPY requirements.txt .
COPY setup.py .
COPY src ./src
RUN pip install .
# COPY main.py .
COPY test_flask.py .
CMD [ "python", "test_flask.py", "run", "--host", "0.0.0.0"]
docker-compose.yml
version: '3.9'
services:
asr-cpu-local:
build:
context: ./
dockerfile: ./Dockerfile
volumes:
- ./data:/data
- ./models:/models
ports:
- 127.0.0.1:8000:8000
In the Terminal and Docker Container log, after I run it with docker, it shows no error at all
* Serving Flask app 'test_flask' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:8000 (Press CTRL+C to quit)
* Restarting with stat
For additional information, I have run docker port to check, but the error still occured:
docker port <container-name>
8000/tcp -> 127.0.0.1:8000
For the File test_flask.py:
from flask import Flask
app = Flask(__name__)
import librosa
from src.jrasr.predictors import Wav2vec2bert2bertPredictor
MODEL_CONFIG = {
"name": "Wav2vec2bert2bertPredictor",
"version": "0.0.1",
"models": [
{
"name": "wav2vec2",
"dir": "/models/wav2vec2-base-csj-kana-v0.0.1"
},
{
"name": "bert2bert",
"dir": "/models/bert2bert-csj-kana-v0.0.1"
}
]
}
#app.before_first_request
def load_model_to_app():
# Load the model
app.predictor = Wav2vec2bert2bertPredictor(MODEL_CONFIG)
#app.route('/')
def index():
return "This is Home Page"
#app.route('/data/<audio_path>')
def predict(audio_path):
model = app.predictor
audio_path = "/data/"+audio_path
data, _ = librosa.load(audio_path, sr=16000, mono=True, duration=10)
words = model.predict(data)
result = "".join(words)
return result
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8000, debug=True)
But when I try to access the Ip address as shown in the Terminal and Docker Container Log, I got that 127.0.0.1 refuse to connect such as in the image shown below. The problem is in the terminal and docker log, there isn't any Error message, but still I can't access it, can anyone tell me, how to solve my problems?
Error at the local browser
127.0.0.1 is the localhost interface and when running inside the container only the container has access to it. To be accessible outside the container it should be the special IP 0.0.0.0 to bind all interfaces.
Solution 1
Change the host to be 0.0.0.0 instead of 127.0.0.1 in the test_flask.py. Something like the following snippet.
WARNING: As 0.0.0.0 binds all interfaces to the outside world it could be a security risk if running it locally instead of in a container. That said, the Solution 2 is the recommended one (With the 127.0.0.1 in the Python source as it is). So, when running it directly locally it binds to the localhost and when running it in the container it binds the outside world of the container itself.
...
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)
Solution 2
On the other hand, to run it passing the host as an argument the Dockerfile should be changed to the following:
...
ENV FLASK_APP=test_flask
CMD ["flask", "run", "--host=0.0.0.0"]
Test it with the browser connecting to http://localhost:8000/ or http://127.0.0.1:8000/

Flask App not working when running on docker container

Im new to docker and Flask and I'm getting an issue when I try to run the app. The browser says the site (172.17.0.2:5000) can't be reached.
For anyone wondering, the Dockerfiles:
FROM ubuntu
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip3 install flask
RUN mkdir -p /opt/MyApp-test
COPY . /opt/MyApp-test
WORKDIR /opt/MyApp-test
EXPOSE 5000
ENTRYPOINT python3 main.py
CMD flask run --host 0.0.0.0
The main.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'IT WORKED! I AM RUNNING FROM A DOCKER CONTAINER!!!'
if __name__ == '__main__':
app.run()
And when I run the container, I get:
(base) daniellombardi#Daniels-MacBook-Pro MyApp-test % docker run 2625
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
I try to go to http://127.0.0.1:5000/, unable to connect, then I inspect the container to get its IP address, and it says 172.0.1 instead of 127.0.0.1:5000, but also unable to connect. And the app works when I run it on my computer.
You should just bind the exposed port inside of your container to a port in docker host when calling docker run, thus docker run should be called this way:
docker run 2625 -p 5000:5000

How do I run my Cloud Run service locally?

I have the following simple Cloud Run service from the Python quickstart:
app.py:
import os
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!\n'
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
Dockerfile:
FROM python:3.7
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .
RUN pip install Flask
CMD python app.py
How can I run & test this locally?
Similar to any other Dockerfile, you can use this two step command to build your image, and then run it locally:
$ docker build -t your_service .
$ docker run --rm -p 8080:8080 -e PORT=8080 your_service
It's important to specify the PORT environment variable here, and ensure that your app uses it appropriately.
Afterwards, your service will be running on http://localhost:8080

Can't manage to build and run a bottle.py app

I've been trying to setup a container to run an app with the bottle framework. Read everything I could find about it, but even so I can't do it. Here's what I did:
Dockerfile:
# Use an official Python runtime as a parent image
FROM python:2.7
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 8080
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
app.py:
import os
from bottle import route, run, template
#route('/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost', port=8080)
requirements.txt
bottle
By running the command docker build -t testapp I create the container.
Then by running the command docker run -p 8080:8080 testapp I get this terminal output:
Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
But when I go to localhost:8080/testing I get localhost refused connection.
Can anyone point me to the right direction?
Problem is this line:
run(host='localhost', port=8080)
It is exposing it for "localhost" insde the container you are running the code. You can use python library netifaces to get container external interface if you want to but I suggest you to set 0.0.0.0 as host like:
run(host='0.0.0.0', port=8080)
Then you will be able to access http://localhost:8080/ (asuming your docker engine is at localhost)
EDIT: mind your previous container might still be listening on 8080/tcp. Remove or stop previous container first.

Can't acces swagger server into docker container

I have a swagger server api in python that I can run on my pc and easily access to the user interface via web. I'm now trying to run this API into a docker container and place it into a remote server. After the doing the 'docker run' command int the remote server all seems to be working fine but when I try to connect I got a ERR_CONNECTION_REFUSED response. The funny thing is that if I enter into the container the swagger server is working and answer my requests.
Here is my Dockerfile:
FROM python:3
MAINTAINER Me
ADD . /myprojectdir
WORKDIR /myprojectdir
RUN pip install -r requirements.txt
RUN ["/bin/bash", "-c", "chmod 777 {start.sh,stop.sh,restart.sh,test.sh}"]
Here are my commands to build/run:
sudo docker build -t mycontainer .
sudo docker run -d -p 33788:80 mycontainer ./start.sh
Here is the start.sh script:
#!/bin/bash
echo $'\r' >> log/server_log_`date +%Y%m`.dat
python3 -m swagger_server >> log/server_log_`date +%Y%m`.dat 2>&1
And the main.py of the swagger server:
#!/usr/bin/env python3
import connexion
from .encoder import JSONEncoder
if __name__ == '__main__':
app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = JSONEncoder
app.add_api('swagger.yaml', arguments={'title': 'A title'})
app.run(port=80, threaded=True, debug=False)
Does anyone know why I can't acces to 'myremoteserver:33788/myservice/ui' and what to change for solving it.
Thanks in advance
I finally managed to find out the solution. It's needed to tell the flask server of connexion to run on 0.0.0.0 so that not only local connections are allowed and to change in the swagger.yaml the url with the name of the server where the docker container is located
app.run(port=80, threaded=True, debug=False, host='0.0.0.0')

Categories