1. Creating a Dockerfile for a Spring Boot project
- Docker can build images automatically by reading the instructions from a Dockerfile.
- A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
Syntax of the Dockerfile
# Comment INSTRUCTION arguments
Note: The instruction is not case-sensitive. However, the convention is for them to be UPPERCASE to distinguish them from arguments more easily.
Creating a Dockerfile for our project
1. Install Docker extension for VSCode.
2. Create a file and name it .dockerignore
, it works in the same way as .gitignore but for Docker instead of GitHub/GitLab, we will not need it for this session, but it is a good practice.
3. Create a file in your project root folder and name it Dockerfile
, you do not need to specify any extension to this file.
4. Copy the following content into the Dockerfile
# Docker runs instructions in a Dockerfile in order. # A Dockerfile must begin with a FROM instruction. # ARG is the only instruction that may precede FROM in the Dockerfile. # The FROM instruction specifies the Parent Image from which you are building. # FROM [--platform=<platform>] <image>[:<tag>] [AS <name>] FROM eclipse-temurin:17-jdk-alpine # Always specify the tag/version, otherwise, it will be set to the latest that might change in time # Note: for ARM-based processors, eclipse-temurin:17-jdk-alpine will fail, thus, use "FROM --platform=linux/amd64 eclipse-temurin:17-jdk-alpine". # The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. # WORKDIR /path/to/workdir # COPY <src>... <dest> # The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>. # COPY . . without specifying a WORKDIR will copy everything from the project root directory to container root directory -not recomanded COPY target/*.jar app.jar # The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. # EXPOSE <port> [<port>/<protocol>...] EXPOSE 8080 # Regardless of the EXPOSE settings, you can override them at runtime by using the -p flag. For example # $ docker run -p 8080:8080 # An ENTRYPOINT allows you to configure a container that will run as an executable. # ENTRYPOINT includes the commands we need to execute in order to run this application. ENTRYPOINT ["java", "-jar", "/app.jar"]
5. We need to clean our project by deleting /target folder. In the explorer bar of VSCode, expand Maven, expand the project, under lifecycle, and run clean.
6. We need to convert our .java source code into a .jar and puts it into the /target folder. In the explorer bar of VSCode, expand Maven, expand the project, under lifecycle, and run package.
7. Now we are ready to build the Docker image for this application. You can do that by writing the following command in your terminal. Make sure you are in the root directory of your project and don’t forget the last ".", which refers to the location to host the image:
$ docker build -t docker-w1 .
8. Check whether the image has been successfully created in your local Docker registry using the following command:
$ docker image ls
9. Check also your Docker desktop app and the image should be there as well.
10. You can run the image using the following command, but do not do that because the project in week 2 needs a database, which we still need to create an image for.
$ docker run docker-w1