Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Docker for development, should I use ADD or VOLUME?

Tags:

php

docker

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:

  1. 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.

  2. 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?

like image 345
ehriq Avatar asked May 02 '15 23:05

ehriq


2 Answers

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
like image 68
Mark O'Connor Avatar answered Oct 28 '22 23:10

Mark O'Connor


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.

like image 29
Sean King Avatar answered Oct 29 '22 01:10

Sean King