I need to build a small program with microservice architecture:
server service (Python fast API framework)
I run it with Dockerfile command:
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
client service: simple Python CLI textual requires input as username from input CLI and to connect server GET/POST HTTP requests
unsername= input("Please insert your unsername:")
log.info(f"{unsername}")
I run it with Dockerfile command:
CMD ["python", "./main.py"]
I am not sure how to run my client with the docker to run the main but with no existing.
when I am running with venv from 2 different terminals the client and the server all work as expected and succeed to connect (because both of them are on my machine)
with docker.
I got an error related to the username I try to input
EOFError: EOF when reading a line
even if delete the input I still got an error conn = connection.create_connection...Failed to establish a new connection like the client failed to connect my server when it on isolated container.
for enable input from terminal need to adding to docker-compose :
tty: true # docker run -t
stdin_open: true # docker run -i
Related
I'm trying to containerize a nodejs app into a container image.
So in the docker documents we see the correct way to map the host port to the container port is hostport:containerport
Using the same, I've tried to run the command:
docker run -i -t -p 3007:8080
Where 3007 is the port my nodejs app is listening to and 8080 is the hostport(my laptop).
But I keep getting the error "localhost refused to connect" when I hit localhost:8080 in my browser.
It wasn't until I swapped these port numbers like:
docker run -i -t -p 8080:3007
To render the actual app (listening on port 3007) in my browser.
I'm confused as to why this happens? Am I missing any information?
I have docker running on my Windows 10 OS and I have a minimal flask app
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host ='0.0.0.0', port = 5001, debug = True)
And I am dockerizing it using the following file
FROM python:alpine3.7
COPY . /opt
WORKDIR /opt
RUN pip install -r requirements.txt
EXPOSE 5001
ENTRYPOINT [ "python" ]
CMD ["app.py", "run", "--host", "0.0.0.0"]
From what I am seeing on other posts and on Flask tutorials having a 0.0.0.0 should allow me to connect from the windows firefox browser when I type 0.0.0.0:5001 but it is not connecting, I keep getting a 'unable to connect' message. I remember using the 0.0.0.0:port to connect in localhost on a linux ubuntu machine but for whatever reason its not letting me connect on Windows. Is there a special setting to connect on windows ?
Inside the Docker container, the private port is 5001. This private port then needs to be mapped to a public port when running the container. For example, to set the public port to 8000, you could run:
$ docker run --publish 8000:5001 --name <docker-container> <docker-container>:<version-tag>
The Flask app would then be accessible at URL: http://127.0.0.1:8000
Dockerfile
In addition, since in app.py you are setting the host and port, there is no need to specify these values in the Dockerfile CMD. But the public port (in this example 8000) needs to be exposed.
It also looks like the COPY command is placing everything under an /opt directory, so that needs to be included in the app path when launching the Flask app within Docker.
FROM python:alpine3.7
COPY . /opt
WORKDIR /opt
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["python", "/opt/app.py"]
Docker-Flask Example
For a complete Flask-Docker example, including using Gunicorn, see:
Docker Python Flask Example using the Gunicorn WSGI HTTP Server
Here is my dockerfile:
FROM python:3.8
WORKDIR /locust
RUN pip3 install locust
COPY ./ /locust/
EXPOSE 8089
CMD ["locust", "-f", "locustfile.py"]
Here is the response:
Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
Starting Locust 1.2.3
But when I try to access it in the browser - it doesn't load. I feel like I might be missing something simple, but cannot find it.
This statement,
EXPOSE 8089
will only expose your port for inter-container communication, but not to the host.
For allowing host to communicate on the container port you will need to bind the port of host and container in the docker run command as follows
docker run -p <HOST_PORT>:<CONTAINER:PORT> IMAGE_NAME
which in your case will be
docker run -p 8089:8089 IMAGE_NAME
I have some problems running Django on an ECS task.
I want to have a Django webapp running on an ECS task and accessible to the world.
Here are the symptoms:
When I run an ECS task using Django python manage.py runserver 0.0.0.0:8000 as entry point for my container, I have a connection refused response.
When I run the task using Gunicorn using gunicorn --bind 0.0.0.0:8000 my-project.wsgi I have no data response.
I don't see logs on CloudWatch and I can't find any server's logs when I ssh to the ECS instance.
Here are some of my settings related to that kind of issue:
I have set my ECS instance security groups inbound to All TCP | TCP | 0 - 65535 | 0.0.0.0/0 to be sure it's not a firewall problem. And I can assert that because I can run a ruby on rails server on the same ECS instance perfectly.
In my container task definition I set a port mapping to 80:8000 and an other to 8000:8000.
In my settings.py, I have set ALLOWED_HOSTS = ["*"] and DEBUG = False.
Locally my server run perfectly on the same docker image when doing a docker run -it -p 8000:8000 my-image gunicorn --bind=0.0.0.0:8000 wsgi or same with manage.py runserver.
Here is my docker file for a Gunicorn web server.
FROM python:3.6
WORKDIR /usr/src/my-django-project
COPY my-django-project .
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["gunicorn","--bind","0.0.0.0:8000","wsgi"]
# CMD ["python","manage.py", "runserver", "0.0.0.0:8000"]
Any help would be grateful!
To help you debugging:
What is the status of the job when you are trying to access your webapp.
Figure out which instance the job is running and try docker ps on that ecs instance for the running job.
If you are able see the container or the job running on the instance, try access your webapp directly on the server with command like curl http://localhost:8000 or wget
If you container is not running. Try docker ps -a and see which one has just stopped and check with docker logs -f
With this approach, you can cut out all AWS firewall settings, so that you can see if your container is configured correctly. I think it will help you tracking down the issue easier.
After you figuring out the container is running fine and you are able to request with localhost, then you can work on security group inbound/outbound filter.
I'm messing about with Docker (using Docker Toolbox for OSX) and can't seem to get my application to work. It's a simple flask application which looks like this:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Hello World!'
if __name__ == '__main__':
app.run()
Now my Dockerfile contains the following
FROM python:2.7.11-wheezy
ADD ./application/* /opt/local/application/
ADD ./project-requirements.txt /opt/local/application/requirements.txt
RUN pip install -r /opt/local/application/requirements.txt
CMD ["/usr/local/bin/python", "/opt/local/application/app.py"]
EXPOSE 5000
I build the container by running docker build -t python_app . and subsequently boot the container by running docker -i -P python_app and see that the application is booted inside the container, as the output of the command is * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit).
Now when I run docker ps I can see the container is running
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
448defb349ce python_app "/usr/local/bin/pytho" About a minute ago Up About a minute 0.0.0.0:32769->5000/tcp angry_jang
But when I try to curl the container, I get a connection refused error.
$ curl $(docker-machine ip default):32769
curl: (7) Failed to connect to 192.168.99.100 port 32769: Connection refused
I have no clue where I'm going wrong with this and any help is much appreciated!
I found the issue, I had to change
app.run()
to
app.run(host='0.0.0.0')
in the app.py file.