3

before I start: I have searched this question already and implemented what the "Solutions" were and it did not help (setting host to 0.0.0.0). So, with that out of the way,

Directory structure

|-- osr
|    |-- __init__.py
|-- requirements.txt
|-- Dockerfile
|-- docker-compose.yml

Dockerfile:

FROM python:3.7.5-buster

EXPOSE 5000 # i have tried with and without this

ENV INSTALL_PATH /osr
ENV FLASK_APP osr
ENV FLASK_ENV development
RUN mkdir -p $INSTALL_PATH

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

CMD [ "flask", "run", "--host=0.0.0.0"]

docker-compose.yml:

version: '3.8'
services:
    web:
        build: .
        ports:
            - '5000:5000'
        expose:
            - '5000'
        volumes:
            - .:/osr

__ init __.py

import os
from flask import Flask

def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev'
    )

    @app.route('/hello')
    def hello():
        return 'Hello, World!'

    return app

docker-compose build web docker-compose run web

 * Serving Flask app "osr" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 225-441-434

all of the below return "hmm can't reach this page"

http://localhost
http://localhost:5000/hello
http://127.0.0.1:5000/hello

I've even tried going to the containers IP with

docker exec -it 49e677 bash ip add | grep global inet 172.21.0.2/16 brd 172.21.255.255 scope global eth0

http://127.21.0.2:5000/hello # nothing

Nothing.

I'm sure the app itself works fine as I can simply run the app directly.

$env:FLASK_APP="osr"
$env:FLASK_ENV="development"
flask run --host=0.0.0.0

And it runs fine

localhost

EDIT: UPDATE

I am actually able to get to the container when I run it through simply the Dockerfile... using

docker run -it -p 5000:5000 osr_web # the container built by docker-compose build

With this, I am able to access the endpoint through localhost:5000/hello

So the issue appears to lie in spinning it up through docker-compose run

Does this help at all?

UPDATE 2

I have discovered that when I run docker ps -a I can see that Docker run actually exposes the port, but docker-compose run does not:

enter image description here

2
  • What's your host OS? (Is it more specifically an older Windows where you need Docker Toolbox?) Do you have a firewall setup that might need to be adjusted? Commented May 30, 2020 at 11:01
  • I am using the most up to date Windows 10, does Windows 10 need to allow firewall for outbound in this regard? Commented May 30, 2020 at 19:22

3 Answers 3

2

Are you sure app works fine itself? I tried to run your python __init__.py file and ended up with an error.

python osr/__init__.py 
  File "osr/__init__.py", line 11
    def hello():
    ^
IndentationError: unexpected indent

It works after fixing the indentation error.

@app.route('/hello')
def hello():
    return 'Hello, World!'

$ docker run -d -p 5000:5000 harik8/osr:latest 
76628f86fecb61c0be4a969d3c91c5c575702ad8063b594a6c1c90b498ea25f1

$ curl http://127.0.0.1:5000/hello
Hello, World!

You can't run both docker and docker-compose in port 5000 at the same time. Either run one at a time or change the docker-compose/dockerfile host port.

$ docker ps -a | grep osr
8b885c4a9654        harik8/osr:latest                                     "flask run --host=0.…"   12 seconds ago       Up 11 seconds               0.0.0.0:5000->5000/tcp
$ docker ps -a | grep q5      
70f38bf11e26        q5_web                                                "flask run --host=0.…"   About a minute ago   Up 10 seconds               0.0.0.0:5001->5000/tcp

$ docker ps -a | grep q5
f9f6ba999109        q5_web                                                "flask run --host=0.…"   5 minutes ago       Up 5 minutes                0.0.0.0:5000->5000/tcp                                                                                                   q5_web_1
$ docker ps -a | grep osr
93fb421333e4        harik8/osr:latest                                     "flask run --host=0.…"   18 seconds ago      Up 18 seconds               5000/tcp 
Sign up to request clarification or add additional context in comments.

6 Comments

It was mis-pasted, the tabbing didn't copy over and I had to do it manually, the code is already as you posted, sorry I'll fix it in the question
Also, please note the update in the question. This works with docker run as you have shown, but not docker-compose run for some reason.
There is no error, I simply can't connect to the endpoint. I posted all the relevant information. It gives no error and starts up as expected. Please review the question.
As you can see the first was run with -it . Therefore simply exiting the container stopped it. Changing the port still does nothing and the port is not exposed. Are you using docker-compose in your tests? If not you are not simulating the environment.
@Aserian Flag it opens a tty session and it keeps the container alive. I was able to simulate the scenario and I have updated the answer.
|
1

I found the issue. For reference, I am running these versions:

docker-compose version 1.25.5, build 8a1c60f6

Docker version 19.03.8, build afacb8b

There were a couple issues: First and foremost to get the ports exposed I needed to run one of two options.

docker-compose up web or docker-compose run --service-ports web

Simply running docker-compose run web would not expose the ports.

Once this was finished, I was able to access the endpoint. However I started getting another odd error,

flask.cli.NoAppException
flask.cli.NoAppException: Failed to find Flask application or factory in module  
"osr". Use "FLASK_APP=osr:name to specify one.

I had not experienced this simply using docker run -it -p 5000:5000 osr_web which was odd. However I noticed I had not set the work directory in the Dockerfile.

I changed the Dockerfile to this:

FROM python:3.7.5-buster

EXPOSE 5000
ENV INSTALL_PATH /osr
ENV FLASK_APP osr
ENV FLASK_ENV development
RUN mkdir -p $INSTALL_PATH

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

# added this line
WORKDIR $INSTALL_PATH

COPY . .

CMD [ "flask", "run", "--host=0.0.0.0"]

I believe you could get away without setting the WORK_DIR if you turn the flask application into a package and install it.

Comments

1

I ran into similar issue, using docker compose with a target Dockerfile. I took the example from nextjs https://github.com/vercel/next.js/tree/canary/examples/with-docker-multi-env/docker/development

Everything was running correctly, and after some head banging, I noticed that there's a comment about not exposing ports in Dockerfile in their other example:

# Note: Don't expose ports here, Compose will handle that for us https://github.com/vercel/next.js/blob/0082d54893893b71b4be50b1ace748015781c0dc/examples/with-docker-compose/next-app/dev.Dockerfile#L24

I removed these lines from Dockerfile, rebuilt, and it's working now as expected.:

EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME localhost

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.