Docker

Run Your Docker Image as a Container

Docker is the more effective tool for project development and deployment due to its containerization concept. The major components of Docker are Docker containers, Docker Engine, Docker registries, Docker server/client, and Docker images. More specifically, the Docker images are the file to instruct the container on deploying the project. However, the developers can run the Docker images as a container for project deployment.

This blog will demonstrate the method for running an image as a container.

How to Run an Image as a Container?

To execute the image as a Docker container, developers can utilize the “docker run” command. To create and run a Docker image, go through the given instructions.

Step 1: Open Visual Studio Code Editor
First, launch the Visual Studio Code app from the Windows “Startup” menu:

Step 2: Open Project Directory
From the “Files” menu, launch the project directory by clicking on the “Open Folder” option:

Select the folder and press the “Select Folder” button:

Step 3: Create Dockerfile
Next, click on the below-highlighted icon to create a new file and set the name as “Dockerfile”:

After that, paste the following code inside the Dockerfile to deploy the simple Python program:

FROM python:3.6

RUN apt-get update && apt-get install -y --no-install-recommends \
    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')"

Step 4: Create Docker Image
Next, generate the new image through the newly created Dockerfile using the “docker build” command. Here, the “-t” option is used to specify the image name:

$ docker build -t pythonimage .

Step 5: View Docker Images
To view all Docker images, the “docker images” command will be used:

$ docker images

Here, you can see we have successfully generated the new “pythonimage” Docker image:

Step 6: Run Docker Image as a Container
To run the Docker images as a container, take a look at the provided command. The “-i” option is used to run Docker image interactive mode (Keep STDIN open), and the “-t” option is utilized to allocate a pseudo-TTY terminal inside the container:

$ docker run -i -t pythonimage

We have demonstrated the procedure for executing the Docker image as a container.

Conclusion

To run the Docker image as a Container, first, create a new Docker file. Then, generate a Docker image with the help of the “docker build -t <image-name> .” command. Next, in order to process the image as a container, utilize the “docker run -i -t <image-name>” command. This blog has demonstrated the method for executing the Docker image as a container.

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.