Search code examples
dockerdockerfile

How to configure different dockerfile for development and production


I use docker for development and in production for laravel project. I have slightly different dockerfile for development and production. For example I am mounting local directory to docker container in development environment so that I don't need to do docker build for every change in code.

As mounted directory will only be available when running the docker container I can't put commands like "composer install" or "npm install" in dockerfile for development.

Currently I am managing two docker files, is there any way that I can do this with single docker file and decide which commands to run when doing docker build by sending parameters.

What I am trying to achieve is

In docker file

...
IF PROD THEN RUN composer install
...

During docker build

docker build [PROD] -t mytag .

Solution

  • UPDATE (2020): Since this was written 3 years ago, many things have changed (including my opinion about this topic). My suggested way of doing this, is using one dockerfile and using scripts. Please see @yamenk's answer.

    ORIGINAL:

    You can use two different Dockerfiles.

    # ./Dockerfile (non production)
    FROM foo/bar
    MAINTAINER ...
    
    # ....
    

    And a second one:

    # ./Dockerfile.production
    FROM foo/bar
    MAINTAINER ...
    
    RUN composer install
    

    While calling the build command, you can tell which file it should use:

    $> docker build -t mytag .
    $> docker build -t mytag-production -f Dockerfile.production .