I'm starting to use Docker on projects, and the majority of these projects runs on PHP (some on Nginx/PHP-FPM, some on Apache/PHP as module).
The thing is: my brain can't understand when to use the ADD and VOLUME directives on the Dockerfile for the paths that holds the source code.
Here's what I want: to change a line of code, access the browser on the container port, and see the changes. And so on, until things run as expected and then I "add" the code to the machine and deploy it. It's how I think Docker runs. Am I wrong?
AFAIK, the ADD directive copies the files on the given path to the image, and the VOLUME directive just say that a given path will me mounted by|on the Host.
Things that run through my mind, so far:
Add the source code files via ADD, and then user the "-v" switch on command line to mount the source code on the same path used on ADD.
Use VOLUME, and deal with two different Dockerfiles: for development containers, and for testing/development, and build each one as needed (this thing I think is counterproductive, and my heart tells me that this is not how it should work).
Can someone help me with this? What my brain can't grasp, and I should do (and understand) to leverage my Docker knowledge?
Th official PHP image gives examples of the two possible workflows.
For production you can have a Dockerfile that builds a container containing your code:
FROM php:5.6-apache
COPY src/ /var/www/html/
whereas for development you run a container with a volume referencing the local file system:
docker run -it --rm --name my-php-dev -v "$PWD/src":/var/www/html php:5.6-apache
You definitely want to use VOLUME
in development, unless you find rebuilding your image every time you want to see an edit fun. Likewise, you want to use ADD
in production, otherwise the whole idea of immutable environments with docker kind of goes out the window.
To get around the hassle of having two separate Dockerfiles to deal with, you can put your base env (ie: everything but ADDing your app) into one Dockerfile and chuck it up as an image on Docker Hub (or just use an existing image, if you can find one that fits your needs). Then use something like Docker Compose in development (using the image you made before as the container for your app, and mounting your codebase as a volume), and a super slim Dockerfile that just extends from that docker image and adds your codebase for production.
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