安装
检查版本
➜ ~ docker-machine --version
docker-machine version 0.12.0, build 45c69ad
➜ ~ docker --version
Docker version 17.06.0-ce, build 02c1d87
➜ ~ docker-compose --version
docker-compose version 1.14.0, build c7bdf9e
运行docker镜像,本地没有从Docker Hub拉取
docker run hello-world
docker run -d -p 8888:80 --name webserver nginx
-d 后台运行 -p 宿主机端口:容器端口
docker ps 列出运行的容器
docker images 列出本地镜像
docker rmi image-name 删除本地镜像
docker mi -f $(docker images) 强制删除所有镜像
docker rm container-「name,id」删除容器
docker rm -f $(docker ps -a -q)删除所有容器
docker stop container-id 停止容器
docker kill container-id 强制停止容器
docker start container-id 启动
docker restart container-id 重启
docker attach container-id 进入容器
Dockerfile
vim Dokderfile
写入
FROM nginx
run echo '<h1>Srping Cloud and Docker</h1>' /usr/share/nginx/html/index.html
构建
docker build -t nginx:my .
docker run -d -p 92:80 nginx:my
构建java项目镜像
mvn clean package 打包项目
在jar包所在位置 创建Dockerfile
#基于那个镜像
FROM java:8
#将本地文件挂载到当前容器
VOLUME /tmp
#复制文件到容器
ADD eureka-server-0.0.1-SNAPSHOT.jar /app.jar
#声明需要暴露的端口
EXPOSE 2002
#配置容器启动后执行的命令
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
docker build -t eureka-server:0.0.1 .
docker build -t 仓库名称/镜像名称(:标签) Dockerfile的位置
docker run -d -p 2222:2002 eureka-server:0.0.1
使用Docker Hub 管理Docker镜像
https://hub.docker.com/explore/
docker login 命令登录Docker Hub
创建仓库在Docker Hub
推送镜像
docker push jiaozg/eureka-server:0.0.1
搭建私有仓库
docker run -d -p 5000:5000 --restart always --name registry registry:2
$ docker pull ubuntu
$ docker tag ubuntu localhost:5000/ubuntu
$ docker push localhost:5000/ubuntu
打本地仓库标签
docker tag eureka-server:0.0.1 localhost:5000/eureka-server
推送到本地仓库
docker push localhost:5000/eureka-server
从本地仓库拉取
docker pull localhost:5000/eureka-server
运行
docker run -d -p 2222:2002 {image-id}
快速入门,最简单示例
<!-- 添加docker-maven插件 ,mvn clean package docker:build ,docker images 查看打包的image-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>jiaozg/${project.artifactId}:${project.version}</imageName>
<baseImage>java</baseImage>
<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>
<!-- 指定Dockerfile, mvn clean package docker:build ,docker images 查看打包的image-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>jiaozg/${project.artifactId}:${project.version}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
将Docker镜像push到DockerHub上
· 首先修改Maven的全局配置文件settings.xml,添加以下段落
<servers>
<server>
<id>docker-hub</id>
<username>你的DockerHub用户名</username>
<password>你的DockerHub密码</password>
<configuration>
<email>你的DockerHub邮箱</email>
</configuration>
</server>
</servers>
· 在DockerHub上创建repo,例如:test,如下图
· 项目pom.xml修改为如下:注意imageName的路径要和repo的路径一致
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository
name ... only [a-z0-9-_.] are allowed -->
<!-- 如果要将docker镜像push到DockerHub上去的话,这边的路径要和repo路径一致 -->
<imageName>jiaozg/test</imageName>
<!-- 指定Dockerfile所在的路径 -->
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 以下两行是为了docker push到DockerHub使用的。 -->
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
</configuration>
· 执行命令:
mvn clean package docker:build -DpushImage
· 搞定,等构建成功后,我们会发现Docker镜像已经被push到DockerHub上了。
将镜像push到私有仓库
在很多场景下,我们需要将镜像push到私有仓库中去,这边为了讲解的全面性,私有仓库采用的是配置登录认证的私有仓库。
· 和push镜像到DockerHub中一样,我们首先需要修改Maven的全局配置文件settings.xml,添加以下段落
<servers>
<server>
<id>docker-registry</id>
<username>你的DockerHub用户名</username>
<password>你的DockerHub密码</password>
<configuration>
<email>你的DockerHub邮箱</email>
</configuration>
</server>
</servers>
· 将项目的pom.xml改成如下,
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<!-- 路径为:私有仓库地址/你想要的镜像路径 -->
<imageName>localhost:5000/test-pull-registry</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 与maven配置文件settings.xml一致 -->
<serverId>docker-registry</serverId>
</configuration>
</plugin>
· 执行:
mvn clean package docker:build -DpushImage
稍等片刻,将会push成功。
docker images
· 如果想要从私服上下载该镜像,执行:
docker login localhost:5000 # 然后输入账号和密码
docker pull localhost:5000/test-pull-registry
将插件绑定在某个phase执行
在很多场景下,我们有这样的需求,例如执行mvn clean package 时,自动地为我们构建docker镜像,可以吗?答案是肯定的。我们只需要将插件的goal 绑定在某个phase即可。
所谓的phase和goal,可以这样理解:maven命令格式是:mvn phase:goal ,例如mvn package docker:build 那么,package 和 docker 都是phase,build 则是goal 。
下面是示例:
首先配置属性:
<properties>
<docker.image.prefix>localhost:5000</docker.image.prefix>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<baseImage>java</baseImage>
<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>
</plugins>
</build>
如上,我们只需要添加:
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
即可。本例指的是讲docker的build目标,绑定在package这个phase上。也就是说,用户只需要执行mvn package ,就自动执行了mvn docker:build
前文提到的Dockerfile 可以让用户管理一个单独的容器,那么如果我要管理多个容器呢,例如:我需要管理一个Web应用的同时还要加上其后端的数据库服务容器呢?Compose就是这样的一个工具。让我们看下官网对Compose的定义:
Compose 是一个用于定义和运行多容器的Docker应用的工具。使用Compose,你可以在一个配置文件(yaml格式)中配置你应用的服务,然后使用一个命令,即可创建并启动配置中引用的所有服务。下面我们进入Compose的实战吧。
安装Compose
Compose的安装有多种方式,例如通过shell安装、通过pip安装、以及将compose作为容器安装等等。本文讲解通过shell安装的方式。其他安装方式如有兴趣,可以查看Docker的官方文档:
· 安装完成,测试:
docker-compose —version
Docker Compose入门示例
Compose的使用非常简单,只需要编写一个docker-compose.yml ,然后使用docker-compose 命令操作即可。docker-compose.yml 描述了容器的配置,而docker-compose 命令描述了对容器的操作。我们首先通过一个示例快速入门:
· 我们在eureka-server-0.0.1-SNAPSHOT.jar 所在目录的上一级目录,创建docker-compose.yml 文件。 目录树结构:
· 然后在docker-compose.yml 中添加内容如下:
eureka:
build: ./target
ports:
- "2002:2002"
expose:
- 2002
· 在docker-compose.yml 所在路径执行:
docker-compose up
访问 localhost:2002验证
version: "2"
services:
peer1: # 默认情况下,其他服务可使用服务名称连接到该服务。对于peer2节点,它需连接http://peer1:8761/eureka/,因此,我们可配置该服务的名称为peer1。
image: jiaozg/eureka-server-ha:0.0.1
ports:
- "8000:8000"
environment:
- spring.profiles.active=peer1
peer2:
image: jiaozg/eureka-server-ha:0.0.1
hostname: peer2
ports:
- "8001:8001"
environment:
- spring.profiles.active=peer2
eureka-client:
image: jiaozg/eureka-client:0.0.1
ports:
- "2011:2011"
## 使用Compose编排高可用的Eureka Server。
<!-- 添加docker-maven插件 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>jiaozg/${project.artifactId}:${project.version}</imageName>
<forceTags>true</forceTags>
<baseImage>java</baseImage>
<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>
man clean package docker:build
eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/,http://peer1:8000/eureka/
docker-compose scale eureka-client=3
或 docker-compose up --scale eureka-client=4