将目录从主机复制到 Docker 镜像
Docker 文档将 Docker 定义为独立于基础架构的工具,可帮助开发人员以最快的方式开发、发布和运行应用程序。
除了这些好处之外,DevOps 开发人员还在持续集成、交付和部署中使用 Docker。 这种开发方法通常称为 CI/CD,开发人员使用容器来开发、测试并将更改或功能部署到生产中。
应用程序中的错误在开发环境中得到修复。 完成后,将在测试环境中执行自动化测试。 最后,当所有测试都通过后,具有新更改的图像将部署到生产环境中。
本教程将教我们如何将目录从主机复制到 docker 镜像。 我们将使用一个返回 API 的快速应用程序来实现这一点。
创建一个快速应用程序
打开 Intellij IDEA。 选择文件 -> 新建 -> 项目。 在打开的窗口中,选择左侧的 Node.js 并将项目名称从 untitled 更改为 employee-api。 最后,点击创建按钮生成一个项目。
要在我们的应用程序中安装 Express,请通过选择 View > Tool Windows > Terminal 在 Intellij IDEA 上打开一个新的终端窗口,然后使用以下命令安装 Express。
~/WebstormProjects/employee-api$ npm install express
安装 express 后,在文件夹 employee-api 中创建一个名为 main-app 的文件夹。 然后,要创建我们的 API,请在文件夹 main-app 中创建一个名为 index.js 的文件,并将以下代码栏复制并粘贴到该文件中。
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.json(
[{
name: 'Bob',
email: "bob@gmail.com"
},
{
name: 'Alice',
email: 'alice@gmail.com'
},
])
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
在这个文件中,我们创建了一个暴露端口 3000 的 express 应用程序,任何对路径 / 的请求都会返回一个员工数组。 我们将在以下部分中介绍的所有示例中使用此应用程序。
使用 ADD 指令
在我们的基本文件夹 employee-api 中创建一个名为 Dockerfile 的文件,并将以下代码复制并粘贴到该文件中。
FROM node:16.17.0-alpine
WORKDIR /com/employee
ADD package*.json ./
RUN npm install
ADD main-app /com/employee/main-app
CMD node main-app/index.js
要创建应用程序的自定义映像,我们通常使用名为 Dockerfile 的文件,如本例所示。 以下部分定义了文件中使用的每个命令的含义。 我们关心的是倒数第二行的 ADD 命令。
前面代码示例中使用的所有命令简要说明如下:
- FROM - 在 Dockerfile 中声明的第一个命令,其主要目的是声明我们将用于构建镜像的基础镜像。 请注意,基本图像是从公共存储库中提取的。
- WORKDIR - 设置工作目录,即使我们在其他指令中没有设置,也会创建工作目录。
- ADD - 将文件或文件夹从源复制到目标。 源通常是主机,目标是容器的文件系统。
- RUN - 在现有图像之上执行提供的命令并返回包含执行结果的新图像。 在这种情况下,该命令会在当前映像上安装依赖项。 并且生成的图像配备了依赖项。
- CMD - 当我们启动一个容器时,该指令提供容器的默认值,包括可执行文件或入口点指令。 在本例中,我们使用了指令的 shell 形式。 此外,我们提供了一个可执行文件作为我们容器的默认值。
请注意
,我们已使用 ADD 指令将 main-app 目录从我们的主机复制到在我们的工作目录 /com/employee/main-app 中创建的新目录。 要查看实际效果,请使用以下命令创建一个带有标签 employee-api 的图像。
~/WebstormProjects/employee-api$ docker build --tag employee-api:latest.
观察控制台上的前五个指令,直到构建成功。 然后,确保您可以看到正在执行的指令,如下所示。
=> [1/5] FROM docker.io/library/node:16.17.0-alpine@sha256:2c405ed42fc0fd6aacbe5730042640450e5ec030bada7617beac88f742b6997b 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 137B 0.0s
=> CACHED [2/5] WORKDIR /com/employee 0.0s
=> CACHED [3/5] ADD package*.json ./ 0.0s
=> CACHED [4/5] RUN npm install 0.0s
=> CACHED [5/5] ADD main-app /com/employee/main-app
当我们从我们的图像运行一个容器时,最后一条指令被执行。 例如,使用以下命令从我们的镜像运行一个容器。
~/WebstormProjects/employee-api$ docker run --name employee-service -d -p 3000:3000 employee-api:latest
此命令在分离模式下使用我们的图像 employee-api 运行名为 employee-service 的容器 -d 并将端口 -p 3000 映射到本地主机上的端口 3000。
要验证我们的应用程序是否按预期工作,请转到 localhost:3000 http://localhost:3000/
以获取员工数据。 以下是请求返回的 JSON 数组。
[
{
"name": "Bob",
"email": "bob@gmail.com"
},
{
"name": "Alice",
"email": "alice@gmail.com"
}
]
使用 COPY 指令
对名为Dockerfile的文件中的说明进行注释,将以下代码复制粘贴到注释后的文件中。
FROM node:16.17.0-alpine
WORKDIR /com/employee
ADD package*.json ./
RUN npm install
COPY main-app /com/employee/main-app
CMD node main-app/index.js
请注意
,这些说明与前面示例中的说明相同。 我们所做的唯一更改是将倒数第二行中的 ADD 指令替换为 COPY 指令。
COPY - 将文件或文件夹从源复制到目标。 源通常是主机,目标是容器的文件系统。
请注意
,COPY 指令类似于 ADD 指令,因为它们实现了相同的目标。 要查看实际效果,请使用我们在上一个示例中使用的命令来创建映像并从映像运行容器。
我们可以创建新图像或重新创建现有图像。 要重新创建现有镜像,我们需要停止正在运行的容器,移除容器,然后移除镜像。 使用以下命令分别执行步骤。
~/WebstormProjects/employee-api$ docker stop employee-service
~/WebstormProjects/employee-api$ docker rm -f employee-service
~/WebstormProjects/employee-api$ docker rmi -f employee-api:latest
使用 ADD 指令的替代方法
对名为Dockerfile的文件中的说明进行注释,将以下代码复制粘贴到注释后的文件中。
FROM node:16.17.0-alpine
WORKDIR /com/employee
ADD package*.json ./
RUN npm install
ADD . .
CMD node main-app/index.js
请注意
,此示例与前面的示例类似,我们所做的唯一更改是将 COPY 指令替换为 ADD 指令。 本例中的 ADD 指令使用两个点。 . 指定源和目标。
表示我们要将当前目录下的所有内容复制到工作目录下。 因此,例如,使用此命令,我们可以将 main-app 文件夹及其内容添加到映像中的工作目录 /com/employee,这比其他示例容易得多。
我们可以使用 .dockerignore 文件来添加我们不想添加到镜像中的文件和文件夹。 然后,使用我们在前面示例中使用的命令来构建映像并从映像运行容器。
输出仍然相同,因为我们没有更改 express 应用程序中的任何代码。
所以,我们已经学习了如何将目录从主机复制到我们图像的文件系统。 我们还可以使用 ADD 和 COPY 指令将一个目录复制到另一个目录。
我们还可以使用带有两个点 COPY 的 COPY 指令。 .
它仍然会产生同样的效果。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
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