Kaderma
Kaderma

Reputation: 155

Docker executes python scripts only when I stopping the container

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.

Upvotes: 0

Views: 438

Answers (1)

Debdut Goswami
Debdut Goswami

Reputation: 1379

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.

Upvotes: 1

Related Questions