In this guide I show you how to run your Django app in one Docker container and your MySQL database in another and make them talk to each other. We are going to manage and run these two containers using Docker Compose.
I am assuming you already have a Django project locally that is using a local database and that your project has a requirements.txt file. You should also install Docker on your machine if you haven’t already.
Steps for Installing Docker:
1. Open the terminal on Ubuntu.
2. Remove any Docker files that are running in the system, using the following command:
$ sudo apt-get remove docker docker-engine docker.io
After entering the above command, you will need to enter the password of the root and press enter.
3. Check if the system is up-to-date using the following command:
$ sudo apt-get update
4. Install Docker using the following command:
$ sudo apt install docker.io
You’ll then get a prompt asking you to choose between y/n – choose y
5. Install all the dependency packages using the following command:
$ sudo snap install docker
6. Before testing Docker, check the version installed using the following command:
$ docker –version
About docker and Docker Compose:-
what is docker:-
docker is tool which is help full for making containers
docker is advance version of virtualization
Docker is a software platform that allows you to build, test, and deploy applications quickly.
Docker is written in the Go programming language
What is Docker Compose?
Docker Compose is a tool that assists in defining and sharing multi-container applications. By using Compose, we can define the services in a YAML file, as well as spin them up and tear them down with one single command.
Let’s dive right in:
Below is the project structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
└── django_todo # < project root package ├── todo # < todo app │ ├── admin.py │ ├── apps.py │ ├── migrations │ ├── models.py │ ├── serializers.py │ ├── urls.py │ └── views.py ├── manage.py ├── requirements.txt dockerfile dockercomposefile └── django_todo ├── settings.py # Django settings file ├── urls.py └── wsgi.py |
The first step is to create a Dockerfile in the root of your project (this is where you have your requirements.txt and
Inside docker file
1 2 3 4 5 6 |
FROM python :3.8 ENV PYTHONUNBUFFERED 1 WORKDIR /app COPY requirements.txt /app/requirements.txt RUN pip install -r requirements.txt COPY . /app |
Let’s go through that line by line:
1. FROM python:3.8 is the base image your Docker image will be built upon. This will be pulled from DockerHub.
2. ENV PYTHONUNBUFFERED 1 ensures that the output Django writes to the terminal comes out in real time without being buffered somewhere. This makes your Docker logs useful and complete.
3. WORKDIR /app creates a new folder in your container called app which will be your project’s root inside the container. It then sets that as the work directory in which the subsequent commands will be executed in.
4. COPY requirements.txt /app/requirements.txt copies your local requirements.txt file to the Docker container.
5. RUN pip install -r requirements.txt will make sure you have all your project dependencies installed inside the container.
6. COPY . /app and finally it’s time to copy your project’s content into the Docker container.
Now create a new file in your local project root. This one is called docker-compose.yml and is used for configuring Docker Compose.
Inside docker compose file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
version: '3' services: db: image: mysql:8 ports: - "3306:3306" environment: - MYSQL_DATABASE='mydatabase' - MYSQL_USER='root' - MYSQL_PASSWORD='some_password' - MYSQL_ROOT_PASSWORD='some_password' - MYSQL_HOST='' volumes: - /tmp/app/mysqld:/var/run/mysqld - . /db:/var/lib/mysql web: build: . command: python manage.py runserver 0.0.0.0:8000 ports: - "8000:8000" volumes: - .:/app - /tmp/app/mysqld:/run/mysqld depends_on: - db |
This defines two services: db and web.
Db uses an official MySQL image from DockerHub. We attach local port 3306 to port 3306 of this container. We then set a bunch of environment variables that are required for MySQL. Make sure you also set these in your Django database settings.
Volume define two volume mappings:
1. The first one maps the contents of var/run/mysqld of the container to a local folder on your machine. This file contains socket information that enables the web service (your Django app) to talk to the database service.
2. ./db:/var/lib/mysql creates a local db folder in your project root so that database information can be saved if you destroy the db service.
Web build its image using the Dockerfile we created earlier. It then runs the Django server and exposes port 8000 to your machine. We map your local project folder to the app folder and we bring in the socket file. We tell Docker Compose that this service is dependent on the db service.
Now all we have to do is to start everything up. In your project root run:
command:- $ docker-compose up
This should build your images and start the containers. Open a browser and go to 0.0.0.0:8000. You should see your app. Now if you open a new terminal and run a docker ps you will see the web and db containers are running:
command:- docker ps
If you want to stop Docker Compose, press Ctrl+C in the first terminal or run docker-compose stop in the project root folder in another terminal.
To run manage.py commands in the Django container (web):
command: – $ docker-compose run – -rm web python manage.py migrate
Now Run your project on map port inside docker-compose like: -8000
bluethinkinc_blog
2022-11-04