让dockerfile覆盖卷中的文件

时间:2020-02-22 21:49:59

标签: docker docker-compose dockerfile

我有一个Dockerfile,其中包含:

COPY config.xml /path/to/data/config.xml

运行容器时,我使用的卷本身包含一个config.xml文件

volume:
   - "/data:/path/to/data

构建和运行容器时,我想从映像config.xml优先(并覆盖)已安装卷中可能已经存在的副本。

这可能吗?

1 个答案:

答案 0 :(得分:1)

将卷添加到Docker服务时,该卷中的数据将覆盖Docker映像中的所有现有数据。如果要拥有可用作默认文件的Docker映像文件,则需要执行以下操作

  • 将文件存储在Docker映像预定义文件中。 (/路径/到/默认)
  • 在您的Docker文件中添加一个入口点,该入口点应负责将默认文件从/path/to/default复制到卷路径/path/to/data

Dockerfile

From ruby:2.4.5
COPY config.xml /path/to/default/config.xml
ENTRYPOINT  [ "/docker-entrypoint.sh" ]

docker-entrypoint.sh

#!/bin/sh -e

cp -r /path/to/default/config.xml /path/to/data
exec "$@" # or replace this by the command need to run the container