How to edit code in a Docker container in development?

ChocoDeveloper picture ChocoDeveloper · Apr 3, 2014 · Viewed 35.2k times · Source

I have all my websites' code under /srv in my containers.

My Dockerfile downloads the code using git and makes it part of the image for easier deployment to production.

But then how do I edit the code in development? I thought using volumes was the solution, eg: -v /docker/mycontainer/srv:/srv. But it overwrites the directory in the container. If it's the first time I run it it empties it because there's nothing in the host. So whatever I did in the Dockerfile was gets lost.

There are also directories and files inside /srv/myapp that I want to be shared across the different versions of my app, eg: /srv/myapp/user-uploads. This is a common practice in professional web development.

So what can I do to be able to do all these things?:

  • edit code in /srv in development
  • share /srv/myapp/user-uploads across different versions
  • let Dockerfile download the code. Doing "git clone" or "git pull" outside of Docker would defeat Docker's purpose in my opinion. Besides there are things that I can't run in the host, like the database migrations or other app-specific scripts.

Is there a way to do a reverse volume mount? I mean make the container overwrite the host, instead of the opposite.

I'm thinking one soluiton might be to copy /srv to /srv.deployment-copy before running the container's daemon. And then when I run the daemon check if /srv.deployment-copy exists and copy everything back to /srv. This way I can use /srv as a volume and still be able to deploy code to it with the Dockerfile. I'm already using aliases for all the docker commands so automating this won't be a problem. What do you think?

Answer

Mikl picture Mikl · Nov 13, 2014

There is another way to start container with volume from another container:

Look at https://docs.docker.com/userguide/dockervolumes/
Creating and mounting a Data Volume Container

If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it's best to create a named Data Volume Container, and then to mount the data from it.

Let's create a new named container with a volume to share.

$ sudo docker run -d -v /dbdata --name dbdata training/postgres echo Data-only container for postgres

You can then use the --volumes-from flag to mount the /dbdata volume in another container.

$ sudo docker run -d --volumes-from dbdata --name db1 training/postgres

And another:

$ sudo docker run -d --volumes-from dbdata --name db2 training/postgres

Another useful function we can perform with volumes is use them for backups, restores or migrations. We do this by using the --volumes-from flag to create a new container that mounts that volume, like so:

$ sudo docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata

=============

I think you should not use mounting of your host directory to a container. But you can use volumes with all its powers. You can edit files in volumes using other containers with perfect set of your editors and tools. And container this your app will be clean without overhead.

Structure is:
-) Container for app data
docker run -d -v /data --name data
-) Container for app binaries
docker run -d --volumes-from data --name app1
-) Container for editors and utilities for development
docker run -d --volumes-from data --name editor