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
Create Dockerfile
A Dockerfile is simply a text-based script of instructions that is used to create a container image.
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 | docker build -t <app name> . |
Updating App
Change source code.
Build new image.
Find the old container’s id, stop it, remove it.
Run the new image.
1 | docker build -t <app name> . |
Sharing App
Create repo on https://hub.docker.com/
for example, 101-todo-app
Login from local
Give image a new name
Push it.
1 | docker tag docker-101 YOUR-USER-NAME/101-todo-app |
- 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.
- create volume
1 | docker volume create todo-db |
stop the previous todo app container
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 |
- add items into todo list. then stop and remove the container.
start a new container with the same run command above
use
docker volume inspect todo-db
to check TheMountpoint
is the actual location on the disk where the data is stored.