我有一个 dockerized 项目。我构建,将文件从主机系统复制到 docker 容器中,然后 shell 到容器中,发现该文件不存在。 docker cp
应该如何工作?
$ docker build -q -t foo .
Sending build context to Docker daemon 64 kB
Step 0 : FROM ubuntu:14.04
---> 2d24f826cb16
Step 1 : MAINTAINER Brandon Istenes <redacted@email.com>
---> Using cache
---> f53a163ef8ce
Step 2 : RUN apt-get update
---> Using cache
---> 32b06b4131d4
Successfully built 32b06b4131d4
$ docker cp ~/.ssh/known_hosts foo:/root/.ssh/known_hosts
$ docker run -it foo bash
WARNING: Your kernel does not support memory swappiness capabilities, memory swappiness discarded.
root@421fc2866b14:/# ls /root/.ssh
root@421fc2866b14:/#
最佳答案
因此,图像和容器的名称有些混淆。显然,cp 操作作用于与我使用 run 命令不同的容器。无论如何,正确的程序是:
# Build the image, call it foo-build
docker build -q -t foo-build .
# Create a container from the image called foo-tmp
docker create --name foo-tmp foo-build
# Run the copy command on the container
docker cp /src/path foo-tmp:/dest/path
# Commit the container as a new image
docker commit foo-tmp foo
# The new image will have the files
docker run foo ls /dest
关于docker - `docker cp` 不会将文件复制到容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32112035/