75

I am working on a website powered by Node. So I have made a simple Dockerfile that adds my site's files to the container's FS, installs Node and runs the app when I run the container, exposing the private port 80.

But if I want to change a file for that app, I have rebuild the container image and re-run it. That takes some seconds.

Is there an easy way to have some sort of "live sync", NFS like, to have my host system's app files be in sync with the ones from the running container?

This way I only have to relaunch it to have changes apply, or even better, if I use something like supervisor, it will be done automatically.

1

4 Answers 4

60

You can use volumes in order to do this. You have two options:

  1. Docker managed volumes:

    docker run -v /src/path nodejsapp docker run -i -t -volumes-from <container id> bash 

The file you edit in the second container will update the first one.

  1. Host directory volume:

    docker run -v `pwd`/host/src/path:/container/src/path nodejsapp 

The changes you make on the host will update the container.

Sign up to request clarification or add additional context in comments.

4 Comments

Note that the path, where you have written `pwd`/host/src/path needs to be absolute path (for example just /host/src/path). If you use a relative path, it will not synchronize correctly between the host and the container.
Doesn't pwd always return absolute path?
should I write "/host/" and "/container/" or this is the filepath on those systems? the or the container name? what is the host called then? I just want to sync the files from my mac to docker, should I do 1 or 2 then or both?
I get docker: invalid reference format: repository name must be lowercase. using -ditv if I don't use the flags -dit then the container terminates after the command
5

If you are under OSX, those kind of volume shares can become very slow, especially with node-based apps ( a lot of files ). For this issue, http://docker-sync.io can help, by providing a volume-share like synchronisation, without using volume shares, this usually speeds up your container read/write speed of the code-directory from 50-80 times, depending on what docker-machine you use.

For performance see https://github.com/EugenMayer/docker-sync/wiki/4.-Performance and for easy examples how to use it, see the boilerplates https://github.com/EugenMayer/docker-sync-boilerplate for your case the unison example https://github.com/EugenMayer/docker-sync-boilerplate/tree/master/unison is the one you would need for NFS like sync

4 Comments

It does not work for windows, right? Do you know an alternative?
It does not work with Docker Toolbox on Windows. However, it does work with Docker for Windows, which requires Windows 10 (but not Home Edition).
Works with osx/windows/linux and freebsd(0.5.0) - but yes some strategies like native_osx are OSX only, and also some docker-engines
This may be unnecessary from Docker App 4.7 through VirtioFs experimental feature: virtio-fs.gitlab.io
5
docker run -dit -v ~/my/local/path:/container/path/ myimageId 

For /container/path/ you could use for instance /usr/src/app. The flags:

-d = detached mode,

-it = interactive,

-v + paths = specifies the volume.

(If you just care about the volume, you can drop the -dit flag.)

Docker run reference

1 Comment

-d = detached mode, -it = interactive, -v specifies the volume. If you just care about the volume, you can drop the -dit flag. docs.docker.com/engine/reference/run
0

I use Scaffold's File Sync functionality for this. It gets the job done, and without needing overly complex configuration.

Setting up Scaffold in my project was as simple as installing Skaffold (through chocolatey, since I'm on Windows), running skaffold init --generate-manifests in my project folder, and answering a couple questions it asked.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.