Friendly Introduction to Cloud Container — Docker
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?
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?
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.
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
- 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.
- 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.
- Docker Registries / Docker Hub, A server that serves docker repositories. We can pull other docker repositories or push ours on the docker registries.
- Docker Repository, basically a set of images that we or others have made.
- Dockerfile, A set of instruction in the form of a script used to create or modify images.
- Docker Compose, A feature to run different containers into a service of the same.
Docker tutorial 101 with Django
- 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
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
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]
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!
What are Linux containers?
Linux containers, in short, contain applications in a way that keep them isolated from the host system that they run…
What Exactly is Docker?
An introduction to containers and Dcker, including real-life examples and suggestions for complete software…
What is a Container? | Docker
A container is a standard unit of software that packages up code and all its dependencies so the application runs…