Docker this Docker that. I finally know what docker is.
As a software engineer you might have heard Docker. but really, what is it? this article will talk about the basic of Docker that might help you to start using docker.
Docker In a Nutshell (my perspective)
Docker is a platform based on container. It’s like a virtual machine that help contain your application so that it would run in any machine/OS. Unlike virtual machine it don’t reserve any hardware resource and only use what the application need
Docker In The Bigger Shell
Docker is an open source tool that everyone can use to develop software. It is used mainly for deployment as it can run the software. Docker make it possible for you to separate your app from the infrastructure so you can manage your application like you manage your infrastructure, and its definitely faster. Docker isolate your program without using so much resource as it only use hardware resource as much the program need unlike virtual machine which need to allocate some of the hardware resource for it, make it wasteful when the app don’t use the full capability of the virtual machine.
Docker is extremely useful for software engineer as it makes deployment easier especially/even for CI/CD. Docker provides the ability to package and run an application in a loosely isolated environment called a container. This Isolation makes it secure for you to run as much application as you want without hindering the other application environment.
System Agnostic
Docker container is platform agnostic. if it runs in X device/platform it will run in Y device/platform. It helps developer who works in a team and collaborate to develop their application. Not only that it make sure that you only need to configure for one environment. unlike if you dont use container you might need to configure the environment so it would work in your developing and production environment.
No Wasted Resource
One of problem Docker tackled the most is wasted resource. in microservice if you run each app in their own virtual machine. it would waste the allocated resource when the app arent used or just dont use the full capability (shared resource). But with docker which share on operating system. it only resource as much as the application need.
My Discovery (not really)
Virtual Environment, Virtual Machine, Container.
Virtual Machine vs Container : mainly different in resource used
Virtual Environment vs Container : encapsulate python dependency while container encapsulate the whole os.
Docker Orchestration
The portability and reproducibility of a containerized process mean we have an opportunity to move and scale our containerized applications across clouds and datacenters. Containers effectively guarantee that those applications run the same way anywhere, allowing us to quickly and easily take advantage of all these environments. Furthermore, as we scale our applications up, we’ll want some tooling to help automate the maintenance of those applications, able to replace failed containers automatically, and manage the rollout of updates and reconfigurations of those containers during their lifecycle.
Tools to manage, scale, and maintain containerized applications are called orchestrators, and the most common examples of these are Kubernetes and Docker Swarm. Development environment deployments of both of these orchestrators are provided by Docker Desktop, which we’ll use throughout this guide to create our first orchestrated, containerized application.
Docker In Practice
Docker File
is a container file that configure how would the Docker Image built. In Docker file you define the specific of the docker image and also what need to be done. As Example in my project the docker file would look like this :
FROM python:3.8-alpine# set work directory
WORKDIR /app# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0# install psycopg2
RUN apk update \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev \
&& pip install psycopg2#installing Pillow
RUN apk add --no-cache jpeg-dev zlib-dev
RUN apk add --no-cache --virtual .build-deps build-base linux-headers \
&& pip install Pillow
# install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt# deleting uneeded dependency
RUN apk del build-deps# copy project
COPY . /app
RUN python manage.py makemigrations accounts
RUN python manage.py makemigrations content
RUN python manage.py migrate
RUN python manage.py collectstatic --noinput
RUN echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin@myproject.com', 'password')" | python manage.py shell
#Create django super userRUN adduser -D myuser# run gunicorn
CMD gunicorn RaisingStar.wsgi:application --bind 0.0.0.0:$PORT
the first line is like the framework i chose for this container later. As i create a django app i chose to use python:3.8-alpine. I would not go into the detail, but this dockerfile define how my program will run (and all the prerequisite).
Docker Image
Now that we have a dockerfile we can build a docker image. docker image is a file like object that are ready to run. which when ran would make a docker container. to build the dockerfile you use below command:
docker build
for the complete command read : https://docs.docker.com/engine/reference/commandline/build/
it will look something just like this after you run the command :
$ docker build --cache-from $HEROKU_STAGING_REGISTRY_IMAGE --tag $HEROKU_STAGING_REGISTRY_IMAGE --file ./Dockerfile "."Step 1/19 : FROM python:3.8-alpine3.8-alpine: Pulling from library/python540db60ca938: Already existsa7ad1a75a999: Already exists5472576d6ead: Already exists107b9b7cb26b: Already exists9ff9609b1678: Already existsDigest: sha256:4947984a40959b8dfa03512b3518d0224d4f655500af2fe626bac3d77d64e8a3Status: Downloaded newer image for python:3.8-alpine---> 39cc898b3c31...
...
...Step 19/19 : CMD gunicorn RaisingStar.wsgi:application --bind 0.0.0.0:$PORT---> Running in b7e653de1f78Removing intermediate container b7e653de1f78---> 5459c510bac9Successfully built 5459c510bac9Successfully tagged registry.heroku.com/dev-raising-star/web:latest
Docker Contiainer
Docker container is a running instance of docker image. So one dockerfile build one dockerimage. and one dockerimage can run several docker container instance. to run this you will need to type below command :
docker run
for the complete command read : https://docs.docker.com/engine/reference/run/
TL;DR
Docker is a virtualmachine like tool that is platform agnostic and resource efficient. one dockerfile build one dockerimage. and one dockerimage can run several docker container instance.