docker cp from host to container is not working - docker

I am new to docker concept and trying to copy some file from my host to the container. Assuming that my docker name is my_docker when I run the following:
docker cp my_docker:/usr/local/src/test.txt test.txt
It copies the test.txt file from my container to the local host. But doing it the other way around is not working. Here is the command:
docker cp test.txt my_docker:/usr/local/src
Is there something that I am doing run? Any help would be much appreciated.
Thanks

As ahajib mentioned in the comments, you're looking for the file you copied in a different container.
$ docker run --name my_container alpine
$ docker cp test.txt my_container:/tmp/test.txt
$ docker run alpine ls -l /tmp
total 0
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6224c947fbbd alpine "ls -l /tmp" 11 seconds ago Exited (0) 10 seconds ago brave_neumann
22951689a3e4 alpine "/bin/sh" About a minute ago Exited (0) About a minute ago my_container
You copied the file into my_container, but you ran the ls -l /tmp command in brave_neumann.
If you want to copy files into a container and then use those files in the container, you either have to copy the files while the container is still running, or you copy them to a docker volume.
Mounting that volume in a new container then lets it see the files you copied in.
$ docker volume create my_volume
my_volume
$ docker run --name my_container2 -v my_volume:/data alpine echo OK
OK
$ docker cp test.txt my_container2:/data/test.txt
$ docker run -v my_volume:/data alpine ls -l /data
total 4
-rw-r--r-- 1 1006 1006 29 Oct 6 19:43 test.txt
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15b36ccd535f alpine "ls -l /data" 7 seconds ago Exited (0) 6 seconds ago agitated_khorana
b9d4c9e0902f alpine "echo OK" 54 seconds ago Exited (0) 53 seconds ago my_container2
6224c947fbbd alpine "ls -l /tmp" 2 minutes ago Exited (0) 2 minutes ago brave_neumann
22951689a3e4 alpine "/bin/sh" 3 minutes ago Exited (0) 3 minutes ago my_container
$

In your second command you appear to be trying to copy test.txt into /usr/local with the name src. Try adding a / to the end of the destination path:
docker cp test.txt my_docker:/usr/local/src/
to copy with the file name or: docker cp test.txt my_docker:/usr/local/src/newname.txt to rename it in the container.

Related

does docker ignore the -ti flags if -d is used?

I ran this command:
docker run -d -ti foo
it works, but I realized that I probably forgot to remote the -ti part.
I assume that docker ignores those flags if -d is used, does anyone know?
It seems like -ti and -d would contradict each other?
It still sets up the input filehandle, and allocates a pseudo tty for the container. If the app inside the container attempts to read from stdin, it will hang waiting on input rather than exit immediately or fail. Later on, you can attach to that process. E.g.
$ docker run -dit --name test-dit busybox sh
f0e057ce47e03eb227aacb42e3a358b14fa5d8b26ad490fcec7cbfe0cd3cce73
$ docker run -d --name test-d busybox sh
4f2583d3380953f328b702c88884fbe55f16c44bce13dbccc00c4bb81f3270f2
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f2583d33809 busybox "sh" 5 seconds ago Exited (0) 4 seconds ago test-d
f0e057ce47e0 busybox "sh" 14 seconds ago Up 13 seconds test-dit
$ docker container attach test-dit
/ #
/ # ls
bin dev etc home proc root sys tmp usr var
/ # exit
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f2583d33809 busybox "sh" 22 seconds ago Exited (0) 21 seconds ago test-d
f0e057ce47e0 busybox "sh" 31 seconds ago Exited (0) 2 seconds ago test-dit
In the first container ls command, you can see the shell without the -it option immediately exited, while the one with -it was available to connect and run commands.
It does not ignore -ti.
The -ti part means it enables direct user interaction, and the -d part means it detaches the container the moment it gets started. So, in order to actually interact with it, you'll have to do
?> docker attach foo
So, yes, it might not be very useful, but it neither causes an impossible situation, nor one you cannot get out of.

Create new image based on standard one

