Creating your first Docker Image
So far we talked about the very basic of what is a Docker in Dockers for Dummies, and then covered some Useful commands for Dockers.
This time we’ll focus on how we can create our very own Docker image.
In order to build our Docker image, we’ll need any code editor (visual studio code for example ) and a Docker-CLI (Docker Client), in which we’ll type our Docker commands. So, if you didn’t install yet Docker-desktop, now is the time.. please install it from here.
So, what do we need in-order to create a Docker Image?
We’ll need a Dockerfile. A Dockerfile is a simple text file which contains configurations of how our container should behave, what different programs it’s going to contain and what it does once it’s start-up as a container.
For example, We can refer to a Dockerfile as being given a computer with no OS and being asked to install Chrome browser. So we’ll need to cover all steps from installing OS, configuring network connection, starting the default browser, going to chrome web site, downloading, installing and then running chrome. All these steps should be covered in our Dockerfile.
Building a Dockerfile
We’ll start by opening our Power Shell as Admin user, next we’ll create new image directory:
$ mkdir my-first-docker-image
Open the folder we just created in your code editor, here I’ll use Visual Studio Code, and create new file. Named the file ‘Dockerfile’ without any ending.
You can see that once we’ll create the file, we’ll have a small Docker icon next to its name:
Next step will be to write the content of our Dockerfile. There are multiple available commands, we’ll use the most common ones: FROM, RUN and CMD.
FROM — will specify the Docker image we want to use as a base image. e.g. our OS in the above chrome’s example.
Here we’ll use Alpine Image as a base image for Linux distribution, as it’s tightly controlled, small size, and still being full Linux distribution:
$ FROM alpine
RUN — will build our application with make. It will specify the commands to install additional applications and dependencies. e.g. going to chrome web site, downloading, installing etc.
Here we’ll create redis image and output some text while doing so:
$ RUN echo 'Starting to build redis in my first Docker..'
$ RUN apk add --update redis
CMD — will specify what should be done once our image will start-up as a new container. e.g. run chrome.
Here we’ll enter into the redis server:
$ CMD ["redis-server"]
Our complete Dockerfile will look like this:
Once our Dockerfile is ready, we can build our container from the Docker Image.
Building our Docker Image
In this step we’ll use the Docker build command to build our Docker Image.
General command syntax:
$ docker build [OPTIONS] PATH | URL | -
We’ll use the following syntax:
$ docker build -t my-first-docker-image/redis:latest .
Where Tag (
-t) is allowing us to refer easily to our Docker by name instead of using its id and allowing us to access specific version.
Moreover, our Docker Image path is '.' indicating the current location.
The resulting image will be tagged with the repository name- ‘my-first-docker-image/redis’ and the version tag will be ‘latest ‘.
Last step, building our container from our Docker Image
What we have left is to start-up our Container. In order to do so, we’ll use the
docker run command:
$ docker run my-first-docker-image/redis
We can use
docker ps in order to verify that our Container is up and running:
Mission accomplished! We have a Container from our own Docker Image!!
See you soon,