Taking files off a docker container - docker

Is it possible to pull files off a docker container onto the local host?
I want to take certain directories off the docker containers I have worked on and move them to the local host on a daily basis.
Is this possible and how can it be done?

Yes you can, simply use the docker cp command.
An example from the official CLI documentation :
sudo docker cp <container-id>:/etc/hosts .

May I ask what's the reason you want to copy these files out of the container? If it's a one off thing then you're better off copying them as #abronan suggested, but if these directories are something that you'll be copying out and copying back into another container or the same container next time, you might want to look at volumes that enable you to have persistent data in your container as well as between containers

Related

Docker volume from existent container

I'm kind of stuck,
I have a docker container that is running, and that container runs some elasticsearch inside.
But I forgot to use volume on the first deploy. So my container has lots of data inside, in a single folder in /app/data.
I would like to use the same container but use volume on that folder, without losing data inside...
So it will be possible to rebuild other containers to use the same volume.
Have you some tips to share?
The important thing is not to remove your container, or you'll lose all that data. I think docker cp is your friend here (docs here). Copy the data to the host, then start another container with a volume.
Once you've secured your data, you can stop and remove the first container.

how to share an external file in the disk into a running docker container?

I am currently running a docker container with a specific folder shared to it. However, I need to access other folders which are not in the shared folder.
Does docker mount or docker volume help in any way to mount a file into a running docker container?
You can do this in two ways.
just copy the file and paste it the shared location, it will be available for use in docker mount location.
copy the host file to the container where you want, using
docker cp your_local_file containerid:/path/to/container
There are other ways around but that will not be that easy as these two approaches.
You can check this details article but I think it will take your handsome time to that each and every time.
mount-volumes-into-a-running-container
You can also check
docker-mount-dynamic-volumes/
I was thinking one alternative could be to use soft links by placing them inside your mounted volume but it won't work as documented in this post. A suggested alternative is to mount your folders as different volumes:
-v /home/test/:/home/test -v /mnt/mountedfile:/mnt/mountedfile

How to keep dot files in docker container?

I have installed some software in a docker image. When I run the software, it creates some setting files (dot files) under the root home folder. The problem is docker container wipes those files when I quit the container.
Is there a way to keep those dot files after I quite containers? I know I can manually save the container into a image. But that is not an elegant solution. That means every time I used the container, I need to save it to a image.
Any better solutions?
Thanks!
A simple solution would be to use a volume.
docker volume create configuration
And then you just run each container with it.
docker run -d -v configuration:container_configuration_dir your_image_name
Left side of : is name of volume created with first command and right side is dir inside container where your dot files are created.
Keep in mind how mounts work and for more details check docker docs on volumes.

How can I have shared assets (pictures, text documents, etc) between my Docker container and host system?

I have a Docker container and I am trying to make it so that all of the files in /var/www/ on the container will be saved on the host system at a location (/home/me), and vise-versa. Is it possible to have this shared space between the two?
Would you accomplish this with mount points, or is there a better method?
Thanks
You can use volumes for sharing between container and host.
docker run -v /home/me:/var/www <image>
If you have a fixed files/data, you can add to the image using dockerfile or committing after copying into container. If you want to share rw dir between host and container, you need to use the volumes. Your data will also be persisted even if you remove and recreate a new container.
There are three ways that you can do this
Use volumes. Official docs
Burn the files in your image. Basically include the creation of the files inside the Dockerfile. This means every container container from that image will have an initial state of sorts.
Use data-only containers. These are containers without a running process that contain the data that you need. This also uses volumes. But instead of mounting to the host, your containers mount on the data-only container (which in turn mounts on the host if you want to). This answer will be useful

Appropriate use of Volumes - to push files into container?

I was reading Project Atomic's guidance for images which states that the 2 main use cases for using a volume are:-
sharing data between containers
when writing large files to disk
I have neither of these use cases in my example using an Nginx image. I intended to mount a host directory as a volume in the path of the Nginx docroot in the container. This is so that I can push changes to a website's contents into the host rather then addressing the container. I feel it is easier to use this approach since I can - for example - just add my ssh key once to the host.
My question is, is this an appropriate use of a data volume and if not can anyone suggest an alternative approach to updating data inside a container?
One of the primary reasons for using Docker is to isolate your app from the server. This means you can run your container anywhere and get the same result. This is my main use case for it.
If you look at it from that point of view, having your container depend on files on the host machine for a deployed environment is counterproductive- running the same container on a different machine may result in different output.
If you do NOT care about that, and are just using docker to simplify the installation of nginx, then yes you can just use a volume from the host system.
Think about this though...
#Dockerfile
FROM nginx
ADD . /myfiles
#docker-compose.yml
web:
build: .
You could then use docker-machine to connect to your remote server and deploy a new version of your software with easy commands
docker-compose build
docker-compose up -d
even better, you could do
docker build -t me/myapp .
docker push me/myapp
and then deploy with
docker pull
docker run
There's a number of ways to achieve updating data in containers. Host volumes are a valid approach and probably the simplest way to achieve making your data available.
You can also copy files into and out of a container from the host. You may need to commit afterwards if you are stopping and removing the running web host container at all.
docker cp /src/www webserver:/www
You can copy files into a docker image build from your Dockerfile, which is the same process as above (copy and commit). Then restart the webserver container from the new image.
COPY /src/www /www
But I think the host volume is a good choice.
docker run -v /src/www:/www webserver command
Docker data containers are also an option for mounted volumes but they don't solve your immediate problem of copying data into your data container.
If you ever find yourself thinking "I need to ssh into this container", you are probably doing it wrong.
Not sure if I fully understand your request. But why you need do that to push files into Nginx container.
Manage volume in separate docker container, that's my suggestion and recommend by Docker.io
Data volumes
A data volume is a specially-designated directory within one or more containers that bypasses the Union File System. Data volumes provide several useful features for persistent or shared data:
Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization.
Data volumes can be shared and reused among containers.
Changes to a data volume are made directly.
Changes to a data volume will not be included when you update an image.
Data volumes persist even if the container itself is deleted.
refer: Manage data in containers
As said, one of the main reasons to use docker is to achieve always the same result. A best practice is to use a data only container.
With docker inspect <container_name> you can know the path of the volume on the host and update data manually, but this is not recommended;
or you can retrieve data from an external source, like a git repository

Resources