Docker

Sarah
Pilar 2020
Published in
4 min readDec 2, 2020

When working on a project, aside from writing the code, we should also pay attention to the app distribution. Our app might contain lots of dependencies and it would be a hassle to install those dependencies again and again when moving to another system. This is where Docker comes in handy. Docker makes it easier to create and deploy our app with the help of Containers.

Just like the containers on a ship ready to be departed to another part of the world, your application is always ready to be shipped to another platform with Docker. If the containers contain stuff or goods, our Docker container contains our application, its tools, and dependencies so we don’t need to worry about installing the dependencies to the server.

Docker helps us solve the “it works on my machine problem” because Docker containers with the same Dockerfile will have the exact version of all tools available regardless of the system.

Docker is used for:

  • Fast, consistent delivery of your applications: allowing developers to work in standardized environments. An example is for CI/CD workflow.
  • Responsive deployment and scaling: docker’s container can run on a developer’s local laptop, physical/virtual machines, cloud providers, or a mixture of environments. A Docker container is different from a virtual machine. It runs on top of the OS which makes sure that our containers are lightweight as they don’t contain any guest OS.
  • Running more workloads on the same hardware: provides a less expensive alternative than virtual machines.

Docker Architecture

Source: https://docs.docker.com/
  1. Docker daemon: manage Docker objects such as images and containers.
  2. Docker client: a way that user interacts with Docker. If a user types docker run, the client sends these commands to dockerd.
  3. Docker registries: to store Docker images (Docker hub).
  4. Docker images: a read-only template with instructions for creating a Docker container. An image often depends on another image. To build your own image, use the Dockerfile.
  5. Docker container: a runnable instance of an image. An example command is docker run. If we don’t have the image locally, Docker will pull it from the registry. Containers guarantee our application runs the same way in any environment. We can use tools to containerize applications (orchestrators) such as Kubernetes or Docker swarm. These orchestrators help us manage, deploy, and scale our application. Also, orchestrators help replace containers automatically if they fail.

Installing Docker

I’m using MacOS so the installation steps are based on MacOS Catalina. The installation itself is quite simple, you just need to execute the brew cask install docker command on your terminal. If the installation is finished, now you have a Docker app on your Applications folder. Click the application and wait until docker runs. To test whether your docker is running or not, you can type this command docker run hello-world on your terminal.

Now, let’s get into the real thing!

How Do We Implement Docker?

First thing first, create a Dockerfile! This file will help us build docker image instantly. Some important Dockerfile keywords that we use are:

  • FROM: specifies the parent image. The Android SDK requires openjdk, so we’re using it as our parent image.
  • WORKDIR: is the working directory for any instructions on the Dockerfile.
  • ENV: sets the environment key to the value.
  • RUN: execute any command in a new layer on top of the image.

Here’s an example of our Dockerfile made by the previous dev team and modified by us:

Next, it’s time to build the docker image. The command is docker build . -t <username>/<repository>. Our username is tiangppl and our repository name is pilarmobile, so the command would be like this:

After we built the image, check if the image exists by executing command docker images.

Yay! We’re done building a docker image. It’s time to push our image to the repository. The command is docker push <username>/<repository>. Ours look like this:

Now here is our updated docker repository:

Last, use the latest docker image for the CI/CD pipeline. This is done by editing the gitlab-ci.yml file by adding image: tiangppl/pilarmobile:latest if you want to test your image on your local, you can use docker run <username>/<repository>

And, check if it works on the pipeline:

And that’s it, we’re finished!

Our application are divided into frontend web, frontend mobile, backend and postgreSQL database. Each of this component are deployed to their own server. Containers have helped us move each of the component to different servers as it guarantee our app to run anywhere.

References:

https://docs.docker.com/

https://docs.docker.com/get-started/overview/

https://blog.codemagic.io/how-to-dockerize-flutter-apps/

--

--