Using Docker containers

Docker is a container platform that makes software development, deployment, and shipping easier. Containers are lightweight and executable software packages that include everything that is needed to run software. In this section, we are creating a container from our Spring Boot backend, as follows:

  1. Install Docker on your workstation. You can find the installation packages at https://www.docker.com/get-docker. There are installation packages for multiple platforms, and if you have a Windows operating system, you can go through the installation wizard using the default settings.
  2. The Spring Boot application is just an executable JAR file that can be executed with Java. The JAR file can be created with the following Maven command:
mvn clean install

You can also use Eclipse to run Maven goals by opening the Run | Run configurations... menu. Select your project in the Base directory field, using the Workspace button. Type clean install into the Goals field and press the Run button:

  1. Once the build is finished, you can find the executable JAR file from the /target folder:
  1. You can test that the build has executed correctly by running the JAR file with the following command:
 java -jar .cardatabase-0.0.1-SNAPSHOT.jar
  1. You'll see the application's starting messages, and finally, your application will be running:

Containers are defined by using Dockerfiles.

  1. Create a new Dockerfile in the root folder of your project and name it Dockerfile. The following lines show the contents of the Dockerfile. We are using Alpine Linux. EXPOSE defines the port that should be published outside of the container. COPY copies the JAR file to the container's filesystem and renames it app.jar. ENTRYPOINT defines the command-line arguments that the Docker container runs.
There is also a Maven plugin available to build Docker images. It is developed by Spotify and can be found at https://github.com/spotify/docker-maven-plugin.

The following lines show the contents of Dockerfile:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8080
ARG JAR_FILE
COPY target/cardatabase-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  1. Create the container with the following command. With the -t argument, we can give a friendly name to our container:
docker build -t carbackend .

At the end of the build command, you should see the Successfully built message:

  1. Check the list of the container using the docker image ls command:
  1. Run the container with the following command:
docker run -p 4000:8080 carbackend

The Spring Boot application starts, but it ends with an error, because we are trying to access the localhost database. The localhost now points to the container itself, and there is no MariaDB installed.

  1. We will create our own container for MariaDB. You can pull the latest MariaDB container from the Docker Hub using the following command:
docker pull mariadb:latest
  1. Run the MariaDB container. The following command sets the root user password and creates a new database, called cardb, that we need for our Spring Boot application:
docker run --name cardb -e MYSQL_ROOT_PASSWORD=pwd -e MYSQL_DATABASE=cardb mariadb
  1. We have to make one change to our Spring Boot application.properties file. Change the datasource URL to the following. In the next step, we will specify that our application can access the database container using the mariadb name. Once this change has been made, you have to build your application and recreate the Spring Boot container:
spring.datasource.url=jdbc:mariadb://mariadb:3306/cardb
  1. We can run our Spring Boot container and link the MariaDB container to it using the following command. This command now stipulates that our Spring Boot container can access the MariaDB container using the mariadb name:
docker run -p 8080:8080 --name carapp --link cardb:mariadb -d carbackend
  1. We can also access our application logs by typing the docker logs carapp command:

We can see that our application has started successfully, and the demo data has been inserted into the database that exists in the MariaDB container.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.228.40