The real power of Docker lies in tweaking the base docker image to your requirements. With that, you can easily and quickly deploy a custom Linux environment by running a container from that custom docker image.
Docker provides a simple way to configure any docker image and create your own custom image with the help of the dockerfile.
In this tutorial, you will learn how to pull an official image from the Docker repository and customize it according to your own requirements. And then you can run and verify the custom docker image.
Creating custom docker image
So, in this example, you’ll be using an Alpine Linux image that does not include the Vim editor by default. You’ll modify this image to create a new docker image of Alpine Linux that includes Vim editor by default. It cannot get any simpler than this. can it?
If you have not already, please install Docker on Ubuntu or whichever Linux distribution you are using. Make sure to add yourself to the docker group so that you can run docker without sudo.
You’ll need active internet connection for downloading the base docker image.
Step 1: Get docker image [optional]
I chose Alpine Linux in this example because it is really small. The Alpine docker image is hardly 5 MB in size, can you believe it? It’s the perfect Linux distribution for containerization.
This step is optional. I included it show that you can compare it with the customized docker image.
Pull the latest docker image of Alpine Linux using docker pull command:
docker pull alpine
Step 2: Create Dockerfile with the needed customization
Now let’s create a new empty file named Dockerfile using touch command.
FROM alpine:latest RUN apk update RUN apk add vim
What you are doing here is to create a new docker image by downloading the latest Alpine docker image from the Docker Hub.
Like apt, Alpine uses apk package manager. So the next two commands are basically telling Alpine linux to update the available package cache (apk update) and then install Vim (apk add vim).
As you can see, with RUN in Dockerfile, you can customize your base docker image by running specific commands.
Step 3: Create the custom docker image with Dockerfile
The command to build the custom image from the Dockerfile looks like this:
docker build -t new_docker_image_name PATH_to_Dockerfile
With the -t tag, you specify the name of your custom docker image.
Considering that your Dockerfile is in your current directory, you can create the new docker image of Alpine Linux with Vim installed like this:
docker build -t alpine-with-vim .
Let’s see the available Docker images on the system now:
[email protected]:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine-with-vim latest fa8255cf0de0 5 seconds ago 33.9MB alpine latest a187dde48cd2 3 weeks ago 5.6MB
You can see that the base docker image which was hardly 5 MB in size is now 33 MB with Vim installed on it (and the package cache updated).
Now, let’s verify that your modified docker images has vim installed by running a container from it:
docker container run -ti alpine-with-vim /bin/sh
Once you are inside the container, you can verify that Vim is installed by checking its version:
/ # vim --version VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Dec 12 2019 19:30:49) Compiled by Alpine Linux
Congrats! You just learned how to create your very own customized docker image.
I know it’s not a very extensive tutorial and you may have complex need. But this tutorial works as the first step towards understanding docker image customization.
I highly recommend reading more on Dockerfile to know what other options you have available for customizing docker images.
If you have questions or suggestions, please leave a comment below.