CI/CD
Docker image and container, how to build docker file, image and container
Docker Image vs Docker container Through this post, we are going to familiarize ourselves with what is docker image and container, what is the difference between these two. But before knowing them, we need to also know about another option that is the docker file because the docker image is created based on the docker file, Container depends on the docker image. So the workflow will be something like below:
so let's start with -
Dockerfile
The formal definition is, it is a file that contains instructions to build an image. Will show a real-life example for any simple python project later in this post.
Docker Image
Formally docker images are the blueprint or template used to create a docker container. It contains all the files that are required to generate the container.
Docker Container
Docker containers are the running instance or executable of a docker image. It is used to run or execute any program or application. From one image we can create multiple docker containers but one container can map to one image.
Now let's understand these three options with a basic python program. Suppose we have a very basic python project having one python file printed a line as "This is a python code to say hello to docker image - container."
As you can see we don't have any other files or complexity here and all files are in the same directory or a folder, now we will create a docker file. Here we have only one dependency that is, we need python to run our program so in our docker file we have added the python with version as base image. Our source file is main.py and pointed out here what will be the source and destination, in our case we are creating everything in the root directory so the destination is showing as the dot(.).
Dockerfile:
#ADD base image that required to run the program from python:3.8.2
#Add the source file ADD main.py .
#Run the command CMD ["python","./main.py"]
Now it's time to build the image from this docker file.
Docker Image
Go to VS code terminal and go to the project folder where the docker file resides, now run the command below
docker build -t
my case, the command is = docker build -t basic-pythoh .
a new image has been created as below:
Docker container
Now if you check the docker container then we will not see any container for our image basic-pythoh as we did not yet run it.
As docker container is a running instance of a docker image so to create the docker container from our image basic-pythoh let's run the below command
docker run
Our case: docker run basic-pythoh
So you are done! your docker container is ready to share, if you share this image through the docker hub then anyone can pull your image and use