主要分为以下步骤:

1、本地安装docker

2、Docker服务端开放Remote API

3、idea中安装插件

4、应用中编写Dockerfile

5、启动/停止删除

 

1、本地安装docker

在以下网址下载,主要安装 Docker Toolbox 跟 Docker Server ,安装教程在这就不写了

https://www.docker.com/community-edition#/download

2、Docker服务端开放Remote API

Docker服务端:

先备份docker配置

cp /usr/lib/systemd/system/docker.service /usr/lib/systemd/system/docker.service_bak

编辑docker.service 

 

 vi /usr/lib/systemd/system/docker.service 

在 ExecStart=/usr/bin/dockerd-current  后 增加 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

增加后如下,保存并退出

 

ExecStart=/usr/bin/dockerd-current  -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \

          --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \

          --default-runtime=docker-runc \

          --exec-opt native.cgroupdriver=systemd \

          --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \

          --seccomp-profile=/etc/docker/seccomp.json \

          $OPTIONS \

          $DOCKER_STORAGE_OPTIONS \

          $DOCKER_NETWORK_OPTIONS \

          $ADD_REGISTRY \

          $BLOCK_REGISTRY \

          $INSECURE_REGISTRY \

          $REGISTRIES

重新启动服务

 sudo systemctl daemon-reload

 systemctl restart docker

本地测试2375是否通

其他docker版本开放Remote API 请参照https://www.cnblogs.com/sfnz/p/5670466.html

[root@localhost ~]# curl http://127.0.0.1:2375

{"message":"page not found"}

局域网测试是否通 用telnet 命令或 在浏览器里直接访问

 

telnet 192.168.5.100 2375

Trying 192.168.5.100...

Connected to 192.168.5.100.

Escape character is '^]'.

 

如果服务器测试都不通,那可能没有启动好,

如果服务器测试通,外网不通那检查一下防火墙

 

3、idea中安装插件

配置服务器地址

 

依次打开view -> toolwindows -> Docker

4、应用中集成Docker

pom.xml

<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/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zzf</groupId>
	<artifactId>bootDemo</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
  	<description>qq:252462807</description>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<docker.image.prefix>springio</docker.image.prefix>
	</properties>

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


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<build>
		<finalName>bootDemo</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>docker-maven-plugin</artifactId>
				<version>1.0.0</version>
				<configuration>
					<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
					<dockerHost>http://192.168.5.100:2375</dockerHost>
					<dockerDirectory>src/main/docker</dockerDirectory>
					<resources>
						<resource>
							<targetPath>/</targetPath>
							<directory>${project.build.directory}</directory>
							<include>${project.build.finalName}.jar</include>
						</resource>
					</resources>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

application.yml

server:
  port: 9191
SampleController.java
package com.zzf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by zzf
 * qq:252462807
 * 2018-05-09 09:29
 */
@Controller
 public class SampleController {

    @ResponseBody
    @RequestMapping(value = "/")
    String home(){
        return "Hello Docker World";
    }

}
HelloApplication.java
package com.zzf;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
 public class HelloApplication {

	public static void main(String[] args) {
		SpringApplication.run(HelloApplication.class, args);
	}
}

构建Dockerfile

FROM frolvlad/alpine-oraclejdk8
#指定维护者信息
MAINTAINER zzf
VOLUME /tmp
ADD target/bootDemo.jar bootDemo.jar
RUN sh -c 'touch /bootDemo.jar'
ENV JAVA_OPTS=""
EXPOSE 9191
#指定执行启动spring boot小项目     ENTRYPOINT 为容器启动后执行的命令
ENTRYPOINT ["java","-jar","/bootDemo.jar"]

这是我的目录结构

 

5、启动/停止删除

先测试本地是否可以访问

启动后,浏览器访问 http://127.0.0.1:9191

本地可以正常访问后

发布到docker中 并访问

 

运行配置 打开 edit Configurations

第1处: images tag 镜像名

第2处:container name 容器名

第3处:bind ports 需要暴露的端口,需跟dockerfile 中的 EXPOSE 9191 保持一致

在服务端,也可以查到所创建的镜像跟容器

浏览器访问

自此配置完成。如有不妥之处,欢迎指出。

后期将结合jenkins 一起使用

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