How to Copy File to Stopped Docker Container

How to list files in a stopped Docker container

This answer to another question shows how to start a stopped container with another command. Here are the commands to list files in a stopped container.

  1. Commit the stopped container to a new image: test_image.

    • docker commit $CONTAINER_ID test_image
  2. Run the new image in a new container with a shell.

    • docker run -ti --entrypoint=sh test_image
  3. Run the list file command in the new container.

    • docker exec --privileged $NEW_CONTAINER_ID ls -1 /var/log

Docker: Copying files from Docker container to host

In order to copy a file from a container to the host, you can use the command

docker cp <containerId>:/file/path/within/container /host/path/target

Here's an example:

$ sudo docker cp goofy_roentgen:/out_read.jpg .

Here goofy_roentgen is the container name I got from the following command:

$ sudo docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b4ad9311e93 bamos/openface "/bin/bash" 33 minutes ago Up 33 minutes 0.0.0.0:8000->8000/tcp, 0.0.0.0:9000->9000/tcp goofy_roentgen

You can also use (part of) the Container ID. The following command is equivalent to the first

$ sudo docker cp 1b4a:/out_read.jpg .

Copy files from container to local in Docker

If the output file is stored in it's own directory (say /app/output) you can run: docker run -d -it -v $PWD/output:/app/output/ --name test [image] and the file will be in the output directory of the current directory.

If it's not, then run the container with: docker run -d -it --name test [image]

Then copy the file to your own filesystem using docker cp test:/app/example.json . to copy it to the current directory.

Before stopping the docker-container copy one file and store to the host

You can have your output sent directly to your host. You have to do it at the moment of running your docker image. Here is the step to do it:

  1. Create a folder on your desktop for example and put your inputs data in it. Call it input_dir. The full path of this folder will have it like /path/to/input_dir/ (you can get it by going inside this folder and type on terminal pwd).
  2. Create another folder for the output of your script on your host machine in the desktop for example. Call it output_dir. The full path of this folder is: path/to/output_dir
  3. Running your docker image should be like this:

     docker run -it -v /path/to/input_dir/:/data/ -v /path/to/output_dir/:/data/output/ my-image bash

Once done, your inputs will be automatically available in /data/input and make sure you set your output directory to '/data/output'

when you finish running your script, you will find your output on your host machine on the folder: /path/to/output_dir/

How to edit files in stopped/not starting docker container

Answering my own question.. still hoping for a better answer from a more knowledgable person!!

There are 2 possibilities.

1) Editing file system on host directly. This is somewhat dangerous and has a chance of completely breaking the container, possibly other data depending on what goes wrong.

2) Changing the startup script to something that never fails like starting a bash, doing the fixes/edits and then changing the startup program again to the desired one (like node or whatever it was before).

More details:

1) Using

docker ps

to find the running containers or

docker ps -a

to find all containers (including stopped ones) and

docker inspect (containername)

look for the "Id", one of the first values.

This is the part that contains implementation detail and might change, be aware that you may lose your container this way.

Go to

/var/lib/docker/aufs/diff/9bc343a9..(long container id)/

and there you will find all files that are changed towards the image the container is based upon. You can overwrite files, add or edit files.

Again, I would not recommend this.

2) As is described at https://stackoverflow.com/a/32353134/586754 you can find the configuration json config.json at a path like

/var/lib/docker/containers/9bc343a99..(long container id)/config.json

There you can change the args from e. g. "nodejs app.js" to "/bin/bash". Now restart the docker service and start the container (you should see that it now correctly starts up). You should use

docker start -i (containername)

to make sure it does not quit straight away. You can now work with the container and/or later attach with

docker exec -ti (containername) /bin/bash

Also, docker cp is rather useful for copying files that were edited outside of the container.

Also, one should only fall back to those measures if the container is more or less "lost" anyway, so any change would be an improvement.

remove file from stopped docker container (without create new image)

Answering by myself using hints from another answer

  1. Find where directory stored on docker host:

    export LOCAL_DIR=$(docker inspect -f '{{ .GraphDriver.Data.UpperDir }}' container_name)
  2. Remove file locally:

    sudo rm -f ${LOCAL_DIR}/run/service.pid
  3. Run container:

    docker start container_name

Or all in one:

sudo rm -f "$(docker inspect -f '{{ .GraphDriver.Data.UpperDir }}' container_name)/run/service.pid" && docker start container_name

How do I inspect the stopped docker container files

You can start container with specific entrypoint

docker run --entrypoint sleep YOUR_IMAGE 3600

It will block current terminal for 3600 seconds. You can open new terminal tab(do not close current one) and you can verify if your container is working with the

docker ps

If you do not want to block current terminal, you can add -d flag to docker run:

docker run -d --entrypoint sleep YOUR_IMAGE 3600

Above command will start docker which will be doing nothing, then you can ssh into the container when it is working with

docker exec -ti CONTAINER HASH sh


Related Topics



Leave a reply



Submit