Language and Framework Guides
Run a Django Container App
16 min
back4app containers is a powerful platform for hosting django applications with its ability to automatically deploy dockerized django apps, you can launch your project in a scalable and flexible environment with ease in this guide, we will walk you through the process of preparing and deploying your django application on back4app containers in this comprehensive guide, we will walk you through the process of preparing and deploying your django application on back4app containers, covering everything from simple projects to more complex setups if you have any questions or comments, feel free to join the conversation in the #containers channel on the back4app community on slack for discussions specific to django on back4app containers anytime you can also contact us at community\@back4app com at anytime if you want to check a sample working django project on back4app containers go to https //github com/templates back4app/containers python flask sample 1 prepare your django application ensure that your django application is structured properly, and all necessary files, including templates and static files, are organized in the project directory for more complex projects, make sure you have a proper app factory setup and follow django best practices 2 create a requirements txt file list all required dependencies and their versions in a requirements txt file this file will be used by the dockerfile to install the necessary packages when building the docker image here's a sample requirements txt file for a django application, including some common dependencies you might use in various projects feel free to add or remove packages as needed for your specific application requirements txt django==3 2 10 gunicorn==20 1 0 psycopg2 binary==2 9 1 djangorestframework==3 12 4 django cors headers==3 9 0 django environ==0 4 5 django extensions==3 1 3 django filter==21 1 django rest auth==0 9 5 django allauth==0 47 0 requests==2 27 1 3 create a dockerfile write a dockerfile to define your application's docker image make sure to use a suitable base image (such as python 3 x slim), install dependencies from the requirements txt file, copy your project files, expose the correct port, and specify the appropriate command to start your django application (e g , using gunicorn or uwsgi) example 1 basic django application with gunicorn this dockerfile is for a simple django application using gunicorn as the wsgi server it installs the necessary packages from the requirements txt file, copies the project files, and exposes port 8000 for the gunicorn server the cmd specifies the command to start the gunicorn server, binding to 0 0 0 0 8000 \# base image from python 3 9 slim \# working directory workdir /app \# copy requirements file and install dependencies copy requirements txt requirements txt run pip install no cache dir r requirements txt \# copy the rest of the project files copy \# expose the server port expose 8000 \# command to start the server cmd \["gunicorn", " bind", "0 0 0 0 8000", "myproject wsgi"] example 2 django application with uwsgi and nginx this dockerfile is for a more complex django application that uses uwsgi as the wsgi server and nginx as the reverse proxy server it installs build dependencies for uwsgi and nginx, and then installs those packages along with uwsgi using pip it then copies the project files and requirements txt file, and exposes port 8000 for the uwsgi server the cmd specifies the command to start uwsgi, using a separate configuration file called "myproject uwsgi ini" the dockerfile also configures nginx by removing the default configuration file, copying a custom nginx configuration file called "myproject nginx conf" to the appropriate directory, and creating a symlink to enable the configuration the nginx configuration file specifies that nginx should listen on port 8000 and proxy requests to the uwsgi server running on the same docker container \# base image from python 3 9 slim \# install build dependencies and curl run apt get update && apt get install y no install recommends \\ build essential \\ libssl dev \\ libpcre3 dev \\ curl \\ && rm rf /var/lib/apt/lists/ \# install uwsgi and nginx run pip install uwsgi run apt get update && apt get install y no install recommends nginx \# configure nginx run rm /etc/nginx/sites enabled/default copy myproject nginx conf /etc/nginx/sites available/ run ln s /etc/nginx/sites available/myproject nginx conf /etc/nginx/sites enabled/ \# copy requirements file and install dependencies copy requirements txt requirements txt run pip install no cache dir r requirements txt \# copy the rest of the project files copy \# expose the server port expose 8000 \# start uwsgi cmd \["uwsgi", " ini", "/app/myproject uwsgi ini"] make sure to replace "myproject" with your actual project name in the dockerfiles also, for the uwsgi and nginx setup, you need to create a "myproject nginx conf" file and a "myproject uwsgi ini" file in your project directory, which contain the appropriate configuration for nginx and uwsgi 4 test your application locally after creating your dockerfile, you can test your django application locally before deploying it to back4app containers this is an important step to ensure that everything is working as expected and to avoid any issues or errors during deployment by testing your application locally, you can catch any issues or errors before deploying to back4app containers if you encounter any issues, be sure to address them before moving on to the next step docker build t your app name docker run p 8080 8080 your app name 5 push your project to a git repository create a gitignore file to exclude unnecessary or sensitive files from your repository (e g , pycache , env , pyc , etc ) initialize a git repository, commit your project files, and push them to a remote repository (e g , on github) 6 deploy your application on back4app containers after creating your back4app account you can follow the steps listed on the docs 1 connect you github repo with back4app 2 prepare your project for deployment in summary containers will follow the instructions detailed on your dockerfile and start to create your app 7 monitor deployment keep an eye on the deployment logs and status on the back4app containers dashboard address any errors or issues that arise during deployment in case of more complex projects, ensure that all necessary services (such as databases or external apis) are correctly configured and accessible notice that you also have a section called logs which lists all the running logs for your app and its also important to keep an eye on it to check if you have any problem with your app 8 troubleshooting common deployments errors are listed here other possible common errors when deploying flask applications are incorrect host configuration django applications should be configured to run on host 0 0 0 0 when deployed on back4app containers if your application is using localhost or 127 0 0 1 , it might not be accessible externally make sure your django settings file ( settings py ) contains the following line allowed hosts = \[' '] if your application is still not accessible, check your dockerfile to make sure the correct port is exposed (e g , expose 8000 for port 8000) incompatible or missing dependencies ensure that all required dependencies are listed in your requirements txt file and that their versions are compatible with each other and your application code missing or incompatible dependencies can lead to runtime errors make sure all the required dependencies are installed in your docker container by running the docker run command with the it flags and opening a shell inside the container docker run it myproject container /bin/bash once inside the container, check that all the required dependencies are installed using pip list if a dependency is missing, install it using pip install \<dependency> database connection issues if your django application relies on a database, ensure that the connection string and credentials are correctly set up in the application configuration also, verify that the database is reachable from the back4app containers environment check your django settings file ( settings py ) to make sure the database configuration is correct you may need to specify the database host, port, name, user, and password depending on your configuration make sure your docker container has access to the database by verifying that the database is reachable from inside the container you can do this by opening a shell inside the container ( docker run it myproject container /bin/bash ) and using ping or telnet to connect to the database application crashes or unhandled exceptions unhandled exceptions or crashes in your django application code can cause deployment failures or unexpected behavior examine your application logs for any error messages, and address any issues in your code check the container logs by running docker logs myproject container to see if there are any errors or exceptions being thrown use a tool like sentry to track and monitor errors in your application incorrectly configured wsgi server make sure that your wsgi server (e g , gunicorn or uwsgi) is correctly configured in your dockerfile and that it starts your django application using the right entry point (e g , myproject wsgi\ application ) verify that the correct server is installed and listed as a dependency in your requirements txt file invalid environment variables or configuration check if your application relies on specific environment variables or configuration files, and ensure they are correctly set up in the back4app containers environment set any necessary environment variables in your dockerfile using the env command verify that any configuration files required by your application are correctly mounted as volumes in the docker container if you encounter any other issues while deploying your django application on back4app containers, contact us at community\@back4app com 9 scale your application for more complex projects that require additional resources or horizontal/vertical scaling, consider upgrading your back4app containers plan to handle increased traffic and load 10 sample application for a sample django application project you can go to https //github com/templates back4app/containers python django https //github com/templates back4app/containers python django