How to create a custom Docker image

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.

Example:
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

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

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

Linux terminal showing commands that create and enter the build-image-example directory for the custom Docker image.

$ mkdir build-image-example

$ cd build-image-example

Screenshot from the Build a Custom Docker Image walkthrough at step 2, showing RUN apk add --update redis; the adjacent article text explains that $ 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

Screenshot from the Build a Custom Docker Image walkthrough at step 3; the adjacent article text explains that 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

Screenshot from the Build a Custom Docker Image walkthrough at step 4; the adjacent article text explains that $ 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.

Screenshot from the Build a Custom Docker Image walkthrough at step 5; the adjacent article text explains that $ docker build .-don’t forget.

$ docker images
Now you can see the new image

Linux terminal showing docker ps -a output for a stopped container created from the custom Redis 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?

Screenshot from the Build a Custom Docker Image walkthrough at step 7, showing RUN apk add --update redis; the adjacent article text explains that let’s see what happens now.

Let’s see what happens now

Screenshot from the Build a Custom Docker Image walkthrough at step 8, showing Sending build context to Docker daemon 2.048kB; the adjacent article text explains that $ docker build -t pbryant/build-image-example:latest.

$ 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

Screenshot from the Build a Custom Docker Image walkthrough at step 9; the adjacent article text explains that $ docker imagesWe can see the new image and it’s name and the tag latest.

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

Screenshot from the Build a Custom Docker Image walkthrough at step 10; the adjacent article text explains that $ docker imagesWe 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

Screenshot from the Build a Custom Docker Image walkthrough at step 11; the adjacent article text explains that $ docker run -d pbryant/build-image-example.

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

Screenshot from the Build a Custom Docker Image walkthrough at step 12, showing CONTAINER ID; the adjacent article text explains that $ docker psWe can see that our new container is up and running.

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

Summary:
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. 

Keep exploring

Choose your next step

Continue with the path that best matches the architecture or operating challenge in front of you.

Leave a Reply

Discover more from Digital Thought Disruption

Subscribe now to keep reading and get access to the full archive.

Continue reading