I have installed Docker and have running some Ubuntu image with command:
sudo docker run ubuntu
I would like to create some text file on it and find it next time the same image will run. How to achieve that?
UPD.
Got problems with attaching to docker.
I have running docker
docker ps -a
aef01293fdc9 ubuntu "/bin/bash" 6 hours ago Up 6 hours priceless_ramanujan
Since it is Up mode, I suppose I don't need to execute command:
docker start priceless_ramanujan
So, I run command attach
docker attach priceless_ramanujan
And got nothing in output while command not returns.
Why I can't get to container's bash?
Simple example:
$ docker run -it ubuntu
root#4d5643e8c1a8:/# echo "test" > test.txt
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/# exit
exit
$ docker run -it ubuntu
root#cdb44750bffc:/# cat test.txt
cat: test.txt: No such file or directory
root#cdb44750bffc:/#
docker run image_name
This command creates and starts a new container based on the provided image_name. If a name is not set for the container, a random one is generated and assigned by docker. In the above example 2 containers were created based on ubuntu.
with docker ps -a we can see that modest_jennings and optimistic_leakey are the random names created:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdb44750bffc ubuntu "/bin/bash" About a minute ago Exited (1) 4 seconds ago optimistic_leakey
4d5643e8c1a8 ubuntu "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago modest_jennings
cat test.txt failed the 2nd time because the file didn't exist. The container started from a "clean" ubuntu image.
Actually, we created test.txt inside modest_jennings only.
docker start container_name
This command starts a stopped container. So, in our case, the file is still there:
$ docker start modest_jennings
modest_jennings
$ docker attach modest_jennings
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/#
docker commit container_name image_name
This command is to create a new image, so that you can use it later and run containers based on that image. Continuing our example...
$ docker commit modest_jennings my_ubuntu
sha256:a4357f37153ac0b94e37315595f1a3b540538283adc3721df4d4e3b39bf8334f
$ docker run -it my_ubuntu
root#2e38616d532a:/# cat test.txt
test
root#2e38616d532a:/#
If you want a custom image, you can create a Dockerfile
`FROM ubuntu:16.04
ADD ./test.txt /tmp/`
after you can build it docker build -t ubuntu:custom .
and finally run your custom image docker run --name myubuntu ubuntu:custom sleep 3000
You can check your file with docker exec -it myubuntu /bin/bash and more /tmp/test.txt

Can I maintain the state of a docker container without commiting

I have a docker container running and would like to know if I can save its state without commiting it.
For example:
1. Start container
2. Create a new file inside it
3. Exit container
4. Start container
Can the file still exist in this container without running docker commit before exiting.
Docker's stopped, exited Containers, Maintain files and changes in container's writable AUFS layer. Please note this layer will be removed when the container is removed.
POC:
sudo docker run -it --name test debian:jessie /bin/bash
root#3d01feb251bd:/# touch farhad
root#3d01feb251bd:/# exit
sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d01feb251bd debian:jessie "/bin/bash" 16 seconds ago Exited (0) 7 seconds ago test
sudo docker start test
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d01feb251bd debian:jessie "/bin/bash" 31 seconds ago Up 8 seconds test
sudo docker exec -it test /bin/bash
root#3d01feb251bd:/# ls
bin boot dev etc farhad home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
As you can see the file I touched before exiting the container is still there.

Cannot mount data volume in Docker with Windows

I am running Docker on Windows. I have succesfully build an image with command:
docker build -t my/tag /c/Users/user/Docker/docker-php/image/
docker images result:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
my/tag latest 763a2f01c832 28 minutes ago 445.8 MB
php 5.6-cli 32d2bd1d7a19 6 days ago 444.5 MB
Unfortunately I have a problem, when I am trying to mount data volume to image. I run:
docker run --rm -it -v /``pwd``:/src -w /src my/tag bash
(There is single backtick on pwd, but I don't knwo how to escape it.)
But files aren't visible in docker:
root#3e91981ef517:/src# ls -al
total 4
drwxr-xr-x 2 root root 40 Aug 31 18:59 .
drwxr-xr-x 47 root root 4096 Aug 31 19:15 ..
I have tried with different approach, such as:
docker run --rm -it -v /``pwd``:/src -w /src my/tag bash
(There is single backtick on pwd, but I don't knwo how to escape it.)
docker run --rm -it -v /full/path/to/sources:/src -w /src my/tag bash
docker run --rm -it -v //full/path/to/sources:/src -w /src my/tag bash
But every time the same result.

How to start a docker container (ubuntu image)

How to stat a docker container. I had created it using
docker run -d -P -v /Users/bsr:/usr/local/users --name test ubuntu
I do have virtual box guest addition installed, and mounting works. But, I am not sure why I can't I keep the shell running.
bsr[~/tmp/web] $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf620ff6c36a ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 minutes ago test
8213c8d49842 nginx:latest "nginx" 3 hours ago Up About an hour 0.0.0.0:49154->80/tcp web
bsr[~/tmp/web] $ docker start test
test
bsr[~/tmp/web] $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf620ff6c36a ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 seconds ago test
8213c8d49842 nginx:latest "nginx" 3 hours ago Up About an hour 0.0.0.0:49154->80/tcp web
bsr[~/tmp/web] $
Edit:
it may be because the command (/bin/bash ??) finishes immediately. When I tried,
docker run -d -P -v /Users/bsr:/usr/local/users --name test5 ubuntu /bin/bash -c "while true; do echo Hello world; sleep 1; done"
I could get the terminal. But isn't there any way to just start a container and get to the terminal ??
If you want to run an interactive process, you should use the -i (keep stdin open in case you detach) and -t (allocate a pseudo-tty) flags:
docker run -it ubuntu
You can look at the docs for more information on those flags and their usage.
You can start by using simple command.
docker run "CONTAINER_ID"

Resources