在启动之前将文件复制到Docker容器

在启动之前将文件复制到Docker容器

问题描述:

我知道您可以使用 docker cp 从主机将文件复制到正在运行的Docker容器,但就我而言,我想在启动Docker容器之前将其复制。我怎样才能做到这一点?

I know that you can copy a file from host to a running Docker container using docker cp, but in my case I want to copy it just before starting the docker container. How can I do that?

请注意,我不想在构建的docker映像中使用此文件,因此即使在构建时也无法复制该文件。另一种方法是使用挂载的卷(与 -v 一起使用),但我也不想要。

Note that I don't want this file in my built docker image, so I cannot copy it at build time even. Another way is to use mounted volumes (with -v), but I don't want that either.

您可以执行以下操作:

docker create mycon
docker cp /myfiles/file mycon:/path/file
docker start mycon