This article talks about core docker concepts like images, containers which are required for understanding docker better.
Hope you now have a basic idea around why we use docker and some of its terminologies. If not, here is the link.
Docker overview - A web developer perspectiveNow we'll start playing with docker images and containers. Time to start playing with docker CLI.
Below sections assumes you have docker desktop running locally. If you are not sure just type the below command in your terminal.
docker version
and you'll see some output like below.
Client: Docker Engine - Community
Cloud integration: v1.0.31
Version: 20.10.24
Server: Docker Desktop
Engine:
Version: 20.10.24
API version: 1.41 (minimum version 1.12)
OS/Arch: linux/amd64
Docker Images 👉👉 Containers
Now that we have docker up and running locally. Let's start our journey by running an official hello-world image.
docker run hello-world
You should see a response something like below.
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:9eabfcf6034695c4f6208296be9090b0a3487e20fb6a5cb056525242621cf73d
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
...remaining response
Don't get confused by all the response what we are looking is something like "Hello from Docker!" Which means our command executed successfully.
If you look at the response it clearly also contains the series of steps docker took for running the image on our system. Like:
- Pulling images from docker-hub,
- Creating a container from that image and
- Starting the container.
Something interesting to note is that If we execute the same command again it would be quicker as docker wont pull the image again from docker hub and will serve from cache.
It's a convenient way of fetching and starting a container at same time.
So we have talked about how the image is kept locally and not pulled every time. We can validate the details with below commands:
To see all locally available docker images
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 2 days ago 13.3kB
To see all local state containers
docker container ls -a
OR
docker ps -a
Now let’s use an official nodejs image and create a container out of it, To understand this process a little better.
docker run node
Unable to find image 'node:latest' locally
latest: Pulling from library/node
15568516ccf: Pull complete
fe64676es347: Pull complete
Digest: sha256:0d16da084c40181ed7141100580f9887ccc8e9a1
Status: Downloaded newer image for node:latest
Notice something different here: We don’t have any message like we had before for our hello-world image. Its because by default node container does nothing (has no default command).
We can ask it for example to print node version:
docker run node -v
v20.1.0
You should be able to see some version being displayed.
ℹ️ with this variation we can override the default command (if any) which was set when this container was created as per the instructions set by creator of the image.
The containers run in an isolated environment (Linux in this case) & we can also see from its file structure.
docker run node ls
response:
bin
boot
dev
etc
…more files.
This run commands has lots of other flags which can be used to pass configuration values while creating a container from image. Example port mapping, volumes etc.
💁♂️ Running same commands on hello-world image won’t be valid and the images uses debian:bullseye-slim version as base image which don’t is small in size and wont contain these binaries. Link: docker-library/hello-worlddocker run hello-world ls Some ERROR
Hope you got some understanding around images and containers after reading this article.
How we can set these commands and create custom images you must be wondering. In the next articles we’ll discuss more around these topics. We’ll also discuss what are Dockerfile and how can they help us. 😊