背景
在微服务背景下,docker的出现,给环境部署及运维减轻了不少的工作量,至于概念相关的东西这里不再讲解,该例子以Centos7系统作为讲解:
一.docker环境
1.卸载docker容器
$ sudo yum remove docker \\
docker-client \\
docker-client-latest \\
docker-common \\
docker-latest \\
docker-latest-logrotate \\
docker-logrotate \\
docker-engine
2.下载docker
1.安装工具包
sudo yum install -y yum-utils \\
device-mapper-persistent-data \\
lvm2
2.使用阿里云镜像
$ sudo yum-config-manager \\
--add-repo \\
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.查看可安装的docker版本
yum list docker-ce --showduplicates | sort -r
4.安装docker
我这里以18.09.1为例
sudo yum install docker-ce-18.09.1 containerd.io
5.启动docker
$ sudo systemctl start docker
6.验证是否启动成功
$ sudo docker run hello-world
7. 卸载 docker
8.删除安装包:
yum remove docker-ce
9. 删除镜像、容器、配置文件等内容:
rm -rf /var/lib/docker
二.docker操作
1.docker开启远程访问
vim /lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service
Wants=network-online.target
Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
[Install]
WantedBy=multi-user.target
2、重新加载配置文件
systemctl daemon-reload
3、重启Docker服务
systemctl restart docker
4、查看端口是否开启
netstat -nlpt
5、关闭防火墙
systemctl stop firewalld.service
6、禁止防火墙开机启动
systemctl disable firewalld.service
7、直接curl看看是否生效
curl http://127.0.0.1:2375/info
三、IDE配置及项目pom.xml 依赖
1.配置docker连接地址
2.设置镜像代理地址
说明:这里可以申请个人镜像代理地址,输入地址:
https://dev.aliyun.com/
3.添加docker依赖插件及配置
pom.xml配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- Docker镜像上传的用户名 -->
<docker.image.prefix>guoxx</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</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>
<!-- 指定标签 -->
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<!-- 基础镜像版本JDK1.8 -->
<baseImage>java</baseImage>
<!-- 作者本人信息 -->
<maintainer>553605867@qq.com</maintainer>
<!-- 切换到ROOT目录 -->
<workdir>/ROOT</workdir>
<cmd>["java", "-version"]</cmd>
<entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint>
<!-- 指定 DockerFile路径 -->
<!-- <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>-->
<!-- 指定远程 Docker api地址 -->
<dockerHost>http://192.168.111.128:2375</dockerHost>
<!-- 复制Jar包到指定的Docker容器 -->
<resources>
<resource>
<targetPath>/ROOT</targetPath>
<!-- 用于指定用于复制的根目录 -->
<directory>${project.build.directory}</directory>
<!-- 用于指定需要复制的文件 -->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
<executions>
<!-- 当执行maven package时执行maven clean package:build-->
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4.实例代码
@SpringBootApplication
@RestController
public class Demo02Application {
public static void main(String[] args) {
SpringApplication.run(Demo02Application.class, args);
}
@GetMapping("/index")
public String index() {
return "hello world";
}
}
5.镜像文件打包
执行mvn clean package docker:build命令打包,并且会自动将打包后的镜像文件上传到云服务docker容器中。
说明:镜像编译成功,然后回到云服务上用
docker images查看当前镜像是否上传成功
6.docker容器中执行镜像文件
执行命令:docker run --name test -d -p 8081:8081 guoxx/demo02:latest
说明:
--name test 将名称设置为test
-d -p 8081:8081 将外网端口映射为容器里面的端口
guoxx/demo02:latest 容器名称及版本号
7.查看docker容器运行进程
执行
docker ps查看当前容器运行情况
docker ps -a 查看当前容器全部运行情况
8.测试
在地址栏输入http://192.168.111.128:8081/index,返回为helloworld表示部署正常
四、总结
以上就是对docker在线部署的一些操作,其他命令可以下来自己操作,不再详细举例说明!