I heard the words, container, and docker a lot. I know they refer to a tool related to Dev/Ops, but I never use one before. Recently I am writing a tool to find the best apartment (rent goes down since COVID19). Then I I think probably I should use docker in the apt finder project. This article is the first glimpse of docker. I would say it’s magical.

What’s Docker?

Container is a solution of isolating & running different applications on the same infrasturcture.
https://www.docker.com/resources/what-container

Docker is a type of containers.

Tutorial

Full tutorial: https://www.docker.com/101-tutorial

Getting Start Container

  1. Create Dockerfile

    A Dockerfile is simply a text-based script of instructions that is used to create a container image.

  2. Run it!

    Running the new container in “detached” mode (in the background) and creating a mapping between the host’s port 3000 to the container’s port 3000.

1
2
docker build -t <app name> .
docker run -dp 3000:3000 <app name>

Updating App

  1. Change source code.

  2. Build new image.

  3. Find the old container’s id, stop it, remove it.

  4. Run the new image.

1
2
3
4
5
6
7
docker build -t <app name> .

docker ps
docker stop <the-container-id>
docker rm <the-container-id>

docker run -dp 3000:3000 docker-101

Sharing App

  1. Create repo on https://hub.docker.com/

    for example, 101-todo-app

  2. Login from local

  3. Give image a new name

  4. Push it.

1
2
docker tag docker-101 YOUR-USER-NAME/101-todo-app
docker push YOUR-USER-NAME/101-todo-app
  1. Running image on new instance
1
docker run -dp 3000:3000 YOUR-USER-NAME/101-todo-app

Persisting DB

to-do app as example. Docker will save data file on the host machine and make it available to the next container. Docker maintains the physical location on the disk and you only need to remember the name of the volume.

  1. create volume
1
docker volume create todo-db
  1. stop the previous todo app container

  2. Start a new todo app container with [-v]

1
docker run -dp 3000:3000 -v todo-db:/etc/todos YOUR-USER-NAME/101-todo-app
  1. add items into todo list. then stop and remove the container.
  2. start a new container with the same run command above

  3. use docker volume inspect todo-db to check The Mountpoint is the actual location on the disk where the data is stored.

Reference