Episode 6. Docker images are read-only, understanding layers

Amir Mustafa
4 min readFeb 20, 2022

→ Suppose we have our code and executed our Dockerfile with the help of the below command, our image is created.

docker build .

→ After some time a new feature is requested or a bug is introduced, we have to modify our code and fix it.

→ Suppose we have added some more codes.

→ If after changes we stop our container and restart it, we will observe our latest code is not rendered.

docker stop <container-ID>
docker run -p 3000:80 <container ID>

Q. Why does this happen? Is it a bug?

→ Mainly images hold one-to-many relationships. Many containers can be created from one image. This will impact other containers.

→ So code is locked. when we build command using the build command

Solution:

Every time code changes, we need to build again using the below command

docker build .     // we get image ID in last line
docker run -p 3000:80 <image ID>

→ If we run in a browser

http://localhost:3000

Understanding Image Layers:

→ Whatever code we write in Dockerfile executes in layers

→ If build is done and see our docker application running:

Case A: If we edit the code and rebuild docker, the latest code is created.

Case B: If we without any change, run the build command again, this time execution will be very fast. Docker caches the data.

→ All the commands run in Dockerfile have different image layers

→ Image is built in multiple layers based on the instruction.

→ If we do make a change, layers after the specific command layer will be re-executed again.

→ What we are trying to understand here is if we rebuild the image again and again npm install command will run again and again because that code is written after line 5

→ So what we will do is first copy the package.json file to /app

→ Then run

npm install

→ Finally all other codes. So if this time we modify our code, the npm install command will not run always.

Summary of Images and Containers:

→ We create images from Docker file

→ Image contains App code + Environment (dependencies for node)

→ Docker is all about the container, image is the building block.

→ When a container is running, the code is not copied, an extra layer is on top of the image. There can be multiple containers running independently of each other

→ New changes in code, add on the container. We need to build again.

Video:

Closing Thoughts:

The takeaway from this article is image is read-only. We need to build code after every code change. We also have seen the execution steps of Dockerfile to save package reinstall again.

Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.

--

--

Amir Mustafa

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on https://www.linkedin.com/in/amirmustafa1/