Build your own Dockerfile, Image, and Container
Tags: docker, microservices, kubernetes, beginners
Published: 2023-01-16 10:16:38

Follow me in my blogs website (https://www.arunyadav.in/blogs)

Linux containers are the way to build a self-contained environment that includes software, libraries, and other tools. This guide describes how to build a docker image that you can use for the running container.

**Prerequisite**
Follow the previous article if you need to understand "[What is Docker?](https://www.arunyadav.in/blogs/post/MTE%3D)" 

[Install Docker on Windows](https://docs.docker.com/desktop/install/windows-install/) 

<u>Once installation is finished and your environment setup is completed.
</u>

An image contains all the software you need to run within your container. Container images can be stored locally on your machine, as well as in a container registry. There are public registries, such as [Docker Hub](https://hub.docker.com/), or private registries such as ACR (Azure Container Registries).

You can pull existing images from the docker hub or private registries using `docker pull`

In the following example, we will pull an image from the public Docker Hub repository and run the actual container. 

```
#First, we will pull an image
docker pull docker/whalesay
#We can then look at which images are stored locally
docker images
#Then we will run our container
docker run docker/whalesay cowsay boo
```

**Following is the command output**

![CommandOutput](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kozu9x9g8cbbmuu6edk2.png)

**Let's look at what happing behind the scene.**

- Docker first pulled your image in multiple parts and stored it locally on the machine it was running on.
- When you run the actual application, it used that local image to start a container.
- If we look at the command in detail, you will see that docker pull took in a single parameter, docker/whalesay. if you don't provide a private container registry, Docker will look into the public Docker Hub for images.
- The docker run command took in a couple of arguments, first docker/whalesay, which is the reference to the image. The next two arguments, cowsay boo, are the commands that were passed to the running container to execute 

In the previous example, you saw that it is possible to run a container without building an image first. however, it is very common in the practical world that you will want to build an image first. To do this, you use a Dockerfile.

Docker images can be created using a special file format called a “Dockerfile”. This file has commands that allow you to:

- use a pre-existing Docker image as a base
- add files to the image
- run installation commands
- set environment variables

In the next example, you will build a custom Docker Image. This custom image will display inspirational quotes in the whale output.

```
FROM docker/whalesay:latest
RUN apt-get -y -qq update
RUN apt-get install -qq -y fortunes
CMD /usr/games/fortune -a | cowsay
```

Let's look at what happing behind the scene.

- The first line will instruct Docker on which image to use as a source/base image for this new image.
- The next two steps are commands that are run to add new functionality to our image
  - In this case, update your apt repository and install an application called fortunes. The fortunes application is a small command-line tool that generates inspirational quotes. We will use that to include quotes in the output rather than user input.

- Finally, the CMD command tells Docker which command to execute when a container based on this image is run.

<u>**_You typically save a Dockerfile in a file called Dockerfile, without an extension. _**</u>

**To build an image and point it to the Dockerfile you created.**
`docker build -t smartwhale .`

You will now see Docker execute a number of steps

![CommandOutput](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sybhqnrv3hxv3872y0x1.png)

**To run your container**
`docker run smartwhale`

Following is the command output with inspirational quotes and if you run the container multiple times, you will see different quotes appear.

![CommandOutput](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ltu4rhagqao2y82oa4h9.png)

That concludes our overview and demo of containers. In this section, you started with an existing container image and launched it on the machine. Afterward, you took that a step further and built your own container image, then started containers using that image. You have now learned what it takes to build and run a container.

Thanks for reading!