【问题标题】:Docker does not create a new container when using docker-compose buildDocker 使用 docker-compose build 时不会创建新容器
【发布时间】:2019-05-16 09:17:10
【问题描述】:

我为 ASP.NET 和 MSSQL 服务器设置了两个 windows 容器。在第一个 docker-compose build 上,一切都按预期工作。然后,在我对自定义 dockerfile 进行了一些更改并再次运行 docker-compose build 之后,它再次使用旧容器,而不进行任何更改。

我假设当我进行构建时它创建了一个新容器。我是否误解了 docker 的工作原理?

这是 docker-compose.yml

version: '3'
services:
  db:
    image: microsoft/mssql-server-windows-developer

    environment:
      sa_password: "Password1234!"
      ACCEPT_EULA: "Y"

    ports:
      - "8003:1433"
    build:
      context: .
      dockerfile: mssql.dockerfile

  web:
    build:
      context: .
      dockerfile: web.dockerfile
    image: mcr.microsoft.com/dotnet/framework/aspnet:4.8
    #volumes:
    #  - .:C:/inetpub/wwwroot
    ports: 
      - "8080:80"
      - "8081:431"

这是 mssql.docker 文件

# escape=`

FROM microsoft/mssql-server-windows-developer

#set shell
SHELL ["powershell.exe", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

#make temp folder
RUN mkdir C:\temp

#copy script to temp folder
COPY DownloadDatabase.ps1 C:\temp
COPY RestoreDatabase.ps1 C:\temp

#run script to retrieve production database
WORKDIR C:\temp

RUN .\DownloadDatabase.ps1 -sourcefile <url> -destinationfile <target>

CMD .\RestoreDatabase.ps1

很容易判断图像是否已被重新使用,因为 mkdir C:\temp 错误提示该目录已存在。

编辑:我已经尝试了 docker compose 上的所有选项。无缓存,强制rm

【问题讨论】:

    标签: windows powershell docker


    【解决方案1】:
    docker-compose build 
    

    只构建镜像,但不启动容器。

    这就是您在 dockerfile 中所做的更改未应用的原因。您已经重建了映像,但没有重建容器。这就是之前启动的容器是基于旧版本镜像的原因。

    docker-compose up 
    

    来自 Docker documentation

    如果服务存在现有容器,并且服务的配置或映像在容器创建后发生更改,docker-compose up 通过停止和重新创建容器(保留已安装的卷)来获取更改。要防止 Compose 获取更改,请使用 --no-recreate 标志。

    为了让你的镜像和容器都被重建,你必须添加这个标志:

    docker-compose up --force-recreate --build
    

    这样您的容器将基于正确的映像版本。

    对来自 Docker documentation 的标志的解释:

    --build                   Build images before starting containers.
    
    --force-recreate          Recreate containers even if their configuration
                              and image haven't changed.
    

    如果您想为特定服务执行此操作,只需在命令行末尾添加服务名称:

    docker-compose up --force-recreate --build serviceName
    

    如果您想要清晰的输出,另一个有用的标志是-d 标志:

     -d, --detach              Detached mode: Run containers in the background,
                               print new container names. Incompatible with
    

    【讨论】:

    • 我尝试使用 docker-compose up --force-recreate --build 重建图像,但这给了我与构建时相同的错误:Step 3/7 : RUN mkdir C:\temp ---&gt; Running in ebc12e0b9e33 mkdir : An item with he specified name C:\temp already exists. 每当我尝试重建图像时,它都会给我错误目录已经存在。哪个(重建时)不应该存在?
    • @LlamaNL 否,如果在构建映像时出现错误,则必须修复它。每次重建时都会出现该错误。据我了解,您构建的图像没有进行任何修改。然后您自定义了 dockerfile 并在步骤 3 RUN mkdir C:\temp 处构建失败。您还尝试使用 --no-cache 标志来确保错误未链接到缓存。这听起来可能很愚蠢,但尝试在不创建临时文件夹的情况下重建图像。似乎图像已经有临时文件夹:github.com/microsoft/mssql-docker/tree/master/windows/…
    • 就是这样,第一个构建工作正常。当我尝试再次构建时,它会出错
    【解决方案2】:

    事实证明,我只需在 docker-compose build 之前执行 docker-compose pull 即可刷新 dockerfile!现在它每次都建立一个新的图像!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 2023-01-22
      相关资源
      最近更新 更多