Basic of Docker

Jonathan Chandra
5 min readMay 3, 2021
Photo by Todd Cravens on Unsplash

This article is made for the Software Project course assignment, Faculty of Computer Science, Universitas Indonesia

In software development, sometimes we encounter a moment where an application can run successfully on your local computer but in other computers, it’s a different story, it failed to run the application. This happen because the application runs in the different computer environment and this problem is so troublesome. So how do we solve the problem? The answer is to use docker.

What is Docker?

Cute logo of docker

Docker is a tool that is used to build, run, and deploy applications by wrapping it into a containers. By using docker, we can wrap an application along with its library and dependencies to become a container. With this container, the application can be run on any other environment without worrying its different setting.

How does docker work?

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

Docker uses a client-server architecture. Docker daemon is the one who build, run, and distribute docker container. Docker client can be your terminal or docker desktop application in your computer. Docker client and daemon communicate through REST API. Docker client will send the command to the docker daemon and the docker daemon will check the requests and interact with the operating system in order to create or manage docker container.

Here some important commands that can be used on docker client:

#docker build (used to build a docker image from specific docker #file)
docker build <path to docker file>
#docker run (used to run docker images)
docker run <docker image>
#docker pull (used to pull docker image from docker registry)
docker pull <docker image>
#docker push (used to push docker image to docker registry)
docker push <username>/<docker image>

Docker Image

Docker image is a template that is used to create docker container and it is built by using docker file. Docker file is a file that consist of instructions to create docker image. Docker images is stored on docker registry so anyone can access the same of docker environment that is already created.

Docker Example

Now I will give you the example how to run a docker container. As we know, we need docker image to create docker container. First we need to get an example of docker image by run this command.

Because we don’t have the image in the first place, it will pull the image from the registry. After it pulls from the registry, it will run the image to create the container and then run the container. From the example above, some message will be printed after we run the container.

Then we can also check our image list by using “docker images” command.

As we can see, there is hello-world image in our computer.

We can use “docker container ls -a” to list all docker container that we have.

Docker Compose

When your application is getting bigger, there is chance that you will be needed more than one container to run. It will be wasting of time if you need to run it one by one. For this problem, we can solve it by using docker compose.

Docker compose is a tool that used to define and run multiple container using YAML file. With docker compose, we can run multiple container that we want only using one command. To use docker compose, first we need to create docker-compose.yml. Here is the example of my project’s docker-compose.yml.

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

From the example above, we’re using two container for each services.

The first service is db for the database, we’re using postgres:13-alpine image to store data with the given volume defined at the bottom of the scripts. Then we configure the database by setting some variables on environment such as username, password, and database name.

The second service is web for the application, the image is from customized image created using docker file. After the service is built, we run some commands that defined on command section. On the example above, the command is to run deployment.sh to migrate our database. Then we forward the service from port 8000 on our host to port 8000 on web service container. For this service, the variables for this environment is defined in .env.dev file. We also defined that this service depends on db service because our application is need a database.

Then we can run docker compose by using these commands.

#To run docker compose
docker-compose up -d --build
#To stop docker compose
docker-compose down -v

Here is the example of running a docker compose:

Here is the example of stopping a docker compose:

That’s it for this article, I hope this article will help you to understand at least the basic of docker and you can implement it for your project. Thank you!!

References

--

--