Reducing Docker's image size while creating an offline version of Carbon.now.sh
Tags: docker, node, containers, development
Published: 2020-05-16 08:23:32

---
title: Reducing Docker's image size while creating an offline version of Carbon.now.sh
published: true
date: 2020-05-15 17:04:59 UTC
tags: docker,node,containers,development
canonical_url: https://lengrand.fr/reducing-dockers-image-size-while-creating-an-offline-version-of-carbon-now-sh/
---

 ![Reducing Docker's image size while creating an offline version of Carbon.now.sh](https://images.unsplash.com/photo-1532344859118-9b9581911b87?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

Disclaimer: It's close to my first time playing around with Docker so you might find the article underwhelming :).

I'm sure most of you are used to those beautiful code snippets you see in presentations or at conference talks. They look like just like this one :

<!--kg-card-begin: image-->

![Reducing Docker's image size while creating an offline version of Carbon.now.sh](https://lengrand.fr/content/images/2020/05/image.png)<figcaption>An example of code snippet</figcaption>

<!--kg-card-end: image-->

Well, almost all of them come from [carbon.now.sh](https://carbon.now.sh/), who is doing a great job at making your code look nice.

Unfortunately, I work in a large company and it decided to block access to the website to avoid risking any data leaks (which makes a whole lot of sense if you ask me). Well, luckily for us Carbon is [open-source](https://github.com/carbon-app/carbon) and uses the [MIT license](https://github.com/carbon-app/carbon/blob/master/LICENSE) so we can spin our own internal version of it.

This blog lists my journey dockerizing the application and reducing the final image size.

## Getting that sweet Docker image working

The first step is to get any kind of Docker image working, straight to the point. Let's do it.

We start by cloning the repo and creating a `Dockerfile` at the root of the project. The project requires node 12 so we'll use the [official node image](https://hub.docker.com/_/node/) as base image.

<!--kg-card-begin: markdown-->
```
FROM node:12

WORKDIR /app

COPY package*.json ./
RUN yarn install
COPY . .
RUN yarn build

CMD ["yarn", "start"] 

```
<!--kg-card-end: markdown-->

What we're doing here is very limited:

- We define a working directory inside the base image
- We install dependencies using `yarn install`
- We build the project 
- We define `yarn start` as start command when the image will be ran

What is now left to do is actually build the image, and test it (you might want to run using the `-d` option to get detached mode if you intend to run the server for a long time :). I am just testing here).

<!--kg-card-begin: markdown-->
```
$ docker build -t julienlengrand/carbon.now.sh .
$ docker run -p 3000:3000 julienlengrand/carbon.now.sh:latest

```
<!--kg-card-end: markdown-->

Now if we go to [http:localhost:3000](http:localhost:3000), we should see this :

<!--kg-card-begin: image-->

![Reducing Docker's image size while creating an offline version of Carbon.now.sh](https://lengrand.fr/content/images/2020/05/image-1.png)<figcaption>The landing page of carbon, but on localhost</figcaption>

<!--kg-card-end: image-->

Great!!!! .... **Except for the fact that my image takes 2.34Gb of disk space!** For something that takes screenshots, it won't be acceptable :).

<!--kg-card-begin: markdown-->
```
➜ carbon git:(feature/docker) docker images
REPOSITORY IMAGE ID SIZE
julienlengrand/carbon.now.sh 81f97ac3419b 2.43GB

```
<!--kg-card-end: markdown-->

Let's see what more we can do.

## Keeping only the app in the image

Thing is, the way we've built the image now works, but it is far from efficient (but we knew that already). We have our whole toolchain in the container, as well as the build and development dependencies and more. We want to get rid of all this, as we don't need it to run our server.

One of the common ways to do this in the Docker world is called multi-step builds, and one of the ways to achieve this is to use the [builder pattern](https://matthiasnoback.nl/2017/04/docker-build-patterns/) (not to be confused with the other well-know [builder pattern](https://en.wikipedia.org/wiki/Builder_pattern)). In short, we use a first container to build our application and create our final image.

Let's see how that looks like :

<!--kg-card-begin: markdown-->
```
FROM node:12 AS builder

WORKDIR /app
COPY package*.json ./
RUN yarn install
COPY . .
RUN yarn build

FROM node:12

WORKDIR /app
COPY --from=builder /app .
EXPOSE 3000
CMD ["yarn", "start"]

```
<!--kg-card-end: markdown-->

This Dockerfile contains essentially the very same lines as before except for two major differences:

- We now split operations over 2 containers (one builds, the other will run)
- We copy the result of the build step over to the second container to create the final image.

Just like before, we use the same commands to run and test this new version (surprisingly, it works as expected!).

The nice side-effect of multi-step build can be directly seen. We divided our final image size by 2 :

<!--kg-card-begin: markdown-->
```
➜ carbon git:(feature/docker) docker images
REPOSITORY IMAGE ID SIZE
julienlengrand/carbon.now.sh 586a65d1ee4e 1.34GB

```
<!--kg-card-end: markdown-->

1.34Gb for a webapp that takes glorified screenshots though, it's still way too much for me. Let's dive further.

## Using a more efficient image

Using the official Node image has benefits but given that it's based on a Debian system, it's also very large. The next step for us is to look at a smaller image. One of the well known 'lighter' distros for containers is [alpine](https://hub.docker.com/_/alpine) and luckily there is a supported node version of it called [mhart/alpine-node](https://hub.docker.com/r/mhart/alpine-node/)!

This time our Dockerfile barely changes, we just want to replace the base image:

<!--kg-card-begin: markdown-->
```
FROM mhart/alpine-node:12 AS builder

WORKDIR /app
COPY package*.json ./
RUN yarn install
COPY . .
RUN yarn build

FROM mhart/alpine-node:12

WORKDIR /app
COPY --from=builder /app .
EXPOSE 3000
CMD ["yarn", "start"]

```
<!--kg-card-end: markdown-->

And again, we build and run with expected success :).

Again, we divide our image size by two and with this version we're just over 500Mb!

<!--kg-card-begin: markdown-->
```
➜ carbon git:(feature/docker) docker images
REPOSITORY IMAGE ID SIZE
julienlengrand/carbon.now.sh b79dbcd33de0 502MB

```
<!--kg-card-end: markdown-->
## Removing more of the dependencies and things we don't use

We can keep trying to reduce the bundle size by shipping even less code to the container. Let's use `npm prune` for that (unfortunately, yarn [decided to not offer an exact equivalent](https://github.com/yarnpkg/yarn/issues/696)). By using `npm prune --production` right after building, we can get rid of all of our dev dependencies. Rebuilding the image shaves yet another 100Mb.

Here is our final Dockerfile:

<!--kg-card-begin: markdown-->
```
FROM mhart/alpine-node:12 AS builder

WORKDIR /app
COPY package*.json ./
RUN yarn install
COPY . .
RUN yarn build
RUN npm prune --production

FROM mhart/alpine-node:12

WORKDIR /app
COPY --from=builder /app .
EXPOSE 3000
# Running the app
CMD ["yarn", "start"]

```
<!--kg-card-end: markdown-->

That's it for now. I'm looking for more ways to shave some more megabytes but we did reduce the size of our deployable by almost a factor of 10! For a bit of feel good, here is the list of images we created so we can see progress:

<!--kg-card-begin: image-->

![Reducing Docker's image size while creating an offline version of Carbon.now.sh](https://lengrand.fr/content/images/2020/05/image-2.png)<figcaption>List of all images and sizes</figcaption>

<!--kg-card-end: image-->

It still feels quite crazy to me that a simple website needs 400Mb to run today, I'm sure we can do better :). But let's stop there for now, time for a well deserved weekend!

Oh, and if you want to use Carbon locally, feel free to pull the image from the Docker Hub and run it locally :

<!--kg-card-begin: markdown-->
```
docker run -p 3000:3000 julienlengrand/carbon.now.sh:latest

```
<!--kg-card-end: markdown-->

_Some references I used today (thanks to them!)_

_[1][https://medium.com/@khwsc1/a-simple-react-next-js-app-development-on-docker-6f0bd3f78c2c](https://medium.com/@khwsc1/a-simple-react-next-js-app-development-on-docker-6f0bd3f78c2c)_

_[2][https://itnext.io/frontend-dockerized-build-artifacts-with-nextjs-9463f3da3362](https://dev.to/efreyreg/frontend-dockerized-build-artifacts-with-nextjs-49c7)_

_[3][https://medium.com/trendyol-tech/how-we-reduce-node-docker-image-size-in-3-steps-ff2762b51d5a](https://medium.com/trendyol-tech/how-we-reduce-node-docker-image-size-in-3-steps-ff2762b51d5a)_