docker打包java的jar包镜像基础入门---从一个空的虚拟机到镜像搭建全流程
文章目录一、前提二、安装docker2.1 更新 yum 仓库,确保 yum 包都是最新版本2.2 添加Docker repository,这里改为国内阿里云repo2.3 安装要求的软件包2.4 安装docker, 必须之前之前的修改docker源,否则无法安装成功2.5 注意,这里这指定了cgroupdriver=systemd,另外由于国内拉取镜像较慢,最后追加了阿里云镜像加速配置2.6 重
一、前提
- 1.虚拟机:可以联网的centos7
- 2 本文是写给阿旭的,为docker基础,想学好docker,只有多实战操作,再结合网上的理论。
二、安装docker
2.1 更新 yum 仓库,确保 yum 包都是最新版本
备注:yum是一个下载工具, 如果系统较老,可能下载比较久。
yum -y update
2.2 添加Docker repository,这里改为国内阿里云repo
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2.3 安装要求的软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
2.4 安装docker, 必须之前之前的修改docker源,否则无法安装成功
yum install -y docker-ce-18.06.0.ce-3.el7
2.5 注意,这里这指定了cgroupdriver=systemd,另外由于国内拉取镜像较慢,最后追加了阿里云镜像加速配置
备注:全部粘贴-复制-运行即可
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
],
"registry-mirrors": ["https://uyah70su.mirror.aliyuncs.com"]
}
EOF
mkdir -p /etc/systemd/system/docker.service.d
2.6 重启docker服务
systemctl daemon-reload && systemctl restart docker && systemctl enable docker
2.7 查看docker是否安装成功
docker version
三、制作镜像的准备
创建springboot项目
3.1.项目名称
3.2. 导入spring-web依赖
添加简易代码
@SpringBootApplication
@RestController
public class DockerImagesApplication {
public static void main(String[] args) {
SpringApplication.run(DockerImagesApplication.class, args);
}
@RequestMapping("/get")
public String get(){
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println(addr.getHostAddress());
return "测试成功: ip: "+addr;
}
}
运行输出:
url: http://localhost:8080/get
返回结果:测试成功: ip: DESKTOP-5IB8PQF/192.168.137.1
打包
四、安装镜像
4.1 创建文件夹,并把镜像放置到文件夹中
mkdir /usr/local/docker-images
[root@localhost ~]# cd /usr/local/docker-images/
[root@localhost docker-images]# ll
总用量 17204
-rw-r--r--. 1 root root 17615650 4月 17 23:15 docker-images-0.0.1-SNAPSHOT.jar
4.2 创建dockerfile
备注: 该方法是采用 dockerfile 的方式进行创建docker镜像的
cd /usr/local/docker-images/
vi dockerfile
dockerfile的内容
# 基础镜像
FROM openjdk:8-jdk-alpine
RUN \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
mkdir -p /hjmos-gateway
ADD /docker-images-0.0.1-SNAPSHOT.jar /docker-images.jar
#环境变量
ENV JAVA_OPTS="-Duser.timezone=Asia/Shanghai"
EXPOSE 8080
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /docker-images.jar"]
4.3 dockerfile build
填写好dockerfile之后,就是bulid镜像了
[root@localhost docker-images]# docker build -t docker-images:1.0.00 .
Sending build context to Docker daemon 17.62MB
Step 1/6 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/6 : RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone && mkdir -p /hjmos-gateway
---> Using cache
---> 2bba77506175
Step 3/6 : ADD /docker-images-0.0.1-SNAPSHOT.jar /docker-images.jar
---> Using cache
---> e9db52f355fa
Step 4/6 : ENV JAVA_OPTS="-Duser.timezone=Asia/Shanghai"
---> Using cache
---> 95f6bcfba2e4
Step 5/6 : EXPOSE 8080
---> Using cache
---> 8fdd86975317
Step 6/6 : ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /docker-images.jari"]
---> Running in 989aefcdff77
Removing intermediate container 989aefcdff77
---> fc803618a271
Successfully built fc803618a271
Successfully tagged docker-images:1.0.00
查看镜像是否存在
[root@localhost docker-images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-images 1.0.00 45d58cf5879b 12 minutes ago 122MB
openjdk 8-jdk-alpine a3562aa0b991 11 months ago 105MB
[root@localhost docker-images]#
五、运行镜像并测试代码
5.1 运行镜像
[root@localhost docker-images]# docker run -itd -p8080:8080 docker-images:1.0.00 /bin/bash
774ca055b63ce8de24895a76b06949ce65d7d617ca8e4dba48862a5a10b7f8bb
以上:
-i, --interactive=false, 打开STDIN,用于控制台交互
-t, --tty=false, 分配tty设备,该可以支持终端登录,默认为false
-d, --detach=false, 指定容器运行于前台还是后台,默认为false
/bin/bash :
表示载入容器后运行 /bin/bash, docker 中必须保持一个进程的运行,要不然整个容器就会退出
5.2 测试镜像功能
url: http://192.168.102.130:8080/get , 其中 192.168.102.130 为虚拟机地址
返回: 测试成功: ip: 774ca055b63c/172.17.0.2, 表示测试成功了。
5.3 查看运行中镜像
[root@localhost docker-images]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
774ca055b63c docker-images:1.0.00 "sh -c 'java $JAVA_O…" 13 minutes ago Up 12 minutes 0.0.0.0:8080->8080/tcp infallible_beaver
[root@localhost docker-images]#
查看日志
[root@localhost docker-images]# docker logs -ft --tail=all 774ca055b63c
2020-04-18T03:39:43.719916006Z
2020-04-18T03:39:43.719963929Z . ____ _ __ _ _
2020-04-18T03:39:43.719967613Z /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2020-04-18T03:39:43.719970209Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2020-04-18T03:39:43.719972620Z \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2020-04-18T03:39:43.719974878Z ' |____| .__|_| |_|_| |_\__, | / / / /
2020-04-18T03:39:43.719977109Z =========|_|==============|___/=/_/_/_/
2020-04-18T03:39:43.719979311Z :: Spring Boot :: (v2.2.6.RELEASE)
2020-04-18T03:39:43.719981945Z
2020-04-18T03:39:44.019825145Z 2020-04-18 11:39:44.015 INFO 1 --- [ main] c.c.d.DockerImagesApplication : Starting DockerImagesApplication v0.0.1-SNAPSHOT on 774ca055b63c with PID 1 (/docker-images.jar started by root in /)
2020-04-18T03:39:44.029611720Z 2020-04-18 11:39:44.026 INFO 1 --- [ main] c.c.d.DockerImagesApplication : No active profile set, falling back to default profiles: default
2020-04-18T03:39:47.378438909Z 2020-04-18 11:39:47.373 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-04-18T03:39:47.436926232Z 2020-04-18 11:39:47.431 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-18T03:39:47.436944499Z 2020-04-18 11:39:47.431 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-18T03:39:47.694031478Z 2020-04-18 11:39:47.688 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-18T03:39:47.694053276Z 2020-04-18 11:39:47.689 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3437 ms
2020-04-18T03:39:49.417078714Z 2020-04-18 11:39:49.415 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-18T03:39:50.014297756Z 2020-04-18 11:39:50.013 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-04-18T03:39:50.031368202Z 2020-04-18 11:39:50.030 INFO 1 --- [ main] c.c.d.DockerImagesApplication : Started DockerImagesApplication in 7.202 seconds (JVM running for 8.642)
2020-04-18T03:39:50.869277918Z 2020-04-18 11:39:50.853 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-04-18T03:39:50.869302964Z 2020-04-18 11:39:50.853 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-04-18T03:39:50.871833303Z 2020-04-18 11:39:50.869 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 16 ms
2020-04-18T03:39:50.933066082Z 172.17.0.2
-f 默认为false 一致跟踪日志的变化,并返回结果
-t 默认为false 在返回的结果上加上时间戳
–tail = “all” 返回后几行的日志数据.
进入镜像查看jar包运行情况
[root@localhost docker-images]# docker exec -it 774ca055b63c /bin/bash
OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown
[root@localhost docker-images]# docker exec -it 774ca055b63c sh
/ # ps aux
PID USER TIME COMMAND
1 root 0:09 java -Duser.timezone=Asia/Shanghai -jar /docker-images.jar
36 root 0:00 sh
41 root 0:00 ps aux
/ # exit
[root@localhost docker-images]#
注意:进入镜像中可以采用 exit 进行退出。
六、删除镜像
6.1 停止运行中的镜像
[root@localhost docker-images]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
774ca055b63c docker-images:1.0.00 "sh -c 'java $JAVA_O…" 20 minutes ago Up 20 minutes 0.0.0.0:8080->8080/tcp infallible_beaver
[root@localhost docker-images]# docker stop 774ca055b63c
774ca055b63c
[root@localhost docker-images]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost docker-images]#
6.1 删除镜像
[root@localhost docker-images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-images 1.0.00 45d58cf5879b 30 minutes ago 122MB
openjdk 8-jdk-alpine a3562aa0b991 11 months ago 105MB
[root@localhost docker-images]# docker rmi 45d58cf5879b
Error response from daemon: conflict: unable to delete 45d58cf5879b (must be forced) - image is being used by stopped container ae6b26a521e6
以上发现无法删除,可以停止提示中运行中的镜像,或者强制删除
[root@localhost docker-images]# docker rmi -f 45d58cf5879b
Untagged: docker-images:1.0.00
Deleted: sha256:45d58cf5879b158d54252c990de7e2607477cc9a959581d637bf2fdfb287f5e2
Deleted: sha256:8fdd8697531778cc9fdf349a636b38249415d4ed31173fcb8aaf0f03f0d010ad
Deleted: sha256:95f6bcfba2e471970ef5582ecb91c88287d37aa6a988583458b7fefc92d015a4
Deleted: sha256:e9db52f355fa5c8c9d1b417438ff4558e5f91dba1e621b8019cca2b64dfaacb4
Deleted: sha256:2bba775061752a62d89ad120552081ce3f81769b180903a9ed686fa57679d914
[root@localhost docker-images]#
再查看,就没有该镜像了
[root@localhost docker-images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
openjdk 8-jdk-alpine a3562aa0b991 11 months ago 105MB
[root@localhost docker-images]#
补充知识
以上为实际操作,以下为知识的补充,所谓先操作后理论,才读得通透
1.基础知识
每条保留字指令都必须为大写字母且后面要跟随至少一个参数
指令按照从上到下,顺序执行
#表示注释
每条指令都会创建一个新的镜像层,并对镜像进行提交
2.Docker 执行 DockerFile 的大致流程
docker 从基础镜像运行一个容器
执行一条指令并对容器作出修改
执行类似 docker commit 的操作提交一个新的镜像层
docker 再基于刚提交的镜像运行一个新容器
执行 dockerfile 中的下一条指令直到所有指令都执行完成
3.DockerFile 体系结构(保留字指令)
FROM:基础镜像,当前新镜像是基于哪个镜像的
MAINTAINER:镜像维护者的姓名和邮箱地址
RUN:容器构建时需要运行的命令
EXPOSE:当前容器对外暴露出的端口
WORKDIR:指定在创建容器后,终端默认登陆的进来工作目录,一个落脚点
ENV:用来在构建镜像过程中设置环境变量
ADD:将宿主机目录下的文件拷贝进镜像且 ADD 命令会自动处理 URL 和解压 tar 压缩包
COPY:类似 ADD,拷贝文件和目录到镜像中。(COPY src dest 或 COPY ["src","dest"])
VOLUME:容器数据卷,用于数据保存和持久化工作
CMD:指定一个容器启动时要运行的命令,Dockerfile 中可以有多个 CMD 指令,但只有最后一个生效,CMD 会被 docker run 之后的参数替换
ENTRYPOINT:指定一个容器启动时要运行的命令,ENTRYPOINT 的目的和 CMD 一样,都是在指定容器启动程序及参数
ONBUILD:当构建一个被继承的 Dockerfile 时运行命令,父镜像在被子继承后父镜像的 onbuild 被触发
更多推荐
所有评论(0)