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

标签 docker

我有一个 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/

相关文章:

docker - 无法从 Laravel 应用程序连接到 Redis

Docker - 初始化,僵尸 - 为什么重要?

amazon-web-services - AWS EC2 : Cannot reach port 80 or 443 (https) using letsencrypt and nginx via docker

docker - 如何使用 docker swarm 保持粘性 session ( session 持久性)?

docker - 如何更改Docker镜像

docker - 如何保存 Docker 容器状态

mysql - docker 和 mysql : Got an error reading communication packets

jenkins - 将Docker镜像推送到Jenkinsfile中的注册表时出错

docker - 如何为docker创建覆盖网络,获取范围 "global"的错误数据存储未初始化

mysql - 无法使用服务名称连接到 docker-compose 中定义的 mysql 容器