Deploying Django App With Docker & PostgreSQL To Digital Ocean

Gerry Sabar
3 min readJul 25, 2019

--

From my previous article here, now we can try to deploy it to Digital Ocean VPS Droplet. Makesure you have a valid digital ocean account & has created a droplet instance before we begin. I assume you have a valid Digital Ocean account and able to do ssh to your VPS Droplet instance. You also can clone my working repository here.

Previously inside djangoapp folder we have a file named docker-compose.yml. This file is used to adjust your docker configuration. However we may have two environments for now. Let’s say one is development for development purpose and another one is production to be deployed in Digital Ocean server & everyone can access it.

First let’s remove docker-compose.yml inside djangoapp and then inside your root project director let’s create a file named docker-compose-common.yml with code as follow:

This is our main docker-compose, next let’s create docker-compose-local-dev.yml for us to work in development mode:

Last, let’s add docker-compose-prod.yml for production server:

Let’s try running docker in our local development mode with this command:

$ docker-compose -f docker-compose-common.yml -f docker-compose-local-dev.yml up --build

and last please add localhost to your allowed host in your django settings.py file. You can access localhost:5000 in your own web browser then you can see your django app working as usual.

Alright, great, now we’re going to deploy to Digital Ocean server. Please do ssh to your Digital Ocean instance, for example my ssh to my own Digital Ocean is as follow:

$ ssh root@174.138.25.136

After you do ssh login you can try to clone repository from my repository here for already made code or you can try building repo on your own. Then you go to docker-python directory after finishing the clone (if you’re using my repository with default folder name) and execute this command:

$ docker-compose -f docker-compose-common.yml -f docker-compose-prod.yml up --build

After the process is done you can access your server for example my server is 174.138.25.136 . If you get allowed host error, please adjust your allowed host by adding your server IP address in allowed ALLOWED_HOSTS at your django settings.py .

Maybe you also wondering how to createsuperuser in server when you access /admin in your server. To solve this problem we can type this command in your server terminal:

$ docker ps

Take a note for your docker container id and stop your docker service for that container id then do this command:

$ docker exec -it <container name> /bin/bash

For example if your container id is abcd1234 then you do this command:

$ docker exec -it abcd1234/bin/bash

then inside docker terminal you can type this command to create admin user

$ python3 manage.py createsuperuser

Great, now we have a fully deployed django app with Docker and PostgreSQL in Digital Ocean!

--

--

Responses (1)