0

i am trying to upload django app at microsoft azure using their solution app services for containers. The APP without docker is working perfectly, after i added docker it's working locally but when i use microsoft container registry and upload it to and then try to use their app for containers solution it doesn't work. I need some help in here:

I created basic django restapiframework. Didn't add anything in there. After that i checked if it's working locally and it's working perfectly. After that i uploaded it on microsoft azure web app (not for cointaners) and it worked perfectly. After that i tried to add docker to my project and use microsoft azure web services for containers.

Below is my docker code and commands i try to use for locally:

DockerCompose:

version: '3.7'

services:
  web:
    build: .
    command: bash -c "python manage.py makemigrations && python manage.py migrate --run-syncdb && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    volumes:
      - ./django_dashboard:/usr/src/django_dashboard
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev

DockerFile:

FROM python:3.6

# set work directory
WORKDIR /usr/src

# install dependencies
RUN pip install --upgrade pip
COPY requirements.txt /usr/src/requirements.txt
RUN pip install -r requirements.txt

# set work directory
WORKDIR /usr/src/django_dashboard

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1 #Prevents Python from writing pyc files to disc (like python -B)
ENV PYTHONUNBUFFERED 1 #Prevents Python from buffering stdout and stderr (like python -u)

# copy project
COPY . /usr/src/django_dashboard/

Tree of my files

├── backend
│   └── django_dashboard
│       ├── db.sqlite3
│       ├── django_dashboard
│       │   ├── asgi.py
│       │   ├── __init__.py
│       │   ├── __pycache__
│       │   │   ├── __init__.cpython-36.pyc
│       │   │   ├── settings.cpython-36.pyc
│       │   │   ├── urls.cpython-36.pyc
│       │   │   └── wsgi.cpython-36.pyc
│       │   ├── settings.py
│       │   ├── urls.py
│       │   └── wsgi.py
│       ├── Dockerfile
│       ├── __init__.py
│       ├── manage.py
│       ├── requirements.txt
│       └── rest
│           ├── admin.py
│           ├── apps.py
│           ├── __init__.py
│           ├── models.py
│           ├── __pycache__
│           │   ├── admin.cpython-36.pyc
│           │   ├── __init__.cpython-36.pyc
│           │   ├── models.cpython-36.pyc
│           │   ├── serializers.cpython-36.pyc
│           │   └── views.cpython-36.pyc
│           ├── serializers.py
│           ├── tests.py
│           └── views.py
├── docker-compose.yml
├── frontend
│   ├── Dockerfile
│   ├── package.json
│   ├── public
│   │   ├── favicon.ico
│   │   └── index.html
│   ├── README.md
│   └── src
│       ├── App.css
│       ├── App.js
│       ├── App.test.js
│       ├── components
│       │   └── Modal.js
│       ├── index.css
│       ├── index.js
│       └── logo.svg
└── README.md

To run it locally I am using:

docker-compose build --no-cache
docker-compose up

AFter that i am following this: https://learn.microsoft.com/bs-latn-ba/azure/container-instances/container-instances-tutorial-prepare-acr

and next things i am trying to run app services from their portal and i get in the logs:

2020-03-16 15:10:06.070 INFO  - Starting container for site
2020-03-16 15:10:06.071 INFO  - docker run -d -p 1219:8000 --name MYWEBSITE -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITES_PORT=8000 -e WEBSITE_SITE_NAME=MYWEBSITE -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=MYWEBSITE.azurewebsites.net -e WEBSITE_INSTANCE_ID=f2b41f3b5bb846770d96154839b55ad19f2b5bc4fef2a60a950ee081118ef8b0 MYWEBSITE.azurecr.io/MYWEBSITE:1v  

2020-03-16 15:10:06.071 INFO  - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2020-03-16 15:10:33.890 INFO  - Initiating warmup request to container MYWEBSITE for site MYWEBSITE
2020-03-16 15:10:34.336 ERROR - Container MYWEBSITE for site MYWEBSITE has exited, failing site start
2020-03-16 15:10:34.656 ERROR - Container MYWEBSITE didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
2020-03-16 15:10:34.926 INFO  - Stoping site MYWEBSITE because it failed during startup.

Replaced name of the website with word MYWEBSITE

