Ivan
Ivan

Reputation: 11

Dockerfile (multistage build?) and container with multiple executable files

I'm having some issues with producing the following setup.

I've implemented a Java application that can start a process with any executable file (with Runtime.exec({whatever-file-here})....), where the file path is provided via external configuration. I have then created a Docker image with the said application, the idea being that the external executable file will be part of a second Docker image, containing all the necessary dependencies. This will leave the option to easily swap the file being executed by the Java app.

So from one side there is the Java image that should look like:

FROM openjdk:14
WORKDIR /app
COPY /build/some.jar /app/some.jar

And let's say I build a service-image out of it. The next step would be to use the aforementioned image as a base image in either a second Dockerfile or a single file with multiple stages.

The way I imagine it being a second Dockerfile for let's say a Python executable will be:

FROM python:latest #python so I can run the script
COPY --from=service-image / / #to get the runtime environment + app directory + jar
COPY some-file.py /app/some-file.py #copying the file for the jar to run
CMD ["java", "-jar", "/app/some.jar"] #the command that will start the java app

And running a container with an image build from the second file should have both a JRE to run the jar file and python to run the .py file as well as the actual .jar and .py files. I'm ignoring any details such as environment variables necessary for the java app to work. But that doesn't seem right, as the resulting image is absolutely massive.

What would you recommend as an approach? Until now I haven't dealt with complex Docker scenarios.

Upvotes: 0

Views: 1041

Answers (1)

Martin Paucot
Martin Paucot

Reputation: 1251

I really do not think that you will be able to create a proper container by replacing the root folder with the one of an other image.

Here is how you could do:

  1. Build your jar file using an openjdk image
  2. Create an image with python and Java installed and copy the .jar from the previous image

You can start from a python image and install Java or the opposite. Here is an example:

FROM openjdk:14 AS build

WORKDIR /app
COPY . .
RUN build-my-app.sh

FROM openjdk:14-alpine AS runner

WORKDIR /app

# Install python
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools

COPY --from=builder /app/dist/myapp.jar myapp.jar
COPY some-file.py some-file.py

CMD ["java", "-jar", "/app/some.jar"] #the command that will start the java app

EDIT: Apparently you are not using Docker to build your jar so you can simply copy it from your host machine (like that py file) and skip the build step.

Upvotes: 1

Related Questions