Django Fargate. The requested resource was not found on this server - django

I have seem The requested resource was not found on this server, like a thousand times but none of the awnser match my problem.
I made this app from statch, and add a Dockerfile for it.
FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /www
COPY requirements.txt /www/
RUN pip install -r requirements.txt
COPY . /www/
EXPOSE 80
CMD [ "python", "manage.py", "runserver", "0.0.0.0:80" ]
Then i run this:
docker build -t pasquin-django-mainapp .
docker run pasquin-django-mainapp
I did that in my local environment, not woking. Y push this Dcokerfile to ECR and then use it in ECS + Fargate, same bada result. I dont know what else todo.
Somebody, help! thanks!
ps: From docker-compose works just marvelous!

Related

Cannot launch my Django project with Gunicorn inside Docker

I'm new to Docker.
Visual Studio Code has an extension named Remote - Containers and I use it to dockerize a Django project.
For the first step the extension creates a Dockerfile:
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10.5
EXPOSE 8000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
# File wsgi.py was not found. Please enter the Python path to wsgi file.
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "project.wsgi"]
Then it adds Development Container Configuration file:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.238.0/containers/docker-existing-dockerfile
{
"name": "django-4.0.5",
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerFile": "../Dockerfile"
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Uncomment the next line to run commands after the container is created - for example installing curl.
// "postCreateCommand": "apt-get update && apt-get install -y curl",
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
And finally I run the command Rebuild and Reopen in Container.
After a few seconds the container is running and I see a command prompt. Considering that at the end of Dockerfile there's this line:
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "project.wsgi"]
...the Django application must be running, but it isn't and http://127.0.0.1:8000 refuses to connect.
But if I run the same command at the command prompt:
gunicorn --bind 0.0.0.0:8000 project.wsgi
It works just fine?
Why? All files are created by VSCode extension. Same instructions are posted on VSCode official website, yet it's not working.
P.S:
When the container loaded for the first time, I created a project called 'project' so the folder structure is like this:

AWS copilot with Django never finishes deploying

I've followed the this short guide to create a django app with docker
https://docs.docker.com/compose/django/
and then following copilot instructional to push up the container to ECS:
https://aws.amazon.com/blogs/containers/introducing-aws-copilot/
I've also used this sample to test everything -- which works out fine:
https://github.com/aws-samples/aws-copilot-sample-service
The deploy completes and outputs and URL endpoint.
In my case, the everything is successfully built, but once the test environment is being deployed it just continuously builds at this:
72ff4719 size: 3055
⠏ Deploying load-bal:7158348 to test.
and never finishes. I've even downsized my requirements.txt to a bare minimum.
My Dockerfile
FROM python:3.7.4
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
EXPOSE 80
COPY . /code/
docker-compose.yml
version: "3.8"
services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
requirements.txt
Django==3.0.8
djangorestframework==3.11.0
gunicorn==20.0.4
pipenv==2020.6.2
psycopg2-binary==2.8.5
virtualenv==16.7.6
Instructions I follow:
sudo docker-compose run web django-admin startproject composeexample .
Successfully creates the Django App
copilot init
Setup naming for app and load balancer
Choose to create test environment
Everything builds successfully and then just sits here. I've tried a number of variations, but the only one that works is just doing the copilot instructional without django involved.
6f3494a64128: Pushed
cfe650cc4def: Pushed
a477d6671cc7: Pushed
90df760355a7: Pushed
574ea6c52bdd: Pushed
d1573fad78d1: Pushed
14c1ff636882: Pushed
48ebd1638acd: Pushed
31f78d833a92: Pushed
2ea751c0f96c: Pushed
7a435d49206f: Pushed
9674e3075904: Pushed
831b66a484dc: Pushed
ini: digest: sha256:b7460876bc84b1a26e7513fa6d17b5bffd5560ae958a933984376ed2c9fe53f3 size: 3052
⠏ Deploying aiinterview-lb:ini to test.
tl;dr the Dockerfile that's being used by this tutorial is incomplete for Copilot's purposes. It needs an extra line containing
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
and the EXPOSE directive should be updated to 8000. Because Copilot doesn't recognize Docker Compose syntax and there's no command or entrypoint specified in the Dockerfile, the image will never start with Copilot's configuration settings.
Details
AWS Copilot is designed around "services" consisting of an image, possible sidecars, and additional storage resources. That means that its basic unit of config is the Docker image and the service manifest. It doesn't natively read Docker Compose syntax, so all the config that Copilot knows about is that which is specified in the Dockerfile or image and each service's manifest.yml and addons directory.
In this example, designed for use with Docker Compose, the Dockerfile doesn't have any kind of CMD or ENTRYPOINT directive, so the built image which gets pushed to Amazon ECR by Copilot won't ever start. The tutorial specifies the image's command (python manage.py runserver 0.0.0.0:8000) as an override in docker-compose.yml, so you'll want to update your Dockerfile to the following:
FROM python:3.7.4
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
EXPOSE 8000
COPY . /code/
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Note here that I've changed the EXPOSE directive to 8000 to match the command from docker-compose.yml and added the command specified in the web section to the Dockerfile as a CMD directive.
You'll also want to run
copilot init --image postgres --name db --port 5432 --type "Backend Service" --deploy
This will create the db service specified in your docker-compose.yml. You may need to run this first so that your web container doesn't fail to start while searching for credentials.
Some other notes:
You can specify your database credentials by adding variables and secrets in the manifest file for db which is created in your workspace at ./copilot/db/manifest.yml. For more on how to add a secret to SSM and make it accessible to your Copilot services, check out our documentation
variables:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
secrets:
POSTGRES_PASSWORD: POSTGRES_PASSWORD
Your database endpoint is accessible over service discovery at db.$COPILOT_SERVICE_DISCOVERY_ENDPOINT--you may need to update your service code which connects to the database to reflect this endpoint instead of localhost or 0.0.0.0.

