0

I am just trying to dockerize my Spring Boot app with MySQL database and at the beginning I see that I can do that by running some necessary commands as shown below:

docker pull mysql:8.0
docker network create springmysql-net
docker run --name mysqldb --network springmysql-net -e MYSQL_ROOT_PASSWORD=pass -e 
MYSQL_DATABASE=employee-db -e MYSQL_USER=user -e MYSQL_PASSWORD=****** -d mysql:8.0
// update application properties:
url: jdbc:mysql://<container-name>:3306/employee-db&useSSL=false
docker build -t backend .
docker run --network springmysql-net --name backend-container -p 8080:8080 -d backend

And the Dockerfile is as shown below:

FROM openjdk:8
ADD target/employee-api-0.0.1-SNAPSHOT.jar employee-api-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "employee-api-0.0.1-SNAPSHOT.jar"]

Here is the points that I could not be sure or understand:

1. Assume that we send the jar file to the customer and want them to run the app on Docker with less effort. I think we can make all of those commands to be executed automatically. But I am not sure for which file should we use for this. Do we need Dockerfile? Or docker-compose.yml?

2. I also use docker-compose.yml to create container and making necessary settings for development. But, I am not sure what is the purpose of Dockerfile? It seems tı be also used for automation of this procedures. Any clarification pls?

3. In most of the examples, FROM openjdk:8 is used. I think it is related to that there is no JRE of Java 11. So, can it be also used as FROM openjdk:8 on Dockerfile for Java 11 or Java 17 applications?

Jack
  • 1
  • 21
  • 118
  • 236
  • 1
    When you want to deploy your application via docker, you should ideally deliver a built docker image. The docker compose file could be used to setup the application stack. Maybe you should take a look at [this](https://stackoverflow.com/questions/29480099/whats-the-difference-between-docker-compose-vs-dockerfile). – 0x1C1B Nov 09 '22 at 12:44
  • Do you mean that it is only used for application related tasks e.g. copying jar files from target folder, etc.? But I also remember it is used for installing images, and some other commands. Any clarification pls? – Jack Nov 09 '22 at 12:48
  • And what do you think about my other questions? – Jack Nov 09 '22 at 12:48

1 Answers1

2
  1. Assume that we send the jar file to the customer and want them to run the app on Docker with less effort. I think we can make all of those commands to be executed automatically. But I am not sure for which file should we use for this. Do we need Dockerfile? Or docker-compose.yml?

You could send the customer a Dockerfile with commands to build and run the Docker container, or you could send them a docker-compose.yml file and the customer would likely only need to run docker-compose up. If the customer will be using the docker-compose.yml file though, you'll also need to provide the image built from the Dockerfile or the Dockerfile itself with relevant context needed to build it. Giving the customer any files related to application code or more sensitive variables should be decided in a conversation with the customer.

  1. I also use docker-compose.yml to create container and making necessary settings for development. But, I am not sure what is the purpose of Dockerfile? It seems tı be also used for automation of this procedures. Any clarification pls?

The Dockerfile basically contains the instructions to build the Docker image so that the image is built the same way on any machine.

  1. In most of the examples, FROM openjdk:8 is used. I think it is related to that there is no JRE of Java 11. So, can it be also used as FROM openjdk:8 on Dockerfile for Java 11 or Java 17 applications?

You should take note that that image has been deprecated and will no longer be maintained, so you should check out a different base image to use from the variants listed on Docker Hub. For example, you can use an amazoncorreto base image that starts with whatever Java version you're targeting.

Ryan Cahill
  • 662
  • 3
  • 10
  • Wonderful explanations. Regarding to your answers >>> – Jack Nov 09 '22 at 15:08
  • **1.** Which files should I provide to the customer? Are `*jar`, `Dockerfile` and `docker-compose.yml` enough? Or should I also provide `application.yml` as well? Because if I do not include the connection properties in `docker-compose.yml`, the customer will probably need to `application.yml`. Could you explain a little bit more pls? – Jack Nov 09 '22 at 15:11
  • **2.** You can also share an example Dockerfile, it would be so useful for me. – Jack Nov 09 '22 at 15:17
  • **3.** Hmmm, very important point, then I should use `FROM amazoncorretto:11` in my Dockerfile for Java 11. – Jack Nov 09 '22 at 15:17
  • Related to 1, I've updated the answer slightly. Don't give away any application code or secrets unless you and the customer understand how sensitive they are. Regarding 2, you included a Dockerfile above which is a good place to start. I wouldn't be able to generate a Dockerfile for you unless I knew everything about your application. Seeing as your original questions were answered, please upvote and mark the answer as accepted. – Ryan Cahill Nov 09 '22 at 15:54
  • Thanks for reply, already upvoted for your helps. On the other hand, sending docker-compose.yml without Dockerdile seems better option as it seems much easier to run the app. However, I could not understand the function of Dockerfile. When I look at it, it copies jar file to a location (probably the location in the container of the app). So, if I don't provide it, how the docker-compose proceed without it? Also I think I need to provide jar file that you did not mentioned, right? – Jack Nov 09 '22 at 16:52