Docker image construction process record

Keywords: Docker build

Docker image construction process record

Build a docker image for a java project of the company and store the image in a private library. Record the operation process.

1. Packing

This is a maven project of spring boot. The packaging command is very simple.

cd /projecthome
mvn clean package

Here, due to the pom file configuration of mvn, packaging is to package the current code and the dependent library into a jar package. If only the project file needs to be packaged, the pom file needs to be modified, but that is not the focus this time. It will be recorded in another document next time.

See the last chapter for the contents of the pom file

2. Prepare docker documents

The writing process of docker file is not formed at one time, but added bit by bit. The first is a basic description

# Dockerfile-base
FROM java:8
MAINTAINER icefox<icefox_wjx@hotmail.com>

WORKDIR /

Start with Java 8, set the working directory as the root directory, and execute the test.

docker build -t my-proj -f Dockerfile-base .

-f specifies the file name, including the directory relative to the current directory, - t specifies the image name to generate an image. Don't forget the last point, otherwise an error will be reported

# Forgetting the "point" will report an error
$ docker build -t my-proj -f Dockerfile-base 

"docker build" requires exactly 1 argument.
See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Test built image:

docker images													# Check that the image has indeed been built
docker run -it my-proj java -version  # Using the built image, execute java -version

You can see the java version information in the result

openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2~bpo8+1-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)

Continue to modify the docker file

FROM java:8
MAINTAINER wangjianxuan<wangjianxuan@hibpm.cn>

ADD proj.jar /proj.jar
WORKDIR /

EXPOSE 8085
ENTRYPOINT ["java","-jar","proj.jar"]

As a spring boot project, the ADD command adds the newly compiled jar package. Note that the path of the jar package here needs to be correct. Here, I ADD the proj.jar of the current directory to the image root directory and name it proj.jar.

In addition, the export command declares that the project exposes port 8085 to the outside world. This declaration is only a description for the user to see. It does not play a key role in the actual run-time, and it needs to be executed according to the run-time specification.

Another is to set the working directory as the root directory, which affects whether the final execution instruction can find a reasonable directory.

Finally, execute the command "java -jar proj.jar". Since the working directory has been set before, proj.jar can be found here.

# implement
docker build -t my-proj -f Dockerfile-base .

After the image is built, start the test.

docker images
docker run -d -p 8085:8085 --name my-proj-run-01 my-proj
docker logs -f 749a0671b13f13af4e2bf82b8052ef67f499aa478997e6387bc933dcc14bc408

First, from the list of images, you can see that both tag and REPOSITORY are "None" images. This is because the newly built image replaces the original image, and the original image becomes the image of none. You can find the image id and delete the image with the docker rmi command.

Find the newly built image and execute it. Because it is a web project, use - d for background execution. To expose the access port, use - p to name the execution container my-proj-run-01. After execution, a string of characters is returned. You can use the docker logs command to view the output log of the console.

After the container is started, it can be accessed using the browser. However, it should be noted that the ip address is the ip of the host and the port is the exposed port.

http://192.168.1.132:8085/login.html

3. Upload image

After the image is built, you need to upload it. Whether you upload your own private services or services provided by some websites, the steps can be divided into three steps:

Login, tag, submit

# Sign in
docker login -u admin -p Harbor12345 10.10.10.193
# label
docker tag my-proj:latest 10.10.10.193/library/my-proj:latest
# Submit
docker push 10.10.10.193/library/myproj:latest

I use the harbor image service built by myself here. If I use the image service provided by other websites, I usually have a detailed description of these steps. Just refer to the implementation.

In this way, you can successfully build your own private image. Similarly, you can pull it back.

docker pull 10.10.10.193/library/server-framework:latest

Only when the image is configured with a public image library, you can not log in for this pull action. Otherwise, you need to log in first and then pull.

Appendix: pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <groupId>com.dsp.cloud</groupId>
    <artifactId>devuser</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dev-user</name>
    <description>Developer account</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <dependencies>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.4</version>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.15</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Posted by brettpower on Thu, 25 Nov 2021 20:25:33 -0800