DEV Community

Ian Knighton
Ian Knighton

Posted on • Originally published at ianknighton.com on

Building a Custom Docker Image

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
Enter fullscreen mode Exit fullscreen mode

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

root@66894d551112:/#
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then you can use the commit command.

docker commit <containerName or containerID> <imagename:<tag>>
Enter fullscreen mode Exit fullscreen mode

So in this case it would be:

docker commit 66894d551112 baseimage:latest
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.)

Top comments (1)

Collapse
 
gautamkrishnar profile image
Gautam Krishna R • Edited

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