skip to Main Content

I’m tasked with creating a very simple, web browser accessible gui that can run a specific java file within a docker container. To do this I’ve chosen to set up a php-apache server that serves an index.php document with the gui. The Dockerfile looks like this:

FROM php:7.0-apache
COPY src /var/www/html
EXPOSE 80

This gets the gui (index.php is inside the src folder) I’ve written up and running no problem, but it cannot access and run the required java files (obviously, since this creates a separate container).

The Question:

How can I set up a php-apache server inside the existing Dockerfile (provided below) doing the same thing as the Dockerfile above? My aim is to run the java file using php scripts and display the result to the user.

FROM openjdk:8-jre-slim

WORKDIR /usr/src/app
COPY ["./build/libs/*.jar", "./fooBar.jar"]
ENV JAVA_OPTS=${FOO_JAVA_OPTS}
CMD ["/usr/bin/tail", "-f", "/dev/null"]

I have not written the java file myself, only being tasked with running specific commands using it.

2

Answers


  1. Chosen as BEST ANSWER

    There appears to be no easy way of merging images like I initially hoped (You cannot have multiple FROM statements in your Dockerfile). What I eventually ended up doing was to manually merge the two images (openjdk and php) into something like this:

    FROM php:7.0-apache
    
    ENV LANG C.UTF-8
    RUN { 
            echo '#!/bin/sh'; 
            echo 'set -e'; 
            echo; 
            echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"'; 
        } > /usr/local/bin/docker-java-home 
        && chmod +x /usr/local/bin/docker-java-home
    RUN ln -svT "/usr/lib/jvm/java-8-openjdk-$(dpkg --print-architecture)" /docker-java-home
    ENV JAVA_HOME /docker-java-home/jre
    ENV JAVA_VERSION 8u212
    ENV JAVA_DEBIAN_VERSION 8u212-b01-1~deb9u1
    RUN set -ex; 
        if [ ! -d /usr/share/man/man1 ]; then 
            mkdir -p /usr/share/man/man1; 
        fi; 
        apt-get update; 
        apt-get install -y --no-install-recommends openjdk-8-jre-headless="$JAVA_DEBIAN_VERSION"; 
        rm -rf /var/lib/apt/lists/*; 
        [ "$(readlink -f "$JAVA_HOME")" = "$(docker-java-home)" ]; 
        update-alternatives --get-selections | awk -v home="$(readlink -f "$JAVA_HOME")" 'index($3, home) == 1 { $2 = "manual"; print | "update-alternatives --set-selections" }'; 
        update-alternatives --query java | grep -q 'Status: manual'
    
    COPY ["./build/libs/*.jar", "./FooBar.jar"]
    ENV JAVA_OPTS=${FOO_JAVA_OPTS}
    
    COPY gui/src /var/www/html
    EXPOSE 80
    

    Both are Debian based images so the merging were relatively easy (I also removed much of the cluttering comments from the original image source) and since the openjdk image were simpler, I added it on top of the php image instead of the other way around.


  2. As it is Debian based images. one way of doing it, install packages in the container and create the new images from that.

    root@310c94d8d75f:/usr/src/app# cat /etc/os-release
    PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
    NAME="Debian GNU/Linux"
    VERSION_ID="9"
    VERSION="9 (stretch)"
    
    2: root@310c94d8d75f:/usr/src/app# apt update
    
    3- root@310c94d8d75f:/usr/src/app# apt install apache2
    4- root@310c94d8d75f:/usr/src/app# apt install php
    

    finally run : docker commit

    after this, you will get a new image with the mentioned name.

    Ref: https://docs.docker.com/engine/reference/commandline/commit/

    2: you can add the same command in Dockerfile and rebuild.

    FROM openjdk:8-jre-slim
    
    WORKDIR /usr/src/app
    COPY ["./build/libs/*.jar", "./fooBar.jar"]
    ENV JAVA_OPTS=${FOO_JAVA_OPTS}
    CMD ["/usr/bin/tail", "-f", "/dev/null"]
    RUN apt update && apt install apache2 -y && apt install php -y
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search