🌟 Have You Noticed the size of a Docker image when you create it? 🤔 Let’s take a simple example: imagine building a basic Node.js API and turning it into a Docker image.
As you can see in the image below, it’s taking up a whopping 1.6GB of space!
That’s a lot for such a simple app. So, how can we optimize the image size? 🤷♂️
Before we dive into the how, let’s first understand why optimizing image size is important.
Let’s goooo! 🚀🐋
Why?
Faster Image Pulls and Pushes: Smaller images take less time to upload or download from a Docker registry.
Reduced Disk Usage: Smaller images consume less disk space on local machines, servers, and registries.
Faster Deployment: Improves overall application responsiveness.
Lower Network Bandwidth Usage: Smaller images use less bandwidth during transfers.
Better Performance in CI/CD Pipeline: Optimized images reduce build, test and deployment times.
How?
To optimize the size of our Node Js API we can use lightweight base images like alpine
instead of full OS images.
Here is how we can use alpine
base image,
FROM node:18-alpine
WORKDIR /usr/src/app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["node", "index.js"]
To build the Docker image, use the following command:
docker build . -t image_name
After building the image, let’s compare the space it occupies:
Before Optimization: The image size was 1.6GB.
After Optimization: The image size is now reduced to 207.86MB.
This significant reduction was achieved by optimizing the Dockerfile and using a lightweight base image like Alpine Linux. Smaller images not only save storage space but also improve build, transfer, and deployment times. 🚀
Docker Registry: Docker Hub
🌟 Another key concept in Docker is the Docker Registry, which allows us to store and share Docker images. It acts as a centralized hub where Docker images can be pushed and pulled, making it easy to distribute applications and collaborate with others.
Docker Hub allows us to Store and Share docker image with others or, for deployment, Access public images for common services, Tag versions of image for easier management, and Integrate with CI/CD workflows for automation.
So let’s understand how we can push our docker image to and pull the docker image from docker repository.
Push Docker Image
To push a Docker image to Docker Hub, we first need to create a Docker repository. You can easily do this by visiting the Docker Hub website, which offers an interface similar to GitHub. Once there, create a new repository and choose whether to make it public or private based on your preference.
Steps to Build and Push the Docker Image:
Build the Docker Image
In your project directory, run the following command to build the Docker image:docker build . -t username/repository_name
Replace
username
with your Docker Hub username andrepository-name
with your desired repository name.Push the Image to Docker Hub
Once the image is built, push it to your Docker repository with the following command:docker push username/repository_name
This will upload your image to Docker Hub, making it available for others to pull and use.
Pull Docker Image
To pull a Docker image from a public repository (e.g., the official MongoDB image), you can use the following command:
docker pull mongo
This command will download the latest version of the MongoDB image from Docker Hub, a public registry.
If you want to pull an image from your own repository on Docker Hub (where you’ve pushed an image previously), use this command:
docker pull username/repository_name
Outro
Wrapping it up, Docker Hub is your go-to for sharing and storing images, making collaboration easier than ever. By optimizing your images for space and caching, you can streamline builds and enhance performance.
Happy Dockerizing🐋!