This article aims to practice the basic usage of Docker by docker-ization of a specific Spring Boot + MongoDB project. After docker-ization of the project, the back-end service can access the database normally, and the single-page Web application hosted by the back-end service can still be opened. In short, the running status of the project meets expectations. See the GitHub repository for specific projects to use.
To learn the basic principles and usage of Docker, you can refer to this open source e-book Docker – From Getting Started to Practice. I think this book is the best one so far, at least better than some similar published books.
The basic principle and usage of Docker are not introduced in this article. If you want to learn Docker, please refer to the open source book above. This paper will record the docker-ization practice of the project.
1. Environment preparation
- Java: JDK 1.8
- Linux: Ubuntu 17.10.1
- Docker CE: 17.12.0
2. Obtaining the official Docker image
In the Docker Store, search for the Docker image you want to obtain and then follow the instructions to obtain it.
2.1 Obtaining the Java JRE
docker pull store/oracle/serverjre:8
Copy the code
2.2 Obtaining the Java JRE
docker pull mongo
Copy the code
3. Build a custom Docker container
Java projects built as Docker containers require the following files:
- The Jar file
- Local configuration file
- Dockerfile file
The contents of the Dockerfile look like this:
# Basic JRE image
FROM store/oracle/serverjre:8
# change the time zone so that the container time is Beijing time
RUN echo "Asia/Shanghai" > /etc/timezone
Copy the required project files into the container
WORKDIR /app
ADD. / ClusterDevicePlatform - server - 1.8.9 - the jar/appADD ./setting /app
Expose the container's port
EXPOSE 30232 8080
# Run the project
CMD ["java"."-jar"."-Dfile.encoding=UTF-8"."ClusterDevicePlatform - server - 1.8.9 - the jar." "]
Copy the code
After the compilation of Dockerfile file, there will be three files in the Java server directory, among which setting file and Jar package are components of the original project and need to be used together. Setting must be placed in the same directory as Jar package, as shown below:
Then run the following command:
docker build -t ky-server .
Copy the code
Then start to build the Docker image of the Java server application, and name the generated image “Ky-Server”.
After the Docker image construction of the Java server application is completed, execute the Docker images command to obtain all Docker images saved on the machine, as shown below:
4. Run containerized Java server applications
4.1 Creating a Docker Network
With the improvement of Docker network, it is officially suggested to add containers to the custom Docker network to connect multiple containers. The following is to create a new Docker network and name it Bitky:
docker network create -d bridge bitky
Copy the code
Open the two shells and execute the following two commands:
docker run --rm --name kylinked-mongo -p 27017:27017 --network bitky mongo:latest docker run --rm --name kylinked-server -p 80:8080 -p 30232:30232 --network bitky ky-server:latestCopy the code
Open the MongoDB and Java server applications in the foreground of the two shells. 27017 of the MongoDB container is mapped to port 80 of the operating system, and port 8080 of the Java server is mapped to port 80 of the operating system, which is convenient for browser access. The MongoDB image can be pulled directly in the normal way, which is not described in this article.
Open a new terminal to view the status of the container:
docker container ls
Copy the code
The port mapping status and container running status can be seen in the figure.
4.2 Docker Set the time Zone to “Beijing Time”
The default time zone in the container is UTC. If you want to change the time zone of the Docker image, you only need to add the following sentences to the Dockerfile:
RUN echo "Asia/Shanghai" > /etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata
Copy the code
Note that the Dockerfile presented in this article has already added the above statement, so it does not need to be added again. RUN dpkg-reconfigure -f noninteractive tzdata is Ubuntu’s command to change the time zone. If your custom image is using another distribution, the commands here will also need to change. This paper uses JRE as the basic image. After experiments, it is found that only the first sentence of the command can be executed.
Refer to the link
- Docker Documentation
- Docker deployment SpringBoot project integration Redis image for access count Demo
- Spring Boot applications are published to Docker
- Build the Spring Boot + Docker application step by step
- How do I set the time zone of a Container in Docker