Docker Image Optimization: from 1.16GB to 22.4MB

Docker is a platform for software developers and sysadmins to build, run, and share applications with containers. A container is a process that runs in an isolated environment, on its own filesystem; that filesystem is built using a docker image. Images include everything you need to run an application (compiled code, dependencies, libraries, etc.). Images are defined using a file called Dockerfile.
The terms dockerization or containerization are often used to define the process of creating a Docker container.
Containers are popular because they are:
- Flexible: Even the most complex applications can be containerized.
- Lightweight: Containers share the host kernel, making them much more efficient than virtual machines.
- Portable: Can be compiled locally and run anywhere.
- Loosely coupled: Containers are encapsulated, allowing one to be replaced or upgraded without interrupting the others.
- Secure: Containers apply aggressive restrictions and isolations to processes without requiring any configuration on the part of the user.
In this post I’m going to focus on optimizing Docker images to be lightweight.
Let’s start with an example in which we build a React application and we want to dockerize it. After running the npx command and creating the Dockerfile, we have a file structure like the one in Picture 1.
npx create-react-app app --template typescript
If we build a basic Dockerfile, like the following, we end up with an image that weighs 1.16 GB:












