How to create a custom Docker image

Posted by

In this article we will be covering building a simple custom Docker image.  Following creating this Docker image we will deploy a container.  Finally, we will add a couple more dependencies, add a tag, and re-deploy our final custom image.

First let’s start with some Dockerfile basics:
First word is an instruction that it tells the docker server something specific to do and then the next piece is the argument to the instruction.

FROM = first line in every Dockerfile — choose a base image to use
Run = run some dependency to install additional add-ons or programs
CMD = command that will run on startup when the new container is deployed

Think of building a Dockerfile like someone handing you a bare metal server and telling you to go install a vCenter server on it. 
The first thing you are going to want to do is deploy ESXi on the server, right? 
Then after ESXi has been fully deployed you would want to download the VCSA ISO image. 
Then navigate to the installer. 
Then execute the installation process & wait for it to complete

Installing ESXi onto the server
Download VCSA ISO
Deploy from installer
Install VCSA
Execute install process for VCSA

I know the above is real high level basic vCenter install but I hope you get the example.

$ mkdir build-image-example

$ cd build-image-example

Here is where you can use the editor of your choice – I use VS Code as my editor and then copy & paste into VI

$ vim Dockerfile
**be sure to capitalize the D in Dockerfile  all one word

$ docker build .
—don’t forget the . on the end because this indicates to use the current directory
You should see “Ready to accept connections” — now you know it worked.

$ docker images
Now you can see the new image

$ docker ps -a
This command shows all the containers that have been run
The above didn’t have a lot of dependencies nor was it all that complex.  What if we wanted to add some more layers? What if we wanted to tag it with a name to be more professional and to version it?

Let’s see what happens now

$ docker build -t pbryant/build-image-example:latest .
This tag is a two part    Repository_Name:Name_of_Tag
You’ll notice this went faster than before because it already had the alpine image downloaded

$ docker images
We can see the new image and it’s name and the tag latest

$ docker run pbryant/build-image-example
As you can see we were able to create the container using the name of the image which is a lot easier than remembering a random number.
Ready to accept connections, just like earlier.
You’ll also know it just locked up your terminal so let’s ctrl+c out and re-run with the -d command

$ docker run -d pbryant/build-image-example

$ docker ps
We can see that our new container is up and running.

As you can see from this simple example it is not difficult to create a custom image * deploy a container in Docker.  This was a very basic and simple example but more complex examples are coming.  As always, I hope y’all found this helpful. 

Leave a Reply