DEV Community


Building a Custom Docker Image

ianknighton profile image Ian Knighton Originally published at on ・2 min read

Building a Custom Docker Image

Going to break up my record catalog posts with a quick hit about Docker Images. I've had to do this a lot lately and I've struggled to find a quick, concise post on it.

The Problem

I need a docker image that has Node, NPM, Go, and Git installed on it. I could combine images and do some magic that way, but I would much rather have my own image that has everything I need and is configured in a way I know.

The Solution

I'll start with the Ubuntu 18.04 image as my base. It's pretty stripped down, but is a system I'm familiar with.

docker run -it --name base ubuntu:latest

This will pull the image and give you shell access into the Ubuntu image.


Now that we're in our shell, we can update and install the pieces we need.

apt update && \
apt upgrade && \
apt install -y git && \
apt install -y nodejs && \
apt install -y npm && \
apt install -y golang-go

This will take a minute or two to run. Once it finishes, exit the shell. This will stop the container from running. This is also a good opportunity to create any environment variables or folders you may need.

Now you can commit the status of the container to an image.

Let's check to make sure we have the name and ID of the container:

docker ps -a
CONTAINER ID IMAGE         COMMAND     CREATED       STATUS                   NAMES
66894d551112 ubuntu:latest "/bin/bash" 5 minutes ago Exited (0) 2 seconds ago base

Then you can use the commit command.

docker commit <containerName or containerID> <imagename:<tag>>

So in this case it would be:

docker commit 66894d551112 baseimage:latest

At this point, you have a base image on your machine that you can use. If you need it to be available to your team or your CI pipeline, you can put it in docker hub using docker tag and docker push.

docker tag baseimage:latest probablynotian/baseimage:190314.0
docker push probablynotian/baseimage:190314.0

Going forward, you can reference this image anywhere. This makes your Dockerfile a lot easier to work with.

I hope this helps!

(If you want to use this image, I have it up on my public docker hub account.)

Discussion (1)

Editor guide
gautamkrishnar profile image
Gautam krishna R

You are not mentioning about the docker volumes. All the data of the containers can be restrained if we are using it.