Wednesday, September 10, 2025

Docker Tutorial 2025 Part 1

Docker Tutorial 2025 Part 1

This is a quick and dirty tutorial for Docker

What is docker?

Docker is a container, a lightweight vm to run linux os environment. It use lesser (much lesser) resource than Virtualbox. Docker can be used for development and production, it provide consistant environment, and more secure.

Docker can run on Linux, macOS (Docker Desktop) and Windows (WSL2).

Install

Linux

So far using the script is easiest, it even works on Chrome OS constini vm. Before install using the script, remove the previous version as advised.

# eg. for debian, check for dpkg -l | grep "docker\|container.io", remove those softwares
$ curl -fsSL test.docker.com -o get-docker.sh && sh get-docker.sh

macOS (brew colima)

I use colima (+lima +qemu), as a replacement of Docker Dekstop for macOS.
$ brew install colima
$ colima start # it may takes some time to start colima

To test docker is working:
$ docker run hello-world
=>Hello from Docker!

Note: You may need root/sudo to run.

Image and container

Docker image is a pre-build image ready to be run, it can be downloaded from docker hub, modified and update back to the docker hub. A container is a running vm base on the docker image.

# docker pull image:tag
$ docker pull busybox:latest
$ docker run busybox:latest
# OR
$ docker run busybox # if will add the tag:latest and download from docker hub
=> (nothing)

Nothing shown because this image doesn't output any message by default.

$ docker run busybox echo Hello, world
=> Hello, world

Image

Image name and tag can be found on hub.docker.com.
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 0ed463b26dae 11 months ago 4.43MB

You can remove the image by:
# docker rmi image_id/image_name
$ docker rmi 0ed463b26dae
=> unable to remove repository ... container 347423e07780 is using

It is ok you can not delete the image, as container is using it.

Container

$ docker run -it busybox /bin/sh # -it, interactive tty??
busybox# echo "hello, world" >/hello
busybox# exit

# if you run the command again, cant find the file /hello.txt
$ docker run -it busybox ls /hello.txt
ls: /hello: No such file or directory

That is because every time you execute the command it created a new container. The file still exist in the earlier container.

$ docker container ps -a
ea9066a4d753 busybox "ls /hello" ... tender_shaw
86d9045d5cab busybox "/bin/sh" ... kind_maxwell

The container has a unique id and a unique random name. The file hello is at the container kind_maxwell. Since the status is Exited, need to start the container to access the file again.

$ docker container start kind_maxwell # OR simply docker start kind_maxwell
$ docker exec kind_maxwell cat /hello.txt
hello, world

Delete container and image

$ docker container ps -a
You can delete one by one, or delete all.
$ docker container prune
# stop container
$ docker [container] stop id

Now you can delete the image:
$ docker rmi busybox
...
# delete all
$ docker rmi $(docker image ls -q) # -q, quiet, just list the image id

Start container with specific name

$ docker run --name kind_maxwell busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
$ docker ps -a | grep kind_maxwell
ee044ad46bf2 busybox:latest Exited (0) kind_maxwell

No comments: