JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Running a Docker instance from a Dockerfile

Author:JIYIK Last Updated:2025/03/26 Views:

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application.

A Dockerfile is simply a read-only text document that sets the set of instructions when assembling a Docker image. A Docker image, on the other hand, is a set of instructions, application code, dependencies, tools, and libraries used to build a Docker container.

Therefore, a container is a runnable instance of a docker image assembled from a Dockerfile.

This article will walk you through the steps of creating a Dockerfile and running a docker instance from that file.

Steps to run a Docker instance from a Dockerfile

We need to follow the steps below to run the Docker instance from the Dockerfile.

Create a Dockerfile

To create a Dockerfile, we must understand its components. Some of the most common commands include:

  • FROM: Create a layer using the parent/base image.
  • WORKDIR: Allows us to set the working directory.
  • COPY: enables us to copy the contents of the current directory into a directory in the container.
  • PULL: Add files from your Docker repository.
  • RUN: Executed when we want to build an image.
  • CMD: Specifies the command to run when the container starts.
  • ENV: Defines environment variables used during the build.
  • ENTRYPOINT: Determines the command to run when the container starts.
  • MAINTAINER: Specifies the author of the image.

To create the Dockerfile, we will first create the main directory that will host the Dockerfile etc. We will make a simple Flask application that prints a message on the console.

mkdir my-app

Now, we go into that directory and create the main file of the application as app.py. This file contains the application code of the program.

from flask import Flask

app = Flask(__name__)

def hello():

    print("Hello, this is a simple Flask application")

hello()

We can now proceed with creating the Dockerfile and populating it with the necessary commands to create the Docker image.

touch Dockerfile

We have also created a requirements.txtfile that contains the installation files needed to run this application. The requirements are shown below.

click==8.0.4
Flask==2.0.3
gunicorn==20.1.0
itsdangerous==2.1.0
Jinja2==3.0.3
MarkupSafe==2.1.0
Werkzeug==2.0.3

We will edit the Dockerfile and add the following command to create a docker image using docker buildcommand. In this case, Python is the base image.

We also set the working directory and copy the necessary files from the current directory to the directory inside the Docker container.

#  base image
FROM python

# Set your working directory
WORKDIR /var/www/
# Copy the necessary files
COPY ./app.py /var/www/app.py
COPY ./requirements.txt /var/www/requirements.txt

# Install the necessary packages
RUN pip install -r /var/www/requirements.txt
# Run the app
CMD ["echo", "Hello, Developer"]

Creating a Docker Image

We continue by docker buildcreating the Docker image using the command. However, we must run this command in the same directory.

grammar:

$ docker build [OPTIONS] PATH | URL | -

In my-appthe directory, we will execute the following command. -tThe flag enables us to tag the name of the image and indicates that the Dockerfile is located in the same directory where we are executing this command.

~/my-app$ docker build -t new_image .

Output:

[+] Building 184.4s (10/10) FINISHED
 => [internal] load build definition from Dockerfile                                                                                       1.5s
 => => transferring dockerfile: 38B                                                                                                        0.0s
 => [internal] load .dockerignore                                                                                                          1.9s
 => => transferring context: 2B                                                                                                            0.0s
 => [internal] load metadata for docker.io/library/python:latest                                                                          50.8s
 => [1/5] FROM docker.io/library/python@sha256:3204faabc2f0b5e0939bdb8b29079a2a330c38dee92a22482a9ed449c5649a55                           30.4s
 => => resolve docker.io/library/python@sha256:3204faabc2f0b5e0939bdb8b29079a2a330c38dee92a22482a9ed449c5649a55                            0.4s
 => => sha256:3204faabc2f0b5e0939bdb8b29079a2a330c38dee92a22482a9ed449c5649a55 2.14kB / 2.14kB                                             0.0s
 => => sha256:17e2d81e5757980ee40742d77dd5d3e1a69ad0d6dacb13064e1b018a6664ec72 2.22kB / 2.22kB                                             0.0s
 => => sha256:178dcaa62b393b539abc8b866c39be81e8ade01786880dc5d17ce3fe02426dbb 8.55kB / 8.55kB                                             0.0s
 => => sha256:38121472aa0128f87b31fde5c07080418cc17b4a8ee224767b59e24c592ff7d3 2.34MB / 2.34MB                                            10.4s
 => => extracting sha256:38121472aa0128f87b31fde5c07080418cc17b4a8ee224767b59e24c592ff7d3                                                 14.6s
 => [internal] load build context                                                                                                          1.1s
 => => transferring context: 195B                                                                                                          0.0s
 => [2/5] WORKDIR /var/www/                                                                                                                3.2s
 => [3/5] COPY ./app.py /var/www/app.py                                                                                                    1.9s
 => [4/5] COPY ./requirements.txt /var/www/requirements.txt                                                                                2.6s
 => [5/5] RUN pip install -r /var/www/requirements.txt                                                                                    82.3s
 => exporting to image                                                                                                                     8.1s
 => => exporting layers                                                                                                                    6.0s
 => => writing image sha256:5811f24b498ae784af32935318a5fddba536e2be27233b19bf08cad81438d114                                               0.2s
 => => naming to docker.io/library/new_image

docker imagesWe can now list the docker images using the following command.

~/my-app$ docker images

Output:

REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
new_image            latest    5811f24b498a   2 minutes ago   929MB

Run the instance from the Dockerfile

To create a runnable instance from this docker image, we will use docker runthe command to create a writable layer on top of the image we created previously.

grammar:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Finally, we can now create a runnable instance using the command above.

~/my-app$ docker run -it new_image
Hello, Developer

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Copy files from host to Docker container

Publish Date:2025/03/25 Views:127 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Get the IP address of the Docker container

Publish Date:2025/03/25 Views:102 Category:Docker

This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con

Uninstalling Docker on macOS

Publish Date:2025/03/25 Views:95 Category:Docker

Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p

Enter the Docker container's shell

Publish Date:2025/03/25 Views:99 Category:Docker

This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t

Listing containers in Docker

Publish Date:2025/03/25 Views:123 Category:Docker

This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list

Mount the host directory into the Docker container

Publish Date:2025/03/25 Views:189 Category:Docker

Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories

Docker build command with multiple parameters

Publish Date:2025/03/25 Views:138 Category:Docker

docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha

The difference between CMD and ENTRYPOINT in Docker

Publish Date:2025/03/25 Views:142 Category:Docker

Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial