Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 . 
like image 807
Dev Khadka Avatar asked Sep 27 '17 06:09

Dev Khadka


People also ask

Should you use the same Dockerfile for Dev staging and production builds?

In fact, they should not be. You don't need testing dependencies or direct access. They should be as similar as possible, differing only in the environment variables provided to each. Automated tests are executed on staging, but they should not require the setup to be different from production.

Can you have two Dockerfile?

As Kingsley Uchnor said, you can have multiple Dockerfile , one per directory, which represent something you want to build.

Can I use Docker for production?

In short: Docker is only as safe as its users' implemented safety measures. Technically, it can be used in production. When it comes to safety, Docker's come a (really) long way since its early days.

Do you create multiple dockerfiles for each environment?

Too many intro to Docker tutorials create multiple Dockerfiles for each environment, or only go over creating one environment. Here's a quick and easy way to keep a single file and overwrite the command in development.

Can I use the same dockerfile for staging and production?

Staging and production should be using the same image, built from the same Dockerfile to guarantee that they are as-similar-as-possible. If the size of the image is not of essence, and we’re very sure that there will be no negative impact on the performance, those can also contain testing dependencies and dev tools.

Do you use Docker for Laravel 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.

What is the best way to get started with Docker?

Docker images can be a great way to create reproducible, automated environments. You’ve gotten your app to work locally with Docker - everything is up with a single command and you are ready to start planning. You’ll want to use Docker for development and to run your app in testing for now, to learn enough to move to production eventually.


1 Answers

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" ... 
like image 51
yamenk Avatar answered Sep 27 '22 18:09

yamenk