Friendly Introduction to Cloud Container — Docker

syams ramadan
6 min readJun 8, 2021
Source : https://dailysmarty-production.s3.amazonaws.com/uploads/post/img/529/docker-cheat-sheet.png

Docker for me personally is always placed on the gray side of what I know about software development. I know it has it’s roles on developing software — I know it’s got something to do with cloud — but I never know why and how we as software developers use it. All I know is that it offers some support for deployment by a pretty good margin.

Being the curious George that I am, I told myself that I really need to know about at least the basic understanding of Docker and its use. Moreover — I’ll probably need docker sometime near the future, so might as well be prepared right?

Well — after digging the internet about docker and personally experimented on Docker with Python myself, here’s what I found!

What is Docker?

Source : https://www.containex.com/-/m/picturepark/ctx/start/container-und-module/seecontainer/content/jumbotron-text/background-image/8d89dc32a8f0464.ashx?h=1886&la=de-flt&w=2829&rev=268409643&la=en

Okay, so first of all — What is docker? Docker is sort of a container used to store and package applications more or so its codes with the dependencies installed and configured to be ran whenever we want using its own set of isolated processes and resources.

Yeah, it sounded almost exactly the same as a virtual machine, what gives?

Source : https://d2o2utebsixu4k.cloudfront.net/media/images/ec45a4b1-b0fe-4bbf-b4f0-b6e2f0046784.jpg

When I first learn docker I too thought that docker was a virtual machine of some sort. But actually it’s pretty different, although a virtual machine uses an isolated processes and resources, they also have an OS of their own, in which we need to virtualize in order to make them behave as an independent machine.

With virtual machine if you want to run parallel applications separately you need to run multiple of them, resulting in many OS you have to virtualize, that is very costly if you ask me. With docker you can do that exactly with just one OS, how so? by only virtualizing the resources and processes needed to run the applications separately. You can read more about it here

Why exactly do we need docker?

The use of docker is to avoid applications suffering from dependency hell. Imagine this, you have application A which consist of A, B, and C features developed by you and two of your friends. Three of those features uses different dependencies, libraries, and different versions of it.

If you try to host all of the development you and you’re friends have made, it will definitely mess up your application as only one dependency configuration is actually installed on the host. That is obviously a scenario where you need to host multiple applications at the same time, each with their corresponding dependencies needed.

You can try running multiple virtual machines to do that — but again, as proven before, it’s very cost inefficient. Now that is where docker comes in to the rescue. In docker, applications with different dependencies are stored in what’s called a docker container. So in this case, three development cases (A, B, and C) would be separated into three different containers.

Source : https://www.docker.com/sites/default/files/d8/2018-11/docker-containerized-appliction-blue-border_2.png

hence, you have a cost efficient approach to hosting multiple development cases on different virtual machines — the difference is that this time, the only virtualizing you do is for the resources needed to run applications and not the full blown out OS.

Before we go into the docker-101 bit, here’s some docker terms you need to know

  1. Docker Image, An image is basically a template to build a docker container. It consist of applications code, dependency, and tools needed to run the application we want to run on the container.
  2. Docker Container, as to what have been explained before, it’s a standalone software containing all the things needed to run an application while running it separately and in isolation. A docker container needs an image to run, more or so that the image will become a docker container if run on a docker engine.
  3. Docker Registries / Docker Hub, A server that serves docker repositories. We can pull other docker repositories or push ours on the docker registries.
  4. Docker Repository, basically a set of images that we or others have made.
  5. Dockerfile, A set of instruction in the form of a script used to create or modify images.
  6. Docker Compose, A feature to run different containers into a service of the same.

Docker tutorial 101 with Django

  1. Initiating Django and Installing Docker

first of all, you need to have installed django on your end to follow this tutorial, if you haven’t — follow the official tutorial in here. You also need to download and install docker on your end (obviously), you can download it here.

if you need to know whether or not you have configured docker on your end correctly, just run the command “docker” on your CLI and see if it for yourself.

if you’ve configured your docker correctly the output when you type “docker” on your CLI should look something like this.

2. Creating Dockerfile

Assuming you already have a Django project, if you haven’t the tutorial here gave instruction as of how to initiate a Django project. Moving on, go to your django project, and create a file named Dockerfile. Dockerfile should contain this script

#Getting the python image from docker registries
FROM python:3.7
# Create an environment variable for the dependencies
ENV PYTHONUNBUFFERED 1
# Set working directory to your django app
WORKDIR /app
# Much like git add, adds the files in your django app
ADD ./app
# install dependencies of your app
RUN pip install -r requirements.txt
#Host a port for the docker
EXPOSE 8888
#runserver
CMD [ "python", "./manage.py", "runserver", "127.0.0.1:8000"]

Place it on the root folder of your Django project, should be with manage.py

You can modify your Dockerfile according to your needs, more about Dockerfile references in here

3. Create Docker Image

To create a docker image, simply go to CLI with your Django project path, and run this command

docker build -t [image name of your choice] -f Dockerfile .

It will take some time to run this command, but after that you can check whether or not the image is successfully created by typing on your CLI the command

docker images
the process of building an image
running docker images after the image is created

4. Run Your Docker Image

To run your docker image and create a docker container, run this on your CLI with your Django project path

docker run -it -p [Host port]:[Container port][your image name]
initiating docker container
my app run on docker

Now you should be able to see your Django application from your localhost server. and there you have it, Docker implemented on your Django project.

Docker might be your best choice to avoid having a fight with your colleagues and fellow developers when pushing an increment. Its way of isolating resources hence being free of environment limitations allows your application to be run anywhere. Docker can also solve your multiple deployment problems with a low cost. If I were you, I’d definitely use it!

Sources :

--

--