头图

In the first four articles of this series, we learned how to install Docker on the Ubuntu operating system, and through practical exercises, we learned the implementation principle of isolating Docker and host operating system file directories from each other, and the working principle of Docker Volume. By manually modifying the Nginx Docker image, the purpose of customizing its homepage display is achieved.

In the last exercise of the fourth article, we introduced how to run a developed web application in a Docker container. This article starts from this foundation and introduces how to make a new image through dockerfile after placing the web application inside the Docker container.

Exercise 1: Use dockerfile to make an image containing the specified web application

Create a new folder jerry-build , put your web application in this folder, my web application in the folder webapp. Create a new file dockerfile in the jerry-build folder:

The content of this dockerfile is very simple, just three lines:

 FROM nginx:stable
COPY webapp/ /usr/share/nginx/html/webapp/
RUN ls -la /usr/share/nginx/html/webapp*

We execute the command docker build .

This command successfully pushed all the files in the webapp folder containing our web application into the nginx image:

Use option -t to specify the name of the generated image jerry-nginx-image :

After the image is successfully generated, you can execute the image with the docker run command:

docker run -d -p 1082:80 jerry-nginx-image:1.0

Use the command docker images to view the image we just made, the size is 109MB.

The next exercise will show you how to publish this ready image to the Docker hub.

Exercise 2: How to publish the image made by dockerfile to Docker hub

First you have to register an account on docker hub :

Then create a new repository to hold the Docker image. The operation method is the same as creating a code repository in Github.


The repository after creation looks like this:

docker ps gets the ID of the current container:

Use docker commit to submit, which is similar to the git commit familiar to programmers:

docker commit 53de4188b702 i042416/ui5-nginx

docker login Log in to docker hub:

After successful login, use docker push to push the local image to docker hub:

docker push i042416/ui5-nginx:latest

Get the message of successful push:

After the push is successful, go to docker hub to view the generated tag: latest and image size: 45MB.

to another machine:

docker run -it i042416/ui5-nginx:
It can be found from the output that because the local image does not exist, so docker run automatically pull this image from the docker hub:

Then run with the command docker run -d -p 1080:80 i042416/ui5-nginx :

After that localhost:1080/webapp I can access my web application:


Exercise 3: Step-by-step package a SpringBoot application into a Docker image and run it

(1) First have a working SpringBoot application.

Clone a working SpringBoot application locally:

cd into the project folder and use the command line:

mvn spring-boot:run

When you see the console output Tomcat started on port: 5030(http) prompt, it means that the SpringBoot application is successfully started locally:

At this time, you can access the SpringBoot application with the following url. If everything is normal,

http://localhost:5030/commerce/product
You can see Hello World in the browser:

Note: The port monitored by the SpringBoot application is 5030. If you want to change it to another port, modify it in application.properties.

(2) The next step is to package the SpringBoot into a Docker image.
The following is a Dockerfile that has been written, and the Docker image is based on the Dockerfile.

  • The FROM command in the first line specifies that our image is based on the openjdk image.
  • The VOLUME command on the second line defines a persistent store that points to the tmp folder in the container.

The default working directory created by the SpringBoot application for the built-in Tomcat server instance is tmp. Through this command, you can create a temporary directory in the host directory where Docker is running /var/lib/docker , and hook it to the tmp inside the container. .

If your SpringBoot application does not perform persistent write operations, this step can be omitted.

  • The third line, add the jar file in the target folder in the local directory to the container, and rename it to app.jar.
  • Fourth line: The role of the ENV command is to set environment variables. In complex usage scenarios, we may need to start the JVM with various parameters passed into the Java command through the environment variables set by the ENV command. Setting the environment variable can be omitted in this simple example.
  • The fifth line: ENTRYPOINT, as the name suggests, the starting point for the container image to run.

After understanding the function and syntax of this Dockerfile, we use docker build to generate an image based on this Dockerfile.

docker build -t jerry/springbootexample:v1 .

The last punctuation mark in the above command line . is not a punctuation mark indicating the end, but a "." in the Linux system, which represents the current directory.

After executing the above command line, you will see that the five commands we defined in the Dockerfile are executed in sequence. The first is to download the base image of openJDK:

Then follow the remaining steps in sequence.

After you see the message "Successfully built", it means that the image has been created successfully.
Use the docker images command line to see this image, the size is 136MB.

(3) Finally, use the docker run command to execute the image.

The run command has many parameters, such as running the image interactively:

docker run -it jerry/springbootexample:v1
In this way, the output of the mirror processing the user request is automatically redirected to the console of the host.

The -p parameter can implement port mapping. The meaning of the following command line is to map the port monitored by the SpringBoot application in Docker to port 8000 of the host. In this way, when the user accesses in the browser, the port used should be the host's port 8000.

docker run -p 8000:9000 --name jerrydockerdemo -d jerry/springbootexample:v1

You can use the docker ps command to get the ID of the running image, and then use the docker stop command to terminate the running of the image.
You can also use the command docker exec -it to enter inside a running container:

sudo docker exec -it 8302db78f838 /bin/sh
When we wrote the Dockerfile before, the tmp created by the VOLUME command can be observed when entering the container. In tmp, I found the data generated in the working directory of the built-in Tomcat instance during the execution of SpringBoot.

Summarize

This article first introduces how to make an Nginx image containing a web application into a new image through dockerfile and publish it on Docker Hub, and then by showing the steps to make a SpringBoot application into a Docker image, it introduces VOLUME in DockerFile, Usage of commands such as ADD, ENV and ENTRYPOINT.

The four articles preceding this tutorial can be accessed from the links below:


注销
1k 声望1.6k 粉丝

invalid