How to copy files to docker container from host and vica versa?

If you don’t want to install an editor in your docker container just to make a few small changes (e.g. change apache config etc.), you can just

docker cp <container>:/path/to/example.txt .

This command copies the file to your local machine (to your current directory). Then edit the file locally using your favorite editor, and copy it back to the container using the following command

docker cp example.txt <container>:/path/to/example.txt

to replace the old file.

Older docker versions

In older versions of Docker, the docker cp command only allowed copying files from a container to the host. From Docker 1.8 you can copy files from the host to a container.