I want to create a dockerfile which contains 2 stages.
The first stage is to set up a MySQL server and the second stage is to start a backend service that accesses the server.
The problem is that the backend service stops when no MySQL server is available. Is there a way to make the stage dependent on the first stage being started?
what is a little strange is that when i create the dockerfile with the database at the top, the log of the backend is displayed. If the backend is on top, the log of the MySQL is displayed when starting.
Actual Dockerfile:
FROM mysql:latest AS BackendDatabase
RUN chown -R mysql:root /var/lib/mysql/
ARG MYSQL_DATABASE="DienstplanverwaltungDatabase"
ARG MYSQL_USER="user"
ARG MYSQL_PASSWORD="password"
ARG MYSQL_ROOT_PASSWORD="password"
ENV MYSQL_DATABASE=$MYSQL_DATABASE
ENV MYSQL_USER=$MYSQL_USER
ENV MYSQL_PASSWORD=$MYSQL_PASSWORD
ENV MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
EXPOSE 3306
FROM openjdk:10-jre-slim AS Backend
LABEL description="Backend Dienstplanverwaltung"
LABEL maintainer="Martin"
COPY ./SpringDienstplanverwaltung/build/libs/dienstplanverwaltung-0.0.1-SNAPSHOT.jar /usr/local/app.jar
EXPOSE 8080
ENTRYPOINT java -jar /usr/local/app.jar
2
Answers
actually you need Docker-composer of two containers. One for Mysql one for java app.
Multistage is mostly for cases like #1 build something, for example java or Go. #2 create second image and copy results of build. The general idea is to keep the second stage clean. We do not need to build tools, only results in second stage.
please see example:
Okay you seem to be a little confused with various things here. First of all, multi-stage builds are for building an application that needs some kind of build/compiling process, copying that build into another container with fewer dependencies and with just the executable, so in this context, trying to run a database in a multistage build makes no sense at all, due to the fact that building the container does not run it.
Now, you want to have a multi stage to build the java app and then copy that build into another container and then run it. Also, when you are running that container you need a mysql database, using docker-compose is a good tool for that, like this example:
Also, an example multi-stage Dockerfile for java applications: