Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dockerizing a Python Django project

Dockerizing a Python Django project

This is a slide for my talk/work at Python week of code 2019 in Namibia

Ngazetungue Muheue

August 25, 2019
Tweet

More Decks by Ngazetungue Muheue

Other Decks in Programming

Transcript

  1. 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
  2. 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.
  3. 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
  4. 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.
  5. 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 <image name> : This command is used to pull images from the docker repository(hub.docker.com) docker run - Usage: docker run -it -d <image name> - 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 <container id> bash - This command is used to access the running container docker stop - Usage: docker stop <container id> - This command stops a running container docker images -This command lists all the locally stored docker images docker rm - Usage: docker rm <container id> - This command is used to delete a stopped container docker rmi - Usage: docker rmi <image-id> - This command is used to delete an image from local storage docker build - Usage: docker build <path to docker file> -This command is used to build an image from a specified docker file Source edureka
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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 .
  11. 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
  12. 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:
  13. NAMIBIA Create our Django app Command Sudo docker-compose run web

    python manage.py startapp Ngazetungue_Website