迹忆客 EN >

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

在 Docker-Compose 文件中重建容器

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

在我们实施我们的应用程序之前,我们需要了解 Dockerfile 和 compose.yaml 文件之间的区别。

在为我们的应用程序创建自定义图像时,我们通常使用名为 Dockerfile 的文件,并使用 docker run 命令在命令行上从中运行容器。 如果我们想创建多个镜像,这些命令会变得过于繁琐且难以管理容器,所以这就是 compose.yaml 文件的用武之地。

compose.yaml 文件构建图像、运行容器、为这些容器创建网络并将它们添加到网络中。 然后我们可以使用服务的名称访问网络应用程序。

这两个命令必须存在于应用程序中,以便 docker compose up 命令可以创建和运行图像。

如果对图像进行了任何更改,这些命令将获取更改、停止容器并重新创建图像。 在本教程中,我们将了解如何从 Docker 文件中定义的多个容器重建单个 Docker 容器。


创建一个新项目

打开 IntelliJ IDEA 并选择文件 > 新建 > 项目。 在打开的窗口中,选择 Node.js 并将项目名称从无标题更改为 docker-rebuild-container 或使用任何首选名称。

确保安装了 Node 运行时环境,以便从计算机自动添加 Node 解释器和包管理器部分。 最后,单击“创建”按钮生成项目。

在 docker-rebuild-container 文件夹下创建一个名为 index.js 的文件,并将以下代码复制并粘贴到该文件中。

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
    res.json(
        [

            {
                name: "Java in action",
                author: "chad darby",
                price: 345
            },
            {
                name: 'React cookbook',
                author: "mary public",
                price: 600
            },
        ])
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

在此文件中,我们创建了一个示例 Node.js 应用程序来创建图像并从中运行容器。 此应用程序公开了一个 API,可以通过向浏览器/在浏览器上发出获取请求来访问该 API。


为镜像创建 Dockerfile

创建一个名为 Dockerfile 的文件,然后将以下说明复制并粘贴到该文件中。

FROM node:16.17.0-alpine
WORKDIR /com/app
ADD package*.json ./
RUN npm install
ADD . .
CMD node index.js

此文件定义了构建映像并从中运行容器的说明。 docker 文档提供了这些说明如何工作的详细说明。


为容器创建一个 Compose 文件

在 docker-rebuild-container 文件夹中创建一个 compose.yaml 文件,并将以下说明复制并粘贴到该文件中。

services:
  service-one:
    restart: on-failure
    build: .
    hostname: service-one
    ports:
      - '3000:3000'
  service-two:
    restart: on-failure
    build: .
    hostname: service-two
    ports:
      - '5000:3000'

该文件定义了两个名为 service-one 和 service-two 的服务,分别从主机的 3000 端口和 5000 端口监听容器中的 3000 端口。

  1. services - 这定义了应用程序的计算组件。
  2. restart - 这定义了容器终止时要采取的操作。
  3. build - 这定义了包含用于构建映像的配置的 Dockerfile 的源。
  4. hostname - 这设置了服务使用的自定义名称。
  5. ports - 这会暴露主机上的端口来为应用程序提供服务。

Docker 文档提供了一个 docker compose 文件参考,详细解释了这些说明。


使用 compose 运行容器

在这个例子中,我们将看到如何一次构建和运行所有定义的容器。 要执行我们的 docker compose.yaml 文件,请使用键盘快捷键 ALT + F12 在您的开发环境中打开一个新的终端窗口,然后使用以下命令来构建和运行容器。

~/WebstormProjects/docker-rebuild-container$ docker compose up --build --force-recreate --no-deps -d

通过这个命令,运行和管理容器变得非常容易。 在使用此命令之前,我们运行 docker compose up -d 以确保我们正在使用此命令重建映像并重新创建容器。

 => CACHED [docker-rebuild-container_service-two 2/5] WORKDIR /com/app                                                          0.0s
 => CACHED [docker-rebuild-container_service-two 3/5] ADD package*.json ./                                                      0.0s
 => CACHED [docker-rebuild-container_service-two 4/5] RUN npm install                                                           0.0s
 => [docker-rebuild-container_service-two 5/5] ADD . .                                                                          1.2s
 => [docker-rebuild-container_service-one] exporting to image
  • --build - 这确保在运行容器之前构建图像。
  • --force-recreate - 这将重新创建容器而不考虑其配置和图像的状态。
  • --no-deps - 这确保链接的服务不会启动。

使用 compose 重建单个容器

要从 compose.yaml 文件中定义的多个容器重建单个容器,请使用上一个示例中使用的命令并添加要重建的服务的名称,如下所示。

~/WebstormProjects/docker-rebuild-container$ docker compose up --build --force-recreate --no-deps -d service-one

此命令重建映像并重新创建名为 service-one 的容器,因为我们只想重新创建一个容器。 我们可以从该命令的输出中验证仅重新创建了一个容器,如下所示。

[+] Running 1/1
 ⠿ Container docker-rebuild-container-service-one-1  Started

要验证容器是否按预期工作,请在浏览器上发出两个请求,一个在 localhost:3000 (http://localhost:3000/) 上,另一个在 localhost:5000 (http://localhost:5000/) 上,以及 请注意,我们可以从两个容器访问同一个应用程序。 以下是容器返回的 JSON 响应。

[
    {
    "name": "Java in action",
    "author": "chad darby",
    "price": 345
    },
    {
    "name": "React cookbook",
    "author": "mary public",
    "price": 600
    }
]

总结

在本文中,我们学习了如何使用 Dockerfile 定义图像、使用 compose.yaml 文件定义容器、在 compose 文件中运行容器以及在 compose 文件中重新创建单个容器。

请注意 ,从组合文件运行容器的语法是 docker compose up [OPTIONS] [SERVICE...] ,可选的 SERVICE 参数允许我们指定一个或多个我们想要重建其图像或重新创建其容器的服务。

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便