Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit code in a Docker container in development?

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?

like image 690
ChocoDeveloper Avatar asked Apr 03 '14 15:04

ChocoDeveloper


People also ask

Can you edit code in a Docker container?

The Remote – Containers extension for Visual Studio Code lets you edit files and folders inside Docker containers. It works seamlessly with the VS Code editor features, including IntelliSense, directory indexing, debugging, and extensions.

Can you develop inside a Docker container?

Developing inside a Docker container generally means starting a container and leaving it running, while you edit your source code. As you make changes, you see the changes appear in the container. To get your source code inside a container, you can use something called a bind mount.

Can Docker be used for development?

If you're having a hard time building something (by build, I mean compile), build it inside Docker. This primarily applies to developers using MacOS and Windows. You only need Docker to develop. You don't need to install a bunch of language environments on your machine.


1 Answers

I found the best way to edit code in development is install everything as usual (including cloning your app's repository), but move all the code in the container to say /srv/myapp.deploy.dev. Then start the container with a rw volume for /srv/myapp, and a init.d script that cleans that volume and copies the new contents inside like this:

rm -r /srv/myapp/* rm -r /srv/myapp/.[!.]* cp -r /srv/myapp.deploy.dev/. /srv/myapp rm -r /srv/myapp.deploy.dev 
like image 158
ChocoDeveloper Avatar answered Oct 13 '22 08:10

ChocoDeveloper