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

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

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

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial