[root@izwz9eftauv7x69f5jvi96z docker]# vim /usr/lib/systemd/system/docker.service #修改ExecStart這行 ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock #從新加載配置文件 [root@izwz9eftauv7x69f5jvi96z docker]# systemctl daemon-reload #重啓服務 [root@izwz9eftauv7x69f5jvi96z docker]# systemctl restart docker.service #查看端口是否開啓 [root@izwz9eftauv7x69f5jvi96z docker]# netstat -nlpt #直接curl看是否生效 [root@izwz9eftauv7x69f5jvi96z docker]# curl http://127.0.0.1:2375/info
一、新建一個Spring Boot工程,在工程裏寫一個REST接口,以下,簡單返回一個字符串。java
/** * DemoController * * @author weixiang.wu * @date 2018-11-08 17:26 **/ @RestController public class DemoController { @RequestMapping("test") public String demo() { return "demo for docker"; } }
二、修改pom文件,添加properties,添加plugin:web
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>dockercon</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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>wuweixiang</docker.image.prefix> </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> <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> <dockerDirectory></dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> </project>
三、配置Dockerfile文件:在工程根目錄下新建Dockerfile文件,內容以下:spring
#指定基礎鏡像,在其上進行定製 FROM java:8 #維護者信息 MAINTAINER wuweixiang <wuweixiang.alex@gmail.com> #這裏的 /tmp 目錄就會在運行時自動掛載爲匿名卷,任何向 /tmp 中寫入的信息都不會記錄進容器存儲層 VOLUME /tmp #複製上下文目錄下的target/demo-1.0.0.jar 到容器裏 COPY target/demo-1.0.0.jar demo-1.0.0.jar #bash方式執行,使demo-1.0.0.jar可訪問 #RUN新創建一層,在其上執行這些命令,執行結束後, commit 這一層的修改,構成新的鏡像。 RUN bash -c "touch /demo-1.0.0.jar" #聲明運行時容器提供服務端口,這只是一個聲明,在運行時並不會由於這個聲明應用就會開啓這個端口的服務 EXPOSE 8080 #指定容器啓動程序及參數 <ENTRYPOINT> "<CMD>" ENTRYPOINT ["java","-jar","demo-1.0.0.jar"]
四、建立docker鏡像docker
五、啓動鏡像apache
也能夠直接用在docker客戶端上直接執行:vim
docker run -p 28080:8080 --name wwx wuweixiang/demo:1.0
六、客戶端訪問bash