Yesterday while I was writing this post and documentation about how I build and how use my personalized docker django image, I had think (yes, I’m not a genius, and I’m slow to understood) that I can put all the commands sequence in a bash script and automatize the entire procedure.
from terminal inside the folder, I build the local image:
docker build -t django .
tag the local image:
docker tag django pierangelo1982/django
sign in to your docker account:
docker login
push the image on your Docker Hub account:
docker push pierangelo1982/django
P.S: pierangelo1982 is my docker account username, change it with yours.
STEP 2 — create and implement container
pull the image:
docker pull pierangelo1982/django
For work and implement our project, we have need to share the project folders of the container on our local host. For do that we have need to build a volume and copy the file.
create a volume:
docker volume create --name django-volume
connect the volume to the container for can copy the project folder
Create a container Django in docker is a bit more complicated than other framework, because and I don’t know the why, the official Django image is deprecated from your same creators, and they advise to use an approach based on the creation of a own Dockerfile and docker-compose.
So we start creating a new empty folder, inside this folder we create a file named Dockerfile
In Dockerfile insert this:
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
Now we go to the same folder to create a file called requirements.txt, and in this file we are going to include all the Django packages and libraries which we need in our project (it is the same thing of the command pip install).
Now ever in the same folder we go to create a file named docker-compose.yml, and we going the elements that we want run in docker, so our Django instance, a Mysql Database and a phpmyadmin.