jcalloway
jcalloway

Reputation: 1155

docker cp doesn't work for this mysql container

Tried copying a directory and it doesn't seem to work.

Start a MySQL container.

docker cp mysql:/var/lib/mysql . 
cd mysql
ls 

NOTHING.

Here's the script to try it yourself.


extra info.

On Ubuntu 14.04

jc@dev:~/work/jenkins/copy-sql/mysql$ docker -v
Docker version 1.2.0, build fa7b24f

Upvotes: 2

Views: 3458

Answers (2)

Thomasleveil
Thomasleveil

Reputation: 104205

In the Dockerfile for the image your container comes from, there is the VOLUME instruction which tells Docker to leave the /var/lib/mysql directory out of the container filesystem.

The docker cp can only access the container filesystem and thus won't see the files in mounted volumes.

If you need to backup your mysql data, I suggest you follow the instructions from the Docker userguide in section Backup, restore, or migrate data volumes. You might also find the discordianfish/docker-backup docker image useful for that task.


Here's a little example to illustrate your case.

  • given a simple Dockerfile with just a VOLUME instruction

$ cat Dockerfile FROM base VOLUME /data

  • build an image named test

    $ docker build --force-rm -t test .
    
  • run a container named container_1 which will create two files, one being on the mounted volume

    $ docker run -d --name container_1 test bash -c 'echo foo > /data/foo.txt; echo bar > /tmp/bar.txt; while true; do sleep 1; done'
    
  • make sure the container is running

$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9e97aa18ac83 test:latest "bash -c 'echo foo > 3 seconds ago Up 2 seconds container_1

  • use the docker cp command to cp file /tmp/bar.txt and check its content

$ docker cp container_1:/tmp/bar.txt . $ cat bar.txt bar

  • try the same with the file which is in the mounted volume (won't work)

$ docker cp container_1:/data/foo.txt . 2014/09/27 00:03:43 Error response from daemon: Could not find the file /data/foo.txt in container container_1

  • now run a second container to print out the content of that file

$ docker run --rm --volumes-from container_1 base cat /data/foo.txt foo

Upvotes: 1

Gerrat
Gerrat

Reputation: 29730

It looks like you're trying to pass the name of your container to the docker cp command. The docs say it takes a container id. Try grepping for "CONTAINER ID" in your script instead.

EDIT:

Since changing your script to grep for the Container ID didn't help, you should start by trying this manually (outside of your script).

The docker cp command works. The reason it's not working for you is either:

  1. a permission thing
  2. you're not formatting the command correctly, or
  3. the directory doesn't exist in your container.

With a running container id of XXXX, try this (using your container id):

sudo docker cp XXXX:/var/lib/mysql .

If this doesn't work, and you don't get an error, I'd maybe suggest that that directory doesn't exist in your container.

EDIT2:

As I said, it's one of the 3 things above. I get this when I run your script:

2014/09/26 16:10:18 lchown mysql: operation not permitted

Changing the last line of your script to prefix with sudo now gives no errors, but no directory either.

Run the container interactively:

docker run -t -i mysql /bin/bash

Once inside the container:

cd /var/lib/mysql
ls

...no files.

So your script is working fine. The directory is just empty (basically #3 above).

For reference, the mysql Dockerfile is here.

Upvotes: 0

Related Questions