How can I verify in my Dockerfile that PHP contains the modules I need in my image?

Viewed 49

I am moving a legacy PHP application running in Apache under Ubuntu 14.04 to Docker for further development.

Therefore I need the PHP engine to have the same modules installed as the Ubuntu install, and I would like to verify automatically in my Dockerfile that the output from php -m contains the same modules as a cached output from php -m on the Ubuntu machine.

In a shell script I would expect this to be doable using exit codes and set -e. The Dockerfile documentation does not mention anything about exit codes from the RUN command so it is probably not doable.

So, question is generally "How can I make my Dockerfile build fail from code" but I have asked it specifically for my use case as there might be a smarter way to do it.

Suggestions?

1 Answers

Although probably not documented, the RUN commands are error prone and so, the exit code of a script will make the build fail.

A really naive way to tackle your use case then, would be to have two files and diff them.

Here is a skeleton doing this:

FROM alpine

RUN echo 'a' > a; echo 'a' > b \
    && diff a b 
# all is right, this won't fail

RUN echo 'a' > a; echo 'b' > b \
    && diff a b
# woops?!

Would build with:

$ docker build .
[+] Building 1.5s (6/6) FINISHED                                                                                     
 => [internal] load build definition from Dockerfile                                                            0.0s
 => => transferring dockerfile: 200B                                                                            0.0s
 => [internal] load .dockerignore                                                                               0.0s
 => => transferring context: 2B                                                                                 0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                0.6s
 => CACHED [1/3] FROM docker.io/library/alpine@sha256:a75afd8b57e7f34e4dad8d65e2c7ba2e1975c795ce1ee22fa34f8cf4  0.0s
 => [2/3] RUN echo 'a' > a; echo 'a' > b     && diff a b                                                        0.4s
 => ERROR [3/3] RUN echo 'a' > a; echo 'b' > b     && diff a b                                                  0.4s
------
 > [3/3] RUN echo 'a' > a; echo 'b' > b     && diff a b:
#6 0.352 --- a
#6 0.352 +++ b
#6 0.352 @@ -1 +1 @@
#6 0.352 -a
#6 0.352 +b
------
executor failed running [/bin/sh -c echo 'a' > a; echo 'b' > b     && diff a b]: exit code: 1

So in your real use case what you could do is:

FROM alpine

COPY php-modules.txt .

RUN php -m | diff php-modules.txt -

And this is definitely the purposed behaviour:

Related