Make sure you have downloaded these prerequisites before we start
-
Clone the repository to your local machine
git clone https://github.com/jodeh/Dockerized-Django-Nginx-MYSQL.git cd Dockerized-Django-Nginx-MYSQL
-
Before we start to run the containers, we need to make a network to let the containers to connect with each other
docker network create --subnet 10.0.0.0/24 custom-network
-
Now lets run our mysql container :
docker run -d --network custom-network --ip 10.0.0.15 --hostname mysql-database -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=djangodb mysql
-
Build the Django image and run the container
cd /Django docker build -t django-app . docker run -d --network custom-network --ip 10.0.0.10 --hostname django django-app
-
Build the nginx image and run it
cd /nginx docker build -t nginx-reverse-proxy . docker run -d --network custom-network --ip 10.0.0.5 -p 80:80 --hostname nginx nginx-reverse-proxy
-
Create user in mysql .
docker exec -it <mysql container id> bash $ mysql -p > CREATE USER 'jodeh'@'%' IDENTIFIED BY 'password'; > GRANT ALL PRIVILEGES ON *.* TO 'jodeh'@'%' WITH GRANT OPTION; > FLUSH PRIVILEGES;
-
Migrate tables to the new database
docker exec -it <django container id> bash cd ~/mysite python3 manage.py migrate
-
Now go back to mysql container to check tables
docker exec -it <mysql container id> bash $ mysql -p > SHOW DATABASES; > USE djangodb; > SHOW TABLES;
Tables must be shown like this.
-
Finally Check The Connectivity
- We can configure django settings by editting ./django/mysite/settings.py file
- The proxy pass and the server name can be configured by editting ./etc/nginx/conf.d/default.conf file