Introduction to Docker file
In this blog, we will see how an image is created from the docker file
We will use a simple Python program and create a docker file that creates an image and finally create a container.
What is a docker file?
Dockerfiles are collections of instructions that instruct you on creating a specific Docker image.
The following commands can be used in a Dockerfile:
FROM, PULL, RUN, and CMD are all commands.
FROM: Generates a layer based on Ubuntu 18.04.
Pull: This command adds files from your Docker repository.
RUN: Constructs your container.
CMD: specifies which command should be executed within the container.
A sample Dockerfile containing the necessary commands is shown below.
FROM python :3.11-alpine
PULL /file
RUN: make / file command.CMD: python /file/file.py
Example of Dockerfile with python
Below is a simple Python program called calculator.py for the addition of numbers this program is added to the docker file that creates an image and finally container is created and run.
Docker file Content
To build an image from the docker file
syntax is:- docker build -t new_image_name . # note at the end of the command give a space and dot .
now we will create a container for an image we created from docker file.
docker run --name container_mehdi mehdipython_image
docker run --name new_container_name image_created_name
I will explain the above command first is docker run --name container you can give any name and then image name in my case its mehdipython_image
As an example using the same image I have created another container and checked the docker logs as with the container id.
Docker file example with sleep time
we will see how to make the container status up, remember docker container stops when the application it is running is stopped.
the Python program we used above we will add an import time package and mention time.sleep(1000) # sleep for 1000 seconds
- Build the image again as below
- create the container and check the status as up
Subscribe to my newsletter
Read articles from mehdi pasha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by