Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`docker cp` doesn't copy file into container

Tags:

docker

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. How is docker cp supposed to work?

$ 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 <[email protected]>
 ---> 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:/#
like image 819
brandones Avatar asked Jan 07 '23 17:01

brandones


2 Answers

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. 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
like image 90
brandones Avatar answered Jan 17 '23 14:01

brandones


You need to docker exec to get into your container, your command creates a new container.

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))'

like image 24
user2915097 Avatar answered Jan 17 '23 13:01

user2915097