This blog is to explain Django Docker installation. Docker is a software platform to create, deploy and manage virtualized application containers on a common operating system, with an ecosystem of allied tools.
First follow these steps to install Docker in Ubuntu.
Next, install Docker Compose by running these
Next we are going to install Django in Docker.
Create a directory named docker_first somewhere in your home directory.
Create a file named Dockerfile in it. Add the following lines to it and save.
Create a file named requirements.txt in the same location. Add the following lines into it.
Create another file named docker-compose.yml
Add the following code into it
Next, create a Django project by run this
Run this command to change the file permissions
Edit the composeexample/settings.py file. Copy following lines in place of DATABASES
Add 'localhost' in ALLOWED_HOSTS in the same settings.py. It will look like this
Run the following command in the docker_first directory
Now open http://localhost:8000 to check the installation. Thank you
Watch the video tutorial here (Click the gear icon and select video quality to 1080p HD, then click the full screen button for good quality)
Go to this link to subscribe to my YouTube channel to stay updated:- https://www.youtube.com/channel/UCxTp2yDew9m48Ch_hArOoYw
First follow these steps to install Docker in Ubuntu.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
Next, install Docker Compose by running these
sudo curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Next we are going to install Django in Docker.
Create a directory named docker_first somewhere in your home directory.
Create a file named Dockerfile in it. Add the following lines to it and save.
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
Create a file named requirements.txt in the same location. Add the following lines into it.
Django>=2.0,<3.0
psycopg2>=2.7,<3.0
psycopg2>=2.7,<3.0
Create another file named docker-compose.yml
Add the following code into it
version: '3'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Next, create a Django project by run this
sudo docker-compose run web django-admin startproject composeexample .
Run this command to change the file permissions
sudo chown -R $USER:$USER .
Edit the composeexample/settings.py file. Copy following lines in place of DATABASES
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
Add 'localhost' in ALLOWED_HOSTS in the same settings.py. It will look like this
ALLOWED_HOSTS = ['localhost']
Run the following command in the docker_first directory
sudo docker-compose up
Now open http://localhost:8000 to check the installation. Thank you
Watch the video tutorial here (Click the gear icon and select video quality to 1080p HD, then click the full screen button for good quality)
Go to this link to subscribe to my YouTube channel to stay updated:- https://www.youtube.com/channel/UCxTp2yDew9m48Ch_hArOoYw