1、找个带sqlplus的镜像
从docker hub上下载https://hub.docker.com/r/sflyr/sqlplus
docker pull sflyr/sqlplus
2、在k8s中运行
由于该镜像启动后没有运行的程序,所以会很快退出,导致k8s一直报错:Back-off restarting failed container.
是因为,容器内PID为1的进程退出导致(通常用户在构建镜像执行CMD时,启动的程序,均是PID为1)
所以,该镜像不能直接使用,需要自己加工一下。
使用dockerfile重新构建一个镜像。
[root@hadoop01 dockerfiledir]# more Dockerfile
FROM sflyr/sqlplus:latest
CMD ["/bin/bash", "-ce", "tail -f /dev/null"]
[root@hadoop01 dockerfiledir]#
构建
[root@hadoop01 dockerfiledir]# docker build -t sqlplus:v20230424 .
Sending build context to Docker daemon 2.048 kB
Step 1/2 : FROM sflyr/sqlplus:latest
---> d0cab823fcdc
Step 2/2 : CMD /bin/bash -ce tail -f /dev/null
---> Running in b7572268d95e
---> f46ed69aa12d
Removing intermediate container b7572268d95e
Successfully built f46ed69aa12d
查看镜像包
结束后会生成镜像包,使用docker images查看
[root@hadoop01 dockerfiledir]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sqlplus v20230424 f46ed69aa12d About an hour ago 461 MB
保存镜像包
[root@hadoop01 ~]# docker save -o sqlplus.tar sqlplus
[root@hadoop01 ~]# ls -lrt
total 1796316
-rw-------. 1 root root 467651072 Apr 24 02:21 sqlplus.tar
此处生成的镜像包可以直接加载使用。
如果是在启动的时候执行其他脚本,则
Dockerfile为
[root@hadoop01 dockerfiledir]# more Dockerfile
FROM sflyr/sqlplus:latest
COPY ["start.sh", "/root/start.sh"]
WORKDIR /root
CMD ["/bin/bash", "./start.sh"]
[root@hadoop01 dockerfiledir]#
注意:start.sh为Dockerfile同级目录下的文件,如下
[root@hadoop01 dockerfiledir]# ls -lrt
total 456704
-rw-r--r--. 1 root root 52 Apr 24 03:53 start.sh
-rw-r--r--. 1 root root 108 Apr 24 03:58 Dockerfile