Docker

Docker Run Image

Docker images are the essential component of the Docker environment. A Docker image includes instructions for Docker containers. These instructions tell the container how to manage and deploy the application. Docker images are usually made by Dockerfile, which is a set of commands used to build and run Docker images.

This blog will demonstrate how to run a Docker image using the “docker run <image-name>” command.

How to Run Docker Image?

The Docker “run” command is utilized to run images. For this purpose, first, create a simple Dockerfile. Then, build a new Docker image and run the image using the “docker run <image>” command.

Look at the given instructions to execute the Docker image.

Step 1: Create Program File

Create a simple Python program file as “Tutorial.py”. The specified program will display the “Hello, Welcome to Linuxhint Tutorial” message on the console or terminal:

print("Hello, Welcome to Linuxhint Tutorial")

Step 2: Create Dockerfile

Next, create a new file named “Dockerfile” and paste the following piece of code into the file:

FROM python:alpine
WORKDIR /src/app
COPY . .

CMD [ "python", "./Tutorial.py" ]

Step 3: Built Docker Image

Next, build the new Docker image using “Dockerfile”. To do so, check out the mentioned command. Here, the “-t” flag is specifying the image name:

> docker build -t docker-image .

Move toward the next step for verification.

Step 4: List Images

List down all Docker images to verify if the new image is created or not:

> docker images

The output shows that the image has been successfully created from Dockerfile:

Step 5: Run Image

Now, run the Docker image through the “docker run <image-name>” command. This command will automatically build and run the container for program deployment:

> docker run -it docker-image

Here, you can see we have successfully deployed a simple “Tutorial.py” Python program using Docker image:

We have taught you how to run Docker images using the “docker run image” command.

Conclusion

To run the Docker image, first, create a simple Dockerfile. Next, to build an image, utilize the “docker build” command. After that, execute the Docker image with the help of the “docker run <image-name>” command. The blog has demonstrated how to run the Docker image using the “docker run image” command.

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.