getting this Error: "You don't have permission to access that port" for all the ports I have tried - port

Getting this Error: "You don't have permission to access that port" for every solution I have tried to run the server
python3 manage.py runserver
enter image description here
sudo python3 manage.py runserver 80
enter image description here
python manage.py runserver localhost:18080
enter image description here
python3 manage.py runserver --insecure 0.0.0.0:80
enter image description here
I have used several cmds to run django server but for all these I am getting the same error

Related

Docker container stops after a few seconds and shows 'in use (dangling)'

I am using Docker Desktop in Windows 10. I dockerized a Machine Learning project using Django as a web framework. Whenever I am going to run the image, it just runs for a few seconds and stops. In Docker Desktop, it shows 'in use (dangling).'
I restarted the container and did other stuff but it did not work.
This is my Dockerfile:
FROM python
RUN mkdir /disease-prediction
WORKDIR /disease-prediction
ADD . /disease-prediction
RUN pip install -r requirements.txt
EXPOSE 8000
CMD python manage.py runserver 0:8000

docker-compose up command not responding

I am using Ubuntu and tried to run Django with Docker.
When I give docker-compose up command it give below output
Starting aug6_web_1 ... done
Attaching to aug6_web_1
web_1 | Watching for file changes with StatReloader
and does not print anything else ... for almost 20 min now.
Here is my docker-compose.yml:
version: '3'
services:
web:
build:
context: ./
command: python manage.py runserver 127.0.0.0:8000
volumes:
- .:/AUG6
ports:
- '8000:8000'
And my Dockerfile:
FROM python:3
ENV PYTHONBUFFERED 1
RUN mkdir /AUG6
WORKDIR /AUG6
COPY /requirements.txt /AUG6/
RUN pip install -r requirements.txt
COPY . /AUG6/
Very first day of learning Docker, excuse if any mistakes
docker-compose up will not terminate as long as the containers are running.
If you want it to start the containers and then terminate, use docker-compose up -d.
Check the docker-compose manual.
EDIT 1:
I just spotted a mistake in your runserver command:
You have python manage.py runserver 127.0.0.0:8000 but it should be python manage.py runserver 127.0.0.1:8000.
You can remove the IP:PORT completely, as you use the defaults. Check the docs.

Heroku multiple commands in Docker deployment

I'm trying to execute multiple steps after Heroku builds the container with my Python Django application inside.
What I started with in heroku.yml (and it worked just fine), was:
build:
docker:
web: Dockerfile
release:
image: web
command:
- python manage.py collectstatic --noinput
Now, I'd like to run migrations as well, and run some other commands (I'm using sqlite database), but the following does not work, and does not produce any output.
build:
docker:
web: Dockerfile
release:
image: web
command:
- python manage.py collectstatic --noinput && python manage.py makemigrations --no-input && python manage.py migrate --no-input && python manage.py migrate auth --no-input && python manage.py makemigrations auth --no-input && python manage.py migrate --no-input --run-syncdb && echo "from django.contrib.auth.models import User; User.objects.create_superuser('user', 'user#example.com', 'example', first_name='The', last_name='User')" | python manage.py shell"
Do you know what I could be missing here? I'm using Free Heroku plan.
I tried wrapping the commands in bash -c "COMMANDS GO HERE", but then it get errors of python not found (tried both python and python3)
I've managed to get it working. In the heroku.yml, just call the shell script containing all the steps:
build:
docker:
web: Dockerfile
release:
image: web
command:
- ./on_release.sh
For me, it worked when I placed the script in the workdir of my container (the same place where manage.py resides). I couldn't find more information about how exactly it works alltogether.
From heroku logs -t, it turns out that Heroku already uses "bash -c" under the hood (probably that's also how Docker wraps the commands from Yaml file).

adding health check to docker container

I am trying to add a health check to my docker container so in my Dockerfile I added this line
HEALTHCHECK CMD curl --fail http://localhost:8080/health || exit 1
based loosely on this tutorial: https://howchoo.com/g/zwjhogrkywe/how-to-add-a-health-check-to-your-docker-container . In my docker-compose file I have added the health check line like this:
healthcheck:
test: ["CMD", "curl", "--silent", "--fail", "http://localhost:8080/health"]
but the container always reports unhealthy. So if I execute docker exec -it my-container /bin/bash and get inside the container and then execute the health request I get this:
$ curl --fail http://localhost:8080/health
curl: (22) The requested URL returned error: 411 Length Required
What am I missing? Nginx is already installed in the container so I would like to simply make that URL with /health work.
I gave up on using the HEALTHCHECK command in Dockerfile and instead I am changing the nginx.conf file by adding
location /health {
access_log off;
return 200 "healthy\n";
}
docker-compose.yml remains the same.
This works well enough for me.
An alternative approach, correlating a port being listened on with a healthy status.
netcat is installed by default in Alpine Linux
nginx:
image: nginx:1.17-alpine
restart: unless-stopped
healthcheck:
test: ["CMD", "nc", "-vz", "-w1", "localhost", "80"]
interval: 1s
timeout: 1s
retries: 30
This will exit with status 0 if port 80 is open.
As a solution, you can use the following example -
How to Add a Health Check to Your Docker Container
There's a minor correction to the Dockerfile they provide though as there's an issue to check the health status (curl had to be installed there as well but let me know if you need any help or have any question).
Please refer to this particular one as a solution -
FROM python:3.6-alpine
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
RUN mkdir /data \
&& apk add --no-cache \
openssl \
curl \
dumb-init \
postgresql-libs \
ca-certificates
#CMD apt-get install curl
HEALTHCHECK CMD curl -f http://localhost:5000/ || exit 1
CMD ["python", "app.py"]

Can't connect to docker endpoint even with docker-compose up successful

I am trying to learn how to containerize my flask api with docker. I am very new to docker, but from my understanding, I was able to build/update it. When I navigate to my route I get site can't be reached.
Any help would be greatly appreciated. Thank you.
Here is my yml file:
version: "3.6"
services:
users:
build:
context: ./services/users
dockerfile: Dockerfile-dev
volumes:
- "./services/users:/usr/src/app"
ports:
- 5001:5000
environment:
- FLASK_APP=project/__init__.py
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
here is my dockerfile:
FROM python:3.6.5-alpine
WORKDIR /usr/src/app
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
COPY . /usr/src/app
CMD python manage.py -h 0.0.0.0
and here is my powershell command and output:
docker-compose -f docker-compose-dev.yml up -d --build
upon further diagnosis I found this from the ps command
I however cannot find in docker documentation what state of exit 2 means. Unless that is bash for misuse of shell builtin: http://www.tldp.org/LDP/abs/html/exitcodes.html. In that case I really don't know my problem and would appreciate any help!
Edit 3:
upon reading some github threads removing the -d flag from my command showed more information but it is still cryptic if anyone has an explanation for it:
I guess new flask versions doesn't support -h anymore, try with --host -
Change CMD statement in Dockerfile to -
CMD python manage.py runserver --host 0.0.0.0
Ref - https://flask-script.readthedocs.io/en/latest/

Resources