What am I doing wrong? Thanks for you help.

  • As I see your application did not start. You can try to add the startup file when you deploy the image to web app with the command you set in your docker-compose file `python manage.py makemigrations && python manage.py migrate --run-syncdb && python manage.py migrate && python manage.py runserver 0.0.0.0:8000`. – Charles Xu Mar 17 '20 at 01:30
  • I tried to create a file init.sh in main folder with `#!/bin/bash python django_dashboard/manage.py makemigrations && python django_dashboard/manage.py migrate --run-syncdb && python django_dashboard/manage.py migrate && python django_dashboard/manage.py runserver 0.0.0.0:8000` and it doesn't work – Niewasz Biznes Mar 17 '20 at 07:26
  • `Container start failed for MYWEBSITE with System.AggregateException, One or more errors occurred. (Docker API responded with status code=BadRequest, response={"message":"OCI runtime create failed: container_linux.go:345: starting container process caused \"exec: \\\"init.sh\\\": executable file not found in $PATH\": unknown"} ) (Docker API responded with status code=BadRequest, response={"message":"OCI runtime create failed: container_linux.go:345: starting container process caused \"exec: \\\"init.sh\\\": executable file not found in $PATH\": unknown"} )` – Niewasz Biznes Mar 17 '20 at 07:27
  • `InnerException: Docker.DotNet.DockerApiException, Docker API responded with status code=BadRequest, response={"message":"OCI runtime create failed: container_linux.go:345: starting container process caused \"exec: \\\"init.sh\\\": executable file not found in $PATH\": unknown"}` – Niewasz Biznes Mar 17 '20 at 07:28
  • I mean when you deploy to the web app, there is a startup file that you can set, it's a property of the web app. – Charles Xu Mar 17 '20 at 07:42
  • i mean i created a file init.sh with above code and i in Startup File i wrote init.sh, should i just write the code in that place? – Niewasz Biznes Mar 17 '20 at 08:55
  • beacuse that doesn't work and produce exacly the same problem as in the topic – Niewasz Biznes Mar 17 '20 at 08:57
  • Try the command only, not in the shell script. – Charles Xu Mar 17 '20 at 09:05
  • can you explain please? what do you mean? – Niewasz Biznes Mar 17 '20 at 09:36
  • I mean when you deploy the web app for container from the image, there is a startup command, just input the command `python manage.py makemigrations && python manage.py migrate --run-syncdb && python manage.py migrate && python manage.py runserver 0.0.0.0:8000`, not to execute the shell script which the command in. – Charles Xu Mar 17 '20 at 09:42
  • And when you run your application locally through the docker-compose, the image should be created, do you try to run the image again and does it work fine? – Charles Xu Mar 17 '20 at 09:44
  • image locally is working. I think also i am doing something wrong, beacuse i used this link https://docs.docker.com/compose/django/ created new project, uploaded it to azure and i have the same problem as in the topic. For some reason i think i am missing some azure file i should create or something like it. After 4-5 days of trying it all i have no more ideas. – Niewasz Biznes Mar 17 '20 at 13:27
  • How do you run the image with docker command? – Charles Xu Mar 18 '20 at 06:35
  • docker-compose up – Niewasz Biznes Mar 18 '20 at 07:41
  • Yes, I see the docker-compose file, but it does not use the image, it build the image from your code. I mean if you run the image with docker run command and does it work fine. – Charles Xu Mar 18 '20 at 07:53
  • Hey man, thanks for help. You make me thinking, so to make it work I needed add to DockerFIle CMD ["python", "/code/manage.py", "runserver", "0.0.0.0:8000"] – Niewasz Biznes Mar 19 '20 at 07:23
  • Well, then I will add an answer, can you accept it? – Charles Xu Mar 19 '20 at 07:33

1 Answers1

1

According to the information that you provided, you use the docker-compose to run your application and set the command run it only in the docker-compose file, not in the Dockerfile. So when you use the docker-compose command, it runs perfectly, but it failed when you just use the image.

The solution is that you need to add the command inside the Dockerfile like CMD ["python", "/code/manage.py", "runserver", "0.0.0.0:8000"].

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • Thanks for this answer! I had almost exactly the same problem that the op had, and I added this line to my Dockerfile then my page is showing! – Rui May 09 '22 at 04:26