I have just started with Docker. I have installed Docker Toolbox for Windows. I was trying out a sample Flask app to understand how things work. But I am stuck!. I am trying to access my app like this http://docker-machine-ip : port number but every time I do, I get '{docker-machine ip} refused to connect.'
I get no exceptions during the building and deploying stages. I also did docker ps to see that container is running. I even tried to access it via Kitematic but still no luck. Below are details related to the app
app.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Flask inside Docker shakel!!"
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0')
requirements.txt
flask
Dockerfile
FROM python:2.7
MAINTAINER Shekhar Gulati "shekhargulati84#gmail.com"
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
The docker commands I used for building and running are:
docker-machine ip default //To get docker machine ip
docker build -t flask-app .
docker run -d -p 6000:6000 flask-app
I have Windows 7 64 bit. Please let me know if any more info is required.
P.S. However I noticed that if I map my container to 5000 port it will run fine but on any other port I get connection refused
I don't know what am I missing here. This is my first attempt at Docker and I have googled for 4 hrs to find a solution but nothing is working. So it might be a very dumb mistake I am doing somewhere :).anyhow any help is very much appreciated. Thanks in advance.
That's because you didn't set the port for your flask application, which is port 5000 by default.
From flask documentation:
port – the port of the webserver. Defaults to 5000 or the port defined in the SERVER_NAME config variable if present.
Related
First off, I would like to state that I have scoured SO for a solution, yet nothing worked for me...
I am trying to deploy a flask server on App engine, yet I always get a 404 Error with /readiness_check failReason:"null"
This is my app.yaml (yes, I did increase the app_start_timeout_sec)
# yaml config for custom environment that uses docker
runtime: custom
env: flex
service: test-appengine
# change readiness check ;
# rediness failure leads to 502 Error
readiness_check:
path: "/readiness_check"
check_interval_sec: 5
timeout_sec: 4
failure_threshold: 2
success_threshold: 2
app_start_timeout_sec: 1800
And this is my Dockerfile:
# Use the official Python image.
# https://hub.docker.com/_/python
FROM python:3.8-buster
# Install Python dependencies.
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .
# expose port 8080 for app engine
EXPOSE 8080
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
# CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
CMD ["gunicorn", "main:app", "-b", ":8080", "--timeout", "300"]
Finally, my main.py contains a very basic route, for the sake of the argument :
from flask import Flask
app = Flask(__name__)
#app.route("/")
def return_hello():
return "Hello!"
Could you please let me know what I'm doing wrong? Have been battling this issue for days now ... Thank you !
I believe you still need to define the handler for your readiness_check (you're getting 404 which means route not found).
See this article for an example
I'm trying to build a Docker image for my python app (a small api on aiohttp with a couple endpoints)
FROM python:3
WORKDIR /home/emil/Projects/elastic_simple_engine
COPY . .
RUN pip3 install -r requirements.txt
EXPOSE 5000/tcp
CMD ["python3", "entry.py"]
The last line of the Dockerfile runs a python script which starts aiohttp.web.Application():
# entry.py
# ...a few dozens of code lines above...
if __name__ == '__main__':
print('Initializing...')
aiohttp.web.run_app(app, host='127.0.0.1', port=5000)
After building an image I'm trying to run the container:
$ docker run -p 5000:5000 myapp
Docker runs the container silently without any output in shell but I can't reach my app's host: 127.0.0.1:5000 (everything works perfectly when I launch it without docker).
Only when I stop the container it prints in console the lines that should be shown during app's launch and shuts down:
Initializing...
======== Running on http://127.0.0.1:5000 ========
(Press CTRL+C to quit)
Please, help me figure out that I do wrong.
TLDR
Set host to 0.0.0.0
127.0.0.1 is the IP address to local interface. It can only communicate within the same host.
0.0.0.0 means a server (in this context) would listen to every available network interface (including 127.0.0.1).
Here, since you are not sharing the docker network, 127.0.0.1 is only available inside the container and not from outside the container. You should use 0.0.0.0 to access it from outside the container or pass --network="host" in docker run but this can have other complications with port sharing.
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.
I'm getting a connection refused after building my Docker image and running docker run -t imageName
Inside the container my python script is making web requests (external API call) and then communicating over localhost:5000 to a logstash socket.
My dockerfile is really simple:
FROM ubuntu:14.04
RUN apt-get update -y
RUN apt-get install -y nginx git python-setuptools python-dev
RUN easy_install pip
#Install app dependencies
RUN pip install requests configparser
EXPOSE 80
EXPOSE 5000
#Add project directory
ADD . /usr/local/scripts/
#Set default working directory
WORKDIR /usr/local/scripts
CMD ["python", "logs.py"]
However, I get a [ERROR] Connection refused message when I try to run this. It's not immediately obvious to me what I'm doing wrong here - I believe I'm opening 80 and 5000 to the outside world? Is this incorrect? Thanks.
Regarding EXPOSE:
Each container you run has its own network interface. Doing EXPOSE 5000 tell docker to link a port 5000 from container-network-interface to a random port in your host machine (see it with docker ps), as long as you tell docker to do it when you docker run with -P.
Regarding logstash.
If your logstash is installed in your host, or in another container, it means that logstash is not in the "localhost" of the container (remember that each container has its own network interface, each one has its own localhost). So you need to point to logstash properly.
How?
Method 1:
Don't give container its own iterface, so it has the same localhost as your machine:
docker run --net=host ...
Method 2:
If you are using docker-compose, use the docker network linking. i.e:
services:
py_app:
...
links:
- logstash
logstash:
image: .../logstash..
So point as this: logstash:5000 (docker will resolve that name to the internal IP corresponding to logstash)
Method 3:
If logstash listen in your localhost:5000 (from your host), you can point to it as this: 172.17.0.1:5000 from inside your container (the 172.17.0.1 is the host fixed IP, but this option is less elegant, arguably)
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')