How to configure different dockerfile for development and production

前端 未结 4 415
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 00:49

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 director

4条回答
  •  执念已碎
    2021-01-30 01:44

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

提交回复
热议问题