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?
Answer
Solution:
Use
volumes
described here indocker-compose.yml
. Createphp.linux.ini
andphp.mac.ini
in aconfig
folder (or wherever) and map one of them to the container:Of course your users will have to manually change
php.linux.ini
forphp.mac.ini
, but it's a one time manual change.Answer
Solution:
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.
Answer
Solution:
I decided to use Docker Compose ability of reading
.env
files. The whole workflow is as following:.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
filecopy sample file with
$ cp .env.sample .env
and leave uncommented just one line corresponding to your OSmove OS-specific Xdebug-related section of php.ini into separate file with names like
xdebug-mac.ini
,xdebug-windows.ini
,xdebug-linux.ini
, etc.add to
docker-compose.yml
args
section to chosen service with value like- OS=${OS}
in corresponding
Dockerfile
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 timeexecute
$ docker-compose up -d --build
to build image and start containercommit 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