Can't run django rest framework with docker - docker

I'm currently learning Docker and django rest at the same time.
When i run the command python3 manage.py runserver I can access to django admin page from http://localhost:8000/admin/, but when i run docker run -p 80:80 docker_django_tutorial the page is anccesible. (docker_django_tutorial is the name of my docker image)
I guess i need to add somewhere python3 manage.py runserver in my dockerfile ?
Here is my Dockerfile:
#Use the Python3.7.2 container image
FROM python:3.7.2-stretch
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
and here my file requirements.txt:
Django==3.1.1
djangorestframework

You're telling Django to listen on port 8000 but then telling Docker to expose port 80. Use -p 8000:8000 instead. If you want it to listen on port 80, you can use -p 80:8000. The first number is host (your system) port and the second is container port (Django).

Related

Unable to access distant neo4j from my docker fastapi app

I have 2 things : a neo4j database which is deployed on GCP compute engine at this IP bolt://35.241.254.136:7687
And I have a fastapi app that need to access to neo4j.
When I run the API server with uvicorn main:app --reload, all is working correctly. I can access my distant database.
However when I run the api with docker (docker run -d --name mycontainer -p 80:80 myimage), it's impossible to access the neo4j database and I have this error
ServiceUnavailable( neo4j.exceptions.ServiceUnavailable: Couldn't connect to 35.241.254.136:7687 (resolved to ('35.241.254.136:7687',)):
Maybe something is wrong, I don't know about docker.
Here is my dockerfile
FROM python:3.9
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./ /code/app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
Make sure your container can see the jeo4j IP (35.241.254.136).
Execute the container's bash:
docker exec -it mycontainer bash
then ping the url:
ping 35.241.254.136
If you cannot reach to the host, you need to put your container in same network with jeo4j.
This link may be helpful.

Docker runs only on Port 80

I am unable to run my docker image on Port 4000, even though I can run it on Port 80. What am I doing wrong here?
FROM node:latest as build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:latest
COPY --from=build /usr/src/app/dist/admin /usr/share/nginx/html
EXPOSE 4200
I'm creating the image using the following command:
docker build --pull --rm -f "DockerFile" -t admin1:v1 "."
When I run it on port 80, I'm able to use it:
docker run --rm -d -p 4200:4200/tcp -p 80:80/tcp admin1:v1
However, when I run the following command, I'm unable to use it:
docker run --rm -d -p 4200:4200/tcp -p 4000:4000/tcp admin1:v1
I have researched similar questions online, but I haven't been able to fix the problem. Any suggestion will be greatly appreciated!
You need to map the docker container port to the docker host port.
Try the following Command
docker run --rm -d -p 4200:4200/tcp -p 4000:80/tcp admin1:v1
The following is the extract from the Docker Documentation
-p 8080:80 Map TCP port 80 in the container to port 8080 on the Docker host.
You can refer the link for further information.
Docker Documentation

I can't get access to an exposed port in docker

I'm using Ubuntu 20.04 and running Python 3.8. Here is my dockerfile:
FROM python:3.8
WORKDIR /usr/src/flog/
COPY requirements/ requirements/
RUN pip install -r requirements/dev.txt
RUN pip install gunicorn
COPY flog/ flog/
COPY migrations/ migrations/
COPY wsgi.py ./
COPY docker_boot.sh ./
RUN chmod +x docker_boot.sh
ENV FLASK_APP wsgi.py
EXPOSE 5000
ENTRYPOINT ["./docker_boot.sh"]
and my docker_boot.sh
#! /bin/sh
flask deploy
flask create-admin
flask forge
exec gunicorn -b 0.0.0.0:5000 --access-logfile - --error-logfile - wsgi:app
I ran docker run flog -d -p 5000:5000 in my terminal. And I couldn't get my app working by typing localhost:5000 but it worked quite well when I typed 172.17.0.2:5000 (the docker machine's ip address). But I want the app to run on localhost:5000.
I'm sure there is nothing wrong with the requirements/dev.txt and the code because it works well when I run flask run directly in my terminal.
Edit on 2021.3.16:
Add docker ps information when docker run flog -d -p 5000:5000 is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff048e904183 flog "./docker_boot.sh -d…" 8 seconds ago Up 6 seconds 5000/tcp inspiring_kalam
It is strange that there's no mapping of the hosts. I'm sure the firewall is off.
Can anyone help me? Thanks.
Use docker run -d -p 0.0.0.0:5000:5000 flog.
The arguments and the flags that are after the image name are passed as arguments to the entrypoint of the container created from that image.
Run docker ps and you need to see something like
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
565a97468fc7 flog "docker_boot.sh" 1 minute ago Up 1 minutes 0.0.0.0:5000->5000/tcp xxxxxxxx_xxxxxxx

Container is automatically exiting

Below is my Dockerfile:
FROM python:3
COPY . /Demo
WORKDIR /Demo
RUN pip install -r requirements.txt
EXPOSE 9005
CMD python ./app.py
I am using following command to run the resulting image:
docker run -it -d host:port imagename:v1
The container is automatically exiting. When I run docker ps, no running container is shown.
Instead of this line:
CMD python ./app.py
give absolute path like below:
CMD python /<path-to-script>/app.py

How do I map a port on a container to the host?

I'm trying to run a container described by the following Dockerfile:
FROM node:11.4.0
RUN npm install -g sh
RUN npm install -g json-server
WORKDIR /data
VOLUME /data
COPY db.json /data
CMD json-server --watch db.json --port 3001
and specifying the listening port by running:
docker run -it -p 3001:3001 abelalejandro/json-server:final
The container seems to be running fine and json-server is telling me it is serving my requests on port 3001 yet I can't get any joy when browsing http://localhost:3001
Am I missing something on publishing/exposing ports?
It is binding to localhost instead of 0.0.0.0 (any host).
You can change that by setting:
CMD json-server --watch db.json --host 0.0.0.0 --port 3001
I'm assuming you are using https://github.com/typicode/json-server.

Resources