Slide 1

Slide 1 text

Dockerizing a Python Django Project Ngazetungue Muheue Ngazetungue muheuenga Python Week of Code 2019 25 August 2019 Windhoek, Namibia

Slide 2

Slide 2 text

All About Me!!! • Python programmer • Computer Science student • Django framework Community leader in Namibia • Co – founder of Python Namibia • Web Developer, Python and Django instructor • Organize of PyCon Namibia

Slide 3

Slide 3 text

Brief introduction of Docker

Slide 4

Slide 4 text

NAMIBIA What is Docker? • Docker is a platform that packages your application and all its dependencies together in the form of a docker container to ensure that your application works in any environment. • Its opensource • Designed to make it easier to create, deploy, and run applications by using containers. • Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Slide 5

Slide 5 text

NAMIBIA Docker Components 1. Docker Client and Daemon - is the primary way that many Docker users interact with Docker. 2. Images - is a read-only template with instructions for creating a Docker container. 3. Docker registries - stores Docker images, run Docker pull or docker run 4. Containers - is the execution environment for Docker. Ø Containers are created from images

Slide 6

Slide 6 text

HANDS ON PROJECT

Slide 7

Slide 7 text

NAMIBIA Get Started with Docker Installation We’ll start by installing the Docker desktop tools found https://docs.docker.com/install/ Important: Download the correct installer for your operating system and run the installation.

Slide 8

Slide 8 text

NAMIBIA Docker Commands Commands needed for our project: docker –version -This command is used to get the currently installed version of docker docker pull - Usage: docker pull : This command is used to pull images from the docker repository(hub.docker.com) docker run - Usage: docker run -it -d - This command is used to create a container from an image docker ps -This command is used to list the running containers docker ps –a -This command is used to show all the running and exited containers docker exec - Usage: docker exec -it bash - This command is used to access the running container docker stop - Usage: docker stop - This command stops a running container docker images -This command lists all the locally stored docker images docker rm - Usage: docker rm - This command is used to delete a stopped container docker rmi - Usage: docker rmi - This command is used to delete an image from local storage docker build - Usage: docker build -This command is used to build an image from a specified docker file Source edureka

Slide 9

Slide 9 text

Python Script Simple Hello file PyNam Machine code file

Slide 10

Slide 10 text

Python file and Dockerfile DockerFile Pynam2019.py Step 1: Create a folder in local directory In called “docker” in any location of your choice e.g C drive. Step 2: Create python script and Dockerfile Create a file ”pynam2019.py paste code below Cretate a file”Dockerfile”without extension and paste code below NB!!! Save all these files in the same folder

Slide 11

Slide 11 text

NAMIBIA DockerFile and pynammachine.py file DockerFile Pynammachine.py Step 1: Create a folder in local directory In called “docker” in any location of your choice e.g C drive. Step 2: Create python script and Dockerfile Create a file ”pynammachine.py” and paste code below Cretate a file”Dockerfile”without extension and paste code below NB!!! Save all these files in the same folder

Slide 12

Slide 12 text

Django Project

Slide 13

Slide 13 text

NAMIBIA Our Project components 1. Dockerfile, a Python dependencies file, • is a text document that contains all the commands a user could call on the command line to assemble an image 2. Docker-compose file (docker-compose.yml) • Describes the services that make your app. E.g in our case it is web server and database. • Describes which Docker images these services use, how they link together, any volumes they might need mounted inside the containers. • Finally, describes the ports these services expose. 3. Requirements file - for required software

Slide 14

Slide 14 text

NAMIBIA Create a requirements.txt

Slide 15

Slide 15 text

NAMIBIA Dockerfile engine

Slide 16

Slide 16 text

NAMIBIA Docker Compose 1. docker-compose up • Start all the container 2. docker-compose build • rebuild your image 3. docker-compose stop • Stop the container Describes the services that make your app. YML Config

Slide 17

Slide 17 text

NAMIBIA Build image and your Project Directory and files Run this to build your image with the Docker command line tool: sudo docker build -t pynamdjango .

Slide 18

Slide 18 text

NAMIBIA Create our Django Project Step 1: sudo docker-compose run web django-admin startproject DjangoProject . Step 2: Change the ownership of the new files. (Linux user) sudo chown -R $USER:$USER . NB.. Window user and MacOS You should already have ownership of all files, including those generated by django-admin

Slide 19

Slide 19 text

NAMIBIA Connect the Database Set up the database connection for Django. In your project directory, edit the DjangoProject/settings.py file. Replace the DATABASES = ... with the following:

Slide 20

Slide 20 text

NAMIBIA Run Docker compose

Slide 21

Slide 21 text

NAMIBIA Create our Django app Command Sudo docker-compose run web python manage.py startapp Ngazetungue_Website

Slide 22

Slide 22 text

NAMIBIA Django app running on your Docker host

Slide 23

Slide 23 text

NAMIBIA Creating Django app sudo docker-compose run web django-admin startapp Ngazetungue_Website

Slide 24

Slide 24 text

NAMIBIA Django app – urls, views, template

Slide 25

Slide 25 text

NAMIBIA Django app – Admin page

Slide 26

Slide 26 text

Codes Codes on github https://github.com/Ngazetungue/python_with_docker https://github.com/Ngazetungue/pynammachine_with_docker https://github.com/Ngazetungue/pynam_Dockerize_django_Project

Slide 27

Slide 27 text

NAMIBIA THANK YOU!!! Ngazetungue Muheue Ngazetungue muheuenga