迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

从 Dockerfile 运行 Docker 实例

作者:迹忆客 最近更新:2023/03/22 浏览次数:

Docker 容器无疑成为了不同环境下管理软件和依赖关系的标准单元。在使用实际应用程序时,你必须在构建应用程序的容器映像之前创建一个 docker 文件。

Dockerfile 只是一个只读文本文档,在组装 Docker 映像时设置了指令集。另一方面,Docker 镜像是一组用于构建 Docker 容器的指令、应用程序代码、依赖项、工具和库。

因此,容器是由 Dockerfile 组装而成的 docker 镜像的可运行实例。

本文将引导你完成创建 Dockerfile 并从该文件运行 docker 实例的步骤。

从 Dockerfile 运行 Docker 实例的步骤

我们需要按照以下步骤从 Dockerfile 运行 Docker 实例。

创建一个 Dockerfile

要创建 Dockerfile,我们必须了解它的组件。一些最常见的命令包括:

  • FROM:创建使用的父/基础镜像的层。
  • WORKDIR:允许我们设置工作目录。
  • COPY:使我们能够将当前目录内容复制到容器中的目录中。
  • PULL:从你的 Docker 存储库添加文件。
  • RUN:当我们想要构建镜像时执行。
  • CMD:指定容器启动时要运行的命令。
  • ENV:定义构建期间使用的环境变量。
  • ENTRYPOINT:确定容器启动时要运行的命令。
  • MAINTAINER:指定镜像的作者。

要创建 Dockerfile,我们将首先创建将托管 Dockerfile 等文件的主目录。我们将制作一个简单的 Flask 应用程序,在控制台上打印一条消息。

mkdir my-app

现在,我们进入该目录并创建应用程序的主文件为 app.py。此文件包含程序的应用程序代码。

from flask import Flask

app = Flask(__name__)

def hello():

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

hello()

我们现在可以继续创建 Dockerfile 并使用必要的命令填充它以创建 Docker 映像。

touch Dockerfile

我们还创建了一个 requirements.txt 文件,其中包含运行此应用程序所需的安装文件。要求如下所示。

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

我们将编辑 Dockerfile 并添加以下命令以使用 docker build 命令创建 docker 映像。在这种情况下,Python 是基础镜像。

我们还设置了工作目录并将必要的文件从当前目录复制到 Docker 容器中的目录。

#  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"]

创建 Docker 映像

我们继续使用 docker build 命令创建 Docker 映像。但是,我们必须在同一目录中运行此命令。

语法:

$ docker build [OPTIONS] PATH | URL | -

my-app 目录中,我们将执行以下命令。 -t 标志使我们能够标记映像的名称并指示 Dockerfile 位于我们正在执行此命令的同一目录中。

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

输出:

[+] 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 images 命令列出 docker 镜像。

~/my-app$ docker images

输出:

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

从 Dockerfile 运行实例

要从这个 docker 镜像创建一个可运行的实例,我们将使用 docker run 命令在我们之前创建的镜像上创建一个可写层。

语法:

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

最后,我们现在可以使用上面的命令创建一个可运行的实例。

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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类: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

发布时间:2025/03/26 浏览次数:107 分类: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

发布时间:2025/03/26 浏览次数:97 分类: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

发布时间:2025/03/26 浏览次数:124 分类: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

发布时间:2025/03/26 浏览次数:202 分类: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

发布时间:2025/03/26 浏览次数:87 分类: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

Running a Docker instance from a Dockerfile

发布时间:2025/03/26 浏览次数:140 分类:Docker

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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便