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?:
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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With