1

I have a simple python application that I want to run in a Docker Image. The application looks like this

from flask import Flask

app = Flask(__name__)

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

if __name__ == '__main__':
    app.run()

and a dockerfile that looks like this:

FROM python:3
RUN pip install --upgrade pip
RUN pip install flask
CMD ["python","app.py"]
COPY app.py /app.py

In addition to these two files the rest of the folder structure looks like this:

├───.idea
│   └───inspectionProfiles
├───static
├───templates
├───venv
│   ├───Include
│   ├───Lib
│   │   ├───site-packages
│   │   │   └───pip-19.0.3-py3.7.egg
│   │   │       ├───EGG-INFO
│   │   │       └───pip
│   │   │           ├───_internal
│   │   │           │   ├───cli
│   │   │           │   ├───commands
│   │   │           │   ├───models
│   │   │           │   ├───operations
│   │   │           │   ├───req
│   │   │           │   ├───utils
│   │   │           │   └───vcs
│   │   │           └───_vendor
│   │   │               ├───cachecontrol
│   │   │               │   └───caches
│   │   │               ├───certifi
│   │   │               ├───chardet
│   │   │               │   └───cli
│   │   │               ├───colorama
│   │   │               ├───distlib
│   │   │               │   └───_backport
│   │   │               ├───html5lib
│   │   │               │   ├───filters
│   │   │               │   ├───treeadapters
│   │   │               │   ├───treebuilders
│   │   │               │   ├───treewalkers
│   │   │               │   └───_trie
│   │   │               ├───idna
│   │   │               ├───lockfile
│   │   │               ├───msgpack
│   │   │               ├───packaging
│   │   │               ├───pep517
│   │   │               ├───pkg_resources
│   │   │               ├───progress
│   │   │               ├───pytoml
│   │   │               ├───requests
│   │   │               ├───urllib3
│   │   │               │   ├───contrib
│   │   │               │   │   └───_securetransport
│   │   │               │   ├───packages
│   │   │               │   │   ├───backports
│   │   │               │   │   └───ssl_match_hostname
│   │   │               │   └───util
│   │   │               └───webencodings
│   │   └───tcl8.6
│   └───Scripts
└───__pycache__

From Powershell I then build the Docker image by writing the command:

docker build . -t myusername/flaskapp
PS C:\Users\mypcuser\projects\flask_docker_test> docker build . -t myusername/flaskapp
Sending build context to Docker daemon  19.49MB
Step 1/5 : FROM python:3
 ---> f88b2f81f83a
Step 2/5 : RUN pip install --upgrade pip
 ---> Running in 56dc287d7501
Requirement already up-to-date: pip in /usr/local/lib/python3.8/site-packages (20.0.2)
Removing intermediate container 56dc287d7501
 ---> 2dff8ebf09c6
Step 3/5 : RUN pip install flask
 ---> Running in 5b59f8968a63
Collecting flask
  Downloading Flask-1.1.1-py2.py3-none-any.whl (94 kB)
Collecting Werkzeug>=0.15
  Downloading Werkzeug-1.0.0-py2.py3-none-any.whl (298 kB)
Collecting click>=5.1
  Downloading click-7.1.1-py2.py3-none-any.whl (82 kB)
Collecting Jinja2>=2.10.1
  Downloading Jinja2-2.11.1-py2.py3-none-any.whl (126 kB)
Collecting itsdangerous>=0.24
  Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting MarkupSafe>=0.23
  Downloading MarkupSafe-1.1.1-cp38-cp38-manylinux1_x86_64.whl (32 kB)
Installing collected packages: Werkzeug, click, MarkupSafe, Jinja2, itsdangerous, flask
Successfully installed Jinja2-2.11.1 MarkupSafe-1.1.1 Werkzeug-1.0.0 click-7.1.1 flask-1.1.1 itsdangerous-1.1.0
Removing intermediate container 5b59f8968a63
 ---> 7583bc2d8be6
Step 4/5 : CMD ["python","app.py"]
 ---> Running in 9394be530612
Removing intermediate container 9394be530612
 ---> 53e72fb77552
Step 5/5 : COPY app.py /app.py
 ---> 5925b08ae09e
Successfully built 5925b08ae09e
Successfully tagged myusername/flaskapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
PS C:\Users\mypcuser\projects\flask_docker_test>

I then go ahead and run my app using this command:

docker run -p 5001:5000 -t myusername/flaskapp

And get this output:

 * Serving Flask app "app" (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)

But when I go to this URL in both Firefox, Google Chrome and Postman I get this:

enter image description here

davidism
  • 121,510
  • 29
  • 395
  • 339
bjornsing
  • 322
  • 6
  • 25

1 Answers1

2

127.0.0.1 refers to the localhost or the actual inside of the container. You need to access it via your docker network adapter. It is usually something like 172.17.0.1 but yours may differ.

You can find the docker containers IP address by running the following

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <CONTAINER_NAME>

happysad-
  • 86
  • 5
  • Ok. I got this IP from writing your command: 172.17.0.7. When entering it to the browser I get a time out error ```Could not get any response There was an error connecting to http://172.17.0.7. Why this might have happened: The server couldn't send a response: Ensure that the backend is working properly Self-signed SSL certificates are being blocked: Fix this by turning off 'SSL certificate verification' in Settings > General Proxy configured incorrectly Ensure that proxy is configured correctly in Settings > Proxy Request timeout: Change request timeout in Settings > General``` – bjornsing Mar 31 '20 at 14:21
  • @bjornsing You also need to expose port 5000 in your Dockerfile `EXPOSE 5000` and then when you navigate to it use `http://172.17.0.7:5001` as youre forwarding the container port `5000` to `5001` on your host. – happysad- Mar 31 '20 at 14:25