php - How to "dynamize" Dockerfile / Docker Compose?


I'm Dockerizing legacy PHP project. I would like to have Xdebug enabled in development environment and my Dockerfile copies pre-built php.ini into container.

Due to some network issues we have to havexdebug.remote_connect_back = 0 on Mac OS X (and correspondingxdebug.remote_host = docker.for.mac.localhost) andxdebug.remote_connect_back = 1 on Linux.

Is it possible to grab current OS type in Dockerfile/Docker Compose to copy php.ini corresponding to host OS?




Usevolumes described here indocker-compose.yml. Createphp.linux.ini andphp.mac.ini in aconfig folder (or wherever) and map one of them to the container:

    image: php
      - ./config/php.linux.ini:/etc/php.ini #or wherever the config is

Of course your users will have to manually changephp.linux.ini forphp.mac.ini, but it's a one time manual change.




That information isn't (and shouldn't) be available at image build time. The same Linux-based image could be run on native Linux, a Linux VM on Mac (and then either the Docker Machine VM or the hidden VM provided by Docker for Mac), a Linux VM on Windows, or even a Linux VM on Linux, regardless of where it was originally built.

Configuration such as host names should be provided at container run time. Environment variables are a typical way to do this, or you can use the Docker volume mechanism to push in configuration files from the host.

If your issue is purely around debugging your application, you can also set up a full development environment on your host, and only build in to your image the things you need to run it in a more production-like environment.




I decided to use Docker Compose ability of reading.env files. The whole workflow is as following:

  • create.env.sample file with all the lines commented (sorry, couldn't manage to correctly display commented lines): OS=windows OS=linux OS=mac
  • ignore.env file by adding/.env line to.gitignore file

  • copy sample file with$ cp .env.sample .env and leave uncommented just one line corresponding to your OS

  • move OS-specific Xdebug-related section of php.ini into separate file with names likexdebug-mac.ini,xdebug-windows.ini,xdebug-linux.ini, etc.

  • add todocker-compose.ymlargs section to chosen service with value like- OS=${OS}

  • in correspondingDockerfile add lines: ARG OS=${OS} COPY ./xdebug-${OS}.ini /usr/local/etc/php/conf.g/

OS value mentioned in.env will be expanded on building image time

  • execute$ docker-compose up -d --build to build image and start container

  • commit all your changes on success to let your colleagues have Xdebug set properly on any platform; don't forget to tell them make their own instance of.env file from template

People are also looking for solutions to the problem: php - Using custom twig template in Drupal8 custom block


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.