简体   繁体   English

docker cp不会将文件复制到容器中

[英]`docker cp` doesn't copy file into container

I have a dockerized project. 我有一个码头化项目。 I build, copy a file from the host system into the docker container, and then shell into the container to find that the file isn't there. 我进行构建,将文件从主机系统复制到docker容器中,然后将其shell放入容器中,以发现文件不存在。 How is docker cp supposed to work? 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:/#

So there was some mix-up with the names of images and containers. 因此,图像和容器的名称有些混淆。 Obviously, the cp operation was acting on a different container than I brought up with the run command. 显然,与运行命令所提起的cp操作所作用的容器不同。 In any case, the correct procedure is: 无论如何,正确的程序是:

# 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

You need to docker exec to get into your container, your command creates a new container. 您需要docker exec进入您的容器,您的命令创建了一个新容器。

I have this alias to get into the last created container with the shell of the container 我有这个别名可以使用容器的外壳进入最后创建的容器

alias exec_last='docker exec -it $(docker ps -lq) $(docker inspect -f {{'.Path'}} $(docker ps -lq))'

What docker version are you using? 您正在使用哪个docker版本? As per Docker 1.8 cp supports copying from host to container: 按照Docker 1.8 cp支持从主机复制到容器:

• Copy files from host to container: docker cp used to only copy files from a container out to the host, but it now works the other way round: docker cp foo.txt mycontainer:/foo.txt •将文件从主机复制到容器:docker cp过去仅用于将文件从容器复制到主机,但现在反过来又起作用:docker cp foo.txt mycontainer:/foo.txt

Please note the difference between images and containers. 请注意图像和容器之间的区别。 If you want that every container that you create from that Dockerfile contains that file (even if you don't copy afterward) you can use COPY and ADD in the Dockerfile. 如果您希望从该Dockerfile创建的每个容器都包含该文件(即使您以后不复制),也可以在Dockerfile中使用COPY和ADD。 If you want to copy the file after the container is created from the image, you can use the docker cp command in version 1.8. 如果要在从映像创建容器之后复制文件则可以使用1.8版中的docker cp命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM