Ease Your Life with Docker

Ryo Axtonlie
6 min readMay 1, 2021

This article is written as an Individual Review Assignment for PPL CSUI 2021

Have you ever heard anything about Docker? No? It’s okay, here in this article I will talk about Docker. Oh yeah, in this article, a few example will be given using implementation in my PPL CSUI 2021 Course Project.

So What is Docker?

Docker is a software platform based on container that can be used to build and run application [1]. It enables developer to be able to easily wrap, ship, and then running the application just like an item delivery [2]. By utilizing Docker, you can easily put everything needed to develop or run the application like the libraries, dependencies and the other things in one package.

Imagine that one time you broke your laptop containing all of your work, the work is in tight schedule and require you to finish it ASAP. Of course you need to buy a new laptop, but the problem starts here. Do you remember what is every dependencies and libraries used on your project? Reinstalling everything is not going to help you quick and this is where the Docker comes to the rescue. Since everything needed for application is already packed inside the container, you can just run the container and voila! Have fun completing your work.

The advantage that Docker bring is really good, you don’t have to think about the other person environment and even the deployment server. You just need to pack the project in Docker, then do the job no need to be afraid facing a problem just because of different version of software that you were using.

Run Your Container

In this section, I will talk a little about Docker Image. Docker image is a template containing instructions to create container. A Docker Image is a collection of files bundled together containing essential thing like installations, application code, and dependencies.

Figure 1.1 — Run a container

In order to run a container, you need to have a docker image first. But don’t worry, if you don’t have one you just need to simply type the “docker run [image-name]” and then it will automatically download a docker image if you don’t have one and after downloading one the container will be run.

Then you can check if the container is already available or not yet. You can check the container availability from your Docker Desktop application or “docker container ls -a” command in you terminal.

Figure 1.2 — Checking available container from terminal
Figure 1.3 — Checking available container from Docker Desktop app

Creating Custom Image with Dockerfile

Sometimes, just pulling an image isn’t going to be enough. It is possible that you needed something that doesn’t available in the image yet, and here is the solution. You can basically use a Dockerfile to customize you Docker Image.

# pull official base image (our project is using Django framwork, so we need python image)
FROM python:3.8.3-alpine
# set work directory (changing the work directory to /usr/src
WORKDIR /usr/src
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies (since we are using postgresql as database we need to install the postgresql on our container)
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev libffi-dev bash
# install Pillow dependencies (our project contain image file such as .jpg format, this is to install the dependencies)
RUN apk add jpeg-dev zlib-dev libjpeg
# install dependencies (Our django project used a lot of dependencies listed on requirements.txt, so here we are installing those dependencies with pip)
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy project to container
COPY . .

And that’s the way to customize you Docker Image.

Troubled Running Multiple Docker Container?

Let’s say that your project need to use a lot of Docker Image, which mean you need to run a lot of Docker Container. It must be painful to run it one by one for sure. In order to ease yourself, you can use a Docker Compose! Docker Compose is a tool to define and run multiple container in Docker application. One example here, you can create a docker-compose.yml to create a Docker Compose file. Below here is an example for my group project Docker Compose in dev environment.

version: '3.7'services:
db:
image: postgres:13-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_DB=[your-database]
- POSTGRES_USER=[your-username]
- POSTGRES_PASSWORD=[your-password]
web:
build:
context: .
dockerfile: Dockerfile
command: bash devops/deployment.sh
volumes:
- ./:/usr/src/
ports:
- "8000:8000"
env_file:
- ./.env.dev
depends_on:
- db
volumes:
postgres_data:

A little explanation about this Docker Compose file. Since we need to use both Postgresql and Django in our application, we need two Docker image containing both of them. The container will be defined inside the service.

The first one is the db service, where we use the postgres image and storing the data with the given volume. Then, we set the environment variable to access the Postgres with our given database name, user, and password.

The second one is the web service, where we define the container need for the Django. To use the previously modified docker image in Dockerfile, we defined it in build stage, storing it in dockerfile parameter. It will load the customized image we already made. Next, the command stage, here we are running the deployment.sh script to migrate our database automatically based on script. Then we store the data on “./:/usr/src/” path. The next one is the ports where we forward the port 8000 (where the django run on our localhost) from our host to port 8000 (where the forwared port 8000 from our host can be accessed) in the web service container. Unlike the db service, this time to the environment variable were stored on “.env.dev” file, but it serve the same functionality like the db service, except this one stored inside a file. Finally, this service is depends on the db service, since without the db service we can’t store the data in database.

And then you can run the following script to Run or Stop the whole project container from docker-compose file.

# To run the docker-compose
docker-compose up -d --build
# Run specific docker-compose file if you have more than one docker-compose yml files.
docker-compose -f [name.name.yml] up -d --build
# To stop the docker-compose
docker-compose down -v
# Run specific docker-compose file if you have more than one docker-compose yml files.
docker-compose -f [name.name.yml] down -v
Figure 1.4 — Example of Running the docker-compose
Figure 1.5 — Example when the docker-compose stopped

My thought on Docker?

In my opinion, Docker is a really helpful tool. It made the development become easier, especially when you and your teammate don’t use a same environment. With docker, you don’t have to worry to install lot of dependencies, you just need to run the container and done. In fact, I do really recommend to use Docker, your life will become easier for real.

That’s all about Docker in this article. I hope that this article will be useful. Have a nice day! :D

Reference :

  1. What is Docker? | Opensource.com
  2. What is Docker and why is it so darn popular? | ZDNet
  3. Ein Beginner Guide zum Verstehen und Erstellen von Docker-Images (jfrog.com)
  4. Overview of Docker Compose | Docker Documentation

--

--

Ryo Axtonlie

Just an ordinary Computer Science Student at University Of Indonesia