Docker

Build an Image by Specify Dockerfile Location

Docker images serve as the main component of the Docker platform. Docker images are files that contain instructions for executing the container’s code. It includes all the necessary instructions for creating and managing a Docker container. It also tells the container how to deploy or build the project. Moreover, Docker images allow developers to share and deploy apps on another system effortlessly.

This post will explain how to build an image by specifying the Dockerfile location.

How to Build an Image by Specifying Dockerfile Location?

To build an image by specifying the Dockerfile location, check out the given procedure.

Step 1: Open the Terminal
First, launch the Git Bash terminal from the Windows Start menu:

Step 2: Move to Dockerfile Directory
Next, open the directory where you want to build Dockerfile through the “cd” command:

$ cd "C:\Dockerdemo"

Step 3: Create and Open Dockerfile
Create and open the new Dockerfile in the nano text editor using the “nano Dockerfile” command:

$ nano Dockerfile

Paste the provided code inside the Dockerfile, which will print “Docker is more simple Deployment Tool”:

FROM python:3.6

RUN apt-get update && apt-get install -y --no-install-recommended>
        python3-setuptools \
        python3-pip \
        python3-dev \
        python3-venv \
        git \
        && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

EXPOSE 8000
CMD python -c "print('Docker is more simple Deployment Tool')"

After that, press the “CTRL+O” to save the Dockerfile and “CTRL+X” to exit the editor:

Step 6: Create and Open New Directory
Now, create a new directory in which you want to generate a Docker image through Dockerfile using the “mkdir” command. Then, open the directory by utilizing the “cd” command:

$ mkdir Dockerdemo2
$ cd Dockerdemo2

Step 7: Build Docker Image by Specifying Dockerfile Location
Build the new Docker image by specifying the Dockerfile location through the given command. Here, the “-t” option is used to specify the image name, and “-f” is utilized to access the Dockerfile forcefully:

$ docker build -t docker-image -f /c/Dockerdemo/Dockerfile

Step 8: Run Docker Image
Lastly, check if the Docker image is created and working properly or not by executing the “docker run <image-name>” command:

$ docker run docker-image

The output indicates that the Docker image is successfully executing the Docker container to deploy a simple python project:

We have demonstrated how to build an image by specifying the Dockerfile location.

Conclusion

To build an image by specifying the Dockerfile location, first, create a new Dockerfile that contains instructions for the Docker image. After that, move to the directory where you want to build the Docker image. Then, utilize the “$ docker build -t <image-name> -f <Dockerfile-path> .” command. This post has elaborated on how to build an image by specifying the Dockerfile location.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.