First, you need to enable docker remote access function so that remote operations can be performed.
CentOS 6
Modify the /etc/default/docker file, and take effect after restart (service docker restart).
DOCKER_OPTS="-H=unix:///var/run/docker.sock -H=0.0.0.0:2375"
CentOS 7
Open the /usr/lib/systemd/system/docker.service file and modify the ExecStart line.
Copy the code as follows: ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
Take effect after restart
systemctl daemon-reload systemctl restart docker.service
Whether the test takes effect
curl http://127.0.0.1:2375/info
New Maven project
The pom.xml configuration is as follows:
<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"> <modelVersion>4.0.0</modelVersion> <groupId>test.springboot.docker</groupId> <artifactId>docker-springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.14</version> <configuration> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <dockerHost>http://192.168.1.200:2375</dockerHost> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build></project>imageName: Specifies the image's namedockerDirectory: Specify the location of the DockerfiledockerHost: Specify Docker remote API addressresources: refers to files that need to be put together with Dockerfile and used when building images. Generally, application jar packages need to be includedCreate Java classes
package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class Application { @RequestMapping("/") public String home() { return "Hello Docker World"; } public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Create a Dockerfile
Create a file named Dockerfile in the src/main/docker directory, and the configuration is as follows:
FROM javaVOLUME /tmpADD docker-springboot-0.0.1-SNAPSHOT.jar app.jarRUN bash -c 'touch /app.jar'ENV JAVA_OPTS=""ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
FROM java: refers to the java image provided by the official Docker Hub. With this basic image, the Dockerfile can directly obtain its status through the FROM instruction - that is, java is already installed in the container, and then run the Spring Boot application through custom commands.
VOLUME /tmp: Create /tmp directory and persist to Docker data folder, because the embedded Tomcat container used by Spring Boot uses /tmp as the working directory by default.
ADD docker-springboot-0.0.1-SNAPSHOT.jar app.jar: Copy the application jar package to /app.jar
ENTRYPOINT: Indicates the command executed by default after the container is run
The complete directory structure is as follows:
Run the following command to create a Docker image:
package docker:build
Docker startup image
Check whether the project is uploaded successfully
Start the image copy code as follows: docker run -p 8888:8080 springboot/docker-springboot
Access through a browser
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.