Why Bother Use Docker?

Reyhan Alhafizal
3 min readApr 27, 2020

--

Software Container in the Ocean of Code | Source: https://www.valuebound.com/resources/blog/boost-your-drupal-development-docker

If you’re an IT guy/girl but not a DevOps one, you might be familiar with the word “Docker”. You must be heard people said it’s a container. But what is it, actually?

“What is Docker?”

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

“Why use Docker?”

By using containerization (in this case, using Docker), the developer (like us) can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code. It means that we can focus on writing code without worrying about the system that it will finally be running on. No more “it works on my machine, tho”. It also allows us to get a head start by using one of thousands of programs already designed to run in a Docker container as a part of their application.

“Docker sounds like VM to me.”

Container might be similar with virtual machines but those are actually different. Instead of abstracting the hardware, containers abstract the OS. Beside that, containers need much less space size than GBs of virtual machines.

“How to setup Docker?”

Docker provides pretty clear tutorial and guide to started with. You can read Official Docker Get Started or you can watch Learn Docker in 12 Minutes 🐳 on Youtube by Jake Wright. In this article, I will explain how to setup Docker with my team’s PPL project as example.

In our project, we develop Node.js app and contain it using Docker. To setup Docker, you only need to write some files and your app ready to be deployed.

  • First, create a Dockerfilefile in the root directory of the app. Our dockerfile looks like this:
FROM node:12                        # Base image
WORKDIR /usr/src/api # Working directory
COPY package*.json /usr/src/api/ # Copy depedencies
RUN npm i # Install depedencies
COPY . . # Bundle app
EXPOSE 9000 # Port number
CMD ["/bin/bash", "start.sh"] # Run some script to run
  • You might want to prevent unnecessary file to pushed on your image. You can make .dockerignore file and fill it with ignored files. Our dockerignore contains node_modules and some files.
node_modules
*.log
build
.env
  • After that, create docker-compose.yml to defining and running multi-container Docker applications. On our docker-compose file, we configure api (our app) and postgres service.
version: "2"        
services:
api:
restart: always
image: shawcken/agenkan-api:master
ports:
- "9000:9000"
depends_on:
- postgres
environment:
DATABASE_URL : postgres://agenkan@postgres/api
postgres:
image: postgres
restart: always
environment:
POSTGRES_USER : agenkan
POSTGRES_DB : api

version: Compose file format
service: What container to use
image: What image to use in the service
ports: What ports that image is bind to
depends_on: Dependency of the image
environment: Environment used based on the dependency

  • Next, build your docker image. api is our image name.
docker build -t <your username>/api .

Your image will now be listed by Docker:

docker images
  • Now, you can run your image. 9000:9000 is our port.
docker run -p 9000:9000 -d <your username>/api

That’s it for setup and run docker. Next, I might be explain it on my next article how to build and deploy it to Heroku using Gitlab CI. But in the mean time, you can read Dockerizing a Node.js web app to build your Node.js app in docker container.

Reference:

--

--