How to configure different dockerfile for development and production

As a best practice you should try to aim to use one Dockerfile to avoid unexpected errors between different environments. However, you may have a usecase where you cannot do that.

The Dockerfile syntax is not rich enough to support such a scenario, however you can use shell scripts to achieve that.

Create a shell script, called install.sh that does something like:

if [ ${ENV} = "DEV" ]; then 
    composer install
else
    npm install
fi

In your Dockerfile add this script and then execute it when building

...
COPY install.sh install.sh
RUN chmod u+x install.sh && ./install.sh
...

When building pass a build arg to specify the environment, example:

docker build --build-arg "ENV=PROD" ...

Leave a Comment

File not found.