At the end of this blog, you will get the idea of How to create a docker image of your own and How to containerize every application you need.
Firstly, Why do you need to create a docker image of your own?
It could be either because you cannot find a component or a service that you want to use as a part of your application on DockerHub or you decided the application you're developing will be dockerized for ease of shipping and deployment.
If you are new and just getting started with Docker, then checkout out my blog on Docker for beginners for a better understanding of the terminologies that I use here.
Creating a Dockerfile
So, Our first question is simply what is a Dockerfile?
When you are about to build a docker image, docker uses this file to build the image. A Dockerfile consists of build instructions to build the image.
Structure of a Dockerfile
Dockerfile follows a simple Instruction and Argument approach.
[INSTRUCTION] [ARGUMENT]
Instruction is the command we give to execute some tasks in the docker file. Instructions must be in capital letters.
FROM <baseImage>
Every docker image must be based on another image. Either the base image can be an OS or an existing image that was created before based on an OS.
So when the image runs we can access all the commands associated with that BaseImage.
Official releases of all OS are available on DockerHub.
RUN <command>
RUN instruction instructs docker to run a particular command on that BaseImage. The command can be either installing the dependencies or fetching the updated packages for the image.
COPY <source> <destination>
COPY instruction copies files from our local system to the docker image. Because we want our project in the local system to be available for the docker image we are building.
CMD <command>
CMD stand for command at the entry point. The Entry point command allows us to specify a command that will be run when the image is run as a container.
The final Dockerfile will be something like this:
Now our Dockerfile is finished, It's time to build our Docker image.
Building our Docker Image
This command can be used to build our DockerImage with our Dockerfile.
docker build -t <img_name> <location_of_dockerfile>
The last step is to run the Docker image using :
docker run <img_name>
Now the docker image will be successfully running on your system.
That's all from my side, I hope you learned something new from this blog. If the answer is yes, Do drop a like and leave feedback.
I'll be sharing my DevOps learning journey in my upcoming blogs. If you think we are on the same boat, Follow me on Twitter at shashankxrm for more content like this.
Happy reading and Have a great day!!