azure-docs icon indicating copy to clipboard operation
azure-docs copied to clipboard

[Update Request] Run 'python manage.py migrate' in Dockerfile

Open jyama4ta opened this issue 2 years ago • 3 comments

We get following warning when we try to run the custom docker image locally.

Starting SSH ...
[ ok ] Starting OpenBSD Secure Shell server: sshd.
Performing system checks...

System check identified no issues (0 silenced).

You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): auth.
Run 'python manage.py migrate' to apply them.

July 05, 2022 - 10:29:29
Django version 1.11.29, using settings 'djangoapp.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

It says to run 'python manage.py migrate'. I find that the message will be fixed if we update the Dockerfile as below. Could you please take a look the sample project?

FROM tiangolo/uwsgi-nginx-flask:python3.6
 
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt --no-cache-dir
ADD . /code/
 
# ssh
ENV SSH_PASSWD "root:Docker!"
RUN apt-get update \
        && apt-get install -y --no-install-recommends dialog \
        && apt-get update \
        && apt-get install -y --no-install-recommends openssh-server \
        && echo "$SSH_PASSWD" | chpasswd
 
COPY sshd_config /etc/ssh/
COPY init.sh /usr/local/bin/
 
RUN chmod u+x /usr/local/bin/init.sh
RUN python manage.py migrate
EXPOSE 8000 2222
#CMD ["python", "/code/manage.py", "runserver", "0.0.0.0:8000"]
ENTRYPOINT ["init.sh"]

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

jyama4ta avatar Jul 05 '22 10:07 jyama4ta

@jyama4ta Thanks for your feedback! We will investigate and update as appropriate.

SaurabhSharma-MSFT avatar Jul 06 '22 00:07 SaurabhSharma-MSFT

@charris-msft can you take a look?

btardif avatar Oct 12 '22 21:10 btardif

This is a great idea and if effective and reliable will save quite a bit of effort - especially for apps that are running in VNETs, which can be hard to connect to.

I recently had a conversation about migrations with some of the other Python folks. In this sample repo you can see a call to db.create_all(): https://github.com/Azure-Samples/msdocs-flask-postgresql-sample-app/blob/main/app.py#:~:text=%23%20Create%20databases%2C%20if,.commit()

That is how we're handling initial db migrations for Flask, but Django doesn't have the same feature, so those migrations still need to be run manually.

The suggestion in this issue to call RUN python manage.py migrate in the Dockerfile is an interesting one that I haven't tried. While it could work for both Django and Flask in container-based apps, we should test the behavior for a container-based Flask app that also has the db.create_all() call in the app.py file. It will likely work without issue, but should be verified.

charris-msft avatar Oct 17 '22 15:10 charris-msft

In practice I would recommend against automatically running database migration, and this case each time the image is rebuilt. Either run python manage.py migrate locally, or in the case of secured app/database, you can also run it in the SSH session.

Take a look at the bring-your-own-code scenario we now have here that demonstrates running migration using SSH: https://learn.microsoft.com/en-us/azure/app-service/tutorial-python-postgresql-app?tabs=flask%2Cwindows.

For any other issues specific to your scenario, try MICROSOFT Q&A or Stackoverflow.

#please-close

cephalin avatar Jan 26 '23 14:01 cephalin