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 .
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.
As Kingsley Uchnor said, you can have multiple Dockerfile , one per directory, which represent something you want to build.
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.
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.
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.
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.
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.
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" ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With