getting "memory error" during aws elastic beanstalk docker deployment

I have a machine learning model with flask api dockerized and when I am trying to deploy it gives me a vague error but checking the expanded logs I can see that it was a memory error when installing tensorflow
As an aside, I also have included the model.h5 file within the docker image, should I not be doing this? Otherwise it is just the .py and config files (no venv in directory)
This is my Dockerfile (it works locally btw, but beanstalk has a memory limit which is getting hit and im not sure why)
FROM python:latest
COPY . /app
WORKDIR /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app.py", "--host=0.0.0.0"]

How to translate flask run to Dockerfile?

So I'm trying to learn how to containerize flask apps and so far I've understood two ways of firing up a flask app locally:
One is to have this code in the main file:
if __name__ == '__main__':
APP.run(host='0.0.0.0', port=8080, debug=False)
and run with
python3 main.py
The other is to remove the above code from main.py, and just define an environment variable and do flask run:
export FLASK_APP=main.py
flask run
I tried to convert both of these methods into a Dockerfile:
ENTRYPOINT["python3", "main.py"]
which works quite well for the first. However when I try to do something like:
ENV FLASK_APP "./app/main.py"
ENTRYPOINT ["flask", "run"]
I am not able to reach my server via the browser. The container starts up all well, just that it's not reachable. One trick that does work, is if I add the host address in the entrypoint like so:
ENTRYPOINT ["flask", "run", "--host=0.0.0.0"]
I am not sure why do I have to the --host to the entrypoint when locally I can do also without it. Another funny thing that I noticed, was that if I put the host as --host=127.0.0.1, it still doesn't work.
Can someone explain what really is happening here? Either I don't understand the ENTRYPOINT correctly or maybe flask.. or maybe both.
EDIT: The whole Dockerfile for reference is:
FROM python:stretch
COPY . /app
WORKDIR /app
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
ENV FLASK_APP "/app/main.py"
ENTRYPOINT ["flask", "run", "--host=127.0.0.1"]
Try to define FLASK_APP env via absolute path.
Or put this string to your Dockerfile:
WORKDIR /path/to/dir/that/contains/main.py/file
Oh sorry. Host in ENTRYPOINT statement must be 0.0.0.0 :
ENTRYPOINT ["flask", "run", "--host=0.0.0.0"]
And do not forget to tie 5000 port outside via -p option :
docker run -p 5000:5000 <your container>
i believe this task would be better accomplished implementing CMD as opposed to ENTRYPOINT.
(you should also define the working directory before running COPY command.)
for example, your Dockerfile should look something like..
FROM python:stretch
WORKDIR /app
COPY . .
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
CMD python3 main.py

docker image is running but webpage error - Docker

I have a basic django project and I am trying to get it running locally through docker. I have the docker file. I build the docker image. I ran the docker image. It is running, but my webpage shows an error on the screen like it is not connecting to the docker server... Here is what I have:
docker file:
FROM python:3
WORKDIR general
COPY requirements.txt ./
EXPOSE 8000
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Here is how I am buiding and running this project:
omars-mbp:split omarjandali$ docker build -t splitbeta/testing2 .
Sending build context to Docker daemon 223.7kB
Step 1/7 : FROM python:3
---> 79e1dc9af1c1
Step 2/7 : WORKDIR general
---> 04a6f8a7f92a
Removing intermediate container b2ffb485e485
Step 3/7 : COPY requirements.txt ./
---> 649d77ec499e
Step 4/7 : EXPOSE 8000
---> Running in 7d8d6fe8de1d
---> c328d885a5f1
Removing intermediate container 7d8d6fe8de1d
Step 5/7 : RUN pip install -r requirements.txt
---> Running in 1c9aca43dc14
Collecting Django==1.11.5 (from -r requirements.txt (line 1))
Downloading Django-1.11.5-py2.py3-none-any.whl (6.9MB)
Collecting gunicorn==19.6.0 (from -r requirements.txt (line 2))
Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
Collecting pytz (from Django==1.11.5->-r requirements.txt (line 1))
Downloading pytz-2017.3-py2.py3-none-any.whl (511kB)
Installing collected packages: pytz, Django, gunicorn
Successfully installed Django-1.11.5 gunicorn-19.6.0 pytz-2017.3
---> 602e88557c8b
Removing intermediate container 1c9aca43dc14
Step 6/7 : COPY . .
---> 55cff629cb51
Step 7/7 : CMD python manage.py runserver 0.0.0.0:8000
---> Running in efd75f8fb602
---> 2cef664a626d
Removing intermediate container efd75f8fb602
Successfully built 2cef664a626d
Successfully tagged splitbeta/testing2:latest
omars-mbp:split omarjandali$ docker run -d spltibeta/testing2
Here is the project running:
omars-mbp:split omarjandali$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc14f03a18b0 splitbeta/testing2 "python manage.py ..." 3 seconds ago Up 3 seconds 8000/tcp loving_volhard
THe webpage is giving the following error when it is supposed to display a template page....
This site can’t be reached
127.0.0.1 refused to connect.
I got it running yesterday but it is not working any more... I dont know why. I didnt change anything
I am logged into my dockerhub account in my terminal
It seems your Docker run command doesn't publish port 8000. By default, docker won't publish any container ports on the host system if you don't tell it to explicitly. Try using the -p or --publish option of docker run:
docker run -d -p 8000:8000 spltibeta/testing2
Alternatively, you can use the -P or --publish-all option to publish all exposed ports of your container on your host system. This will assign a random port on the host.
docker run -d -P spltibeta/testing2