编程知识 cdmana.com

Creation and operation of docker custom image

writing | Brother Ping date | 20201112

Mode one : Use DockerFile Create a custom image directly

Basic steps :

  • To write Dockerfile file
  • docker build Create a mirror image
  • docker run Create a container to run

Step 1 Type the project into jar Package and upload

stay IDEA Created in SpringBoot project , There's only one called ”/” The controller method of , After visiting the page output :“Success!”, use Maven To be packaged into demojar.jar.
Tips : stay pom Add the following package configuration to the file :

<build>
    <finalName>demojar</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.gcp.TestDockerBuildApp</mainClass>
                <layout>ZIP</layout>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <!-- Package all the packages that are generated Jar In bag -->
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Upload the packed file to the server /usr/loal/docker Directory .

Step 2 download JDK Of Docker Mirror image

Download from the server JDK Of docker Mirror image , I'm using openjdk:8 edition

# docker pull openjdk:8

You can use the following command to confirm , Is there any local jdk Mirror image :

# docker images

Step 3 Create a build file

Create a new one Docke Image building file :builFile, Tips : To build into the mirror image jar Documentation needs and buildFile In the same directory .

# vim /usr/local/docker/buildFile

Enter the following information in the file :

FROM openjdk:8
VOLUME /var/mydatas
ADD demojar.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080

Parameter description :
// Specify base image , This needs to be written according to the version on the warehouse that you configure
FROM openjdk:latest
// Persistent Directory , Container data volume , For data preservation and persistence
VOLUME /var/mydatas
// Specify source package , The former is yours jar package , Copy the files in the host directory into the image and ADD The command will be processed automatically URL And decompress tar Compressed package
ADD demojar.jar app.jar
// Specifies the command to execute when the container starts
ENTRYPOINT ["java","-jar","/app.jar"]
// External port
EXPOSE 8080

Step 4 establish Docker Mirror image

Use the following command to Docker Image creation :

# docker build -f /usr/local/buildFile -t demo:1.0 .

p.s. There's a point at the end of the order ;
-t Meaning --tag list: Name and optionally a tag in the 'name:tag' format

Step 5 Start the container

Use the following command to create the container and start :

# docker run --name demoJarRun -p 8080:8080 -d demo:1.0

After starting , The log query command can be used to check whether the startup is completed and successful :

# docker logs -f demoJarRun

Mode two : Use IDEA Create a custom image

Step 1 modify Docker Service configuration

Modify the following documents :

# vim /usr/lib/systemd/system/docker.service

stay ExecStart Add the following configuration to the end of the :

-H unix:///var/run/docker.sock -H 0.0.0.0:2375

Pictured :

Reload configuration , restart docker service

# systemctl daemon-reload 
# systemctl restart docker

Step 2 New projects POM To configure

Add the following plug-in information based on the original package plug-in :

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.2</version>
    <configuration>
        <imageName>demojar:2.0</imageName>
        <baseImage>openjdk:8</baseImage>
        <dockerHost>http://81.70.38.8:2375</dockerHost>
        <entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

Step 3 newly added IDEA Run configuration

Get into “Run/Debug Configurations” Interface :

Click on the top left corner “+” Number , newly added Maven Project running configuration :

stay Command line Enter the command to run :

clean package docker:build

single click OK preservation .

Step 4 Run and verify that the creation was successful

Select the run item configured in the previous step , Click Run :

Log in to the remote server to see if the image has been created :

版权声明
本文为[Pingge]所创,转载请带上原文链接,感谢

Scroll to Top