Dockerfile镜像制作
需要的文件
[root@localhost tmp]# ll Dockerfile jdk-8u251-linux-x64.tar.gz apache-tomcat-9.0.35.tar.gz
-rw-r--r-- 1 root root 11188192 5月 28 16:12 apache-tomcat-9.0.35.tar.gz
-rwxr-xr-x 1 root root 870 7月 7 11:18 Dockerfile
-rw-r--r-- 1 root root 195132576 5月 28 16:39 jdk-8u251-linux-x64.tar.gz
编写Dockerfile
[root@localhost tmp]# cat Dockerfile
#1.指定基础镜像,并且必须是第一条指令
FROM centos:7
#2.指明该镜像的作者和其电子邮件
MAINTAINER xhx "1135014515@qq.com"
#3.在构建镜像时,指定镜像的工作目录,之后的命令都是基于此工作目录,如果不存在,则会创建目录
WORKDIR /opt
#4.一个复制命令,把jdk安装文件复制到镜像中,语法:ADD <src>... <dest>,注意:jdk*.tar.gz使用的是相对路径
ADD jdk-8u251-linux-x64.tar.gz /xhx/jdk/
ADD apache-tomcat-9.0.35.tar.gz /opt
EXPOSE 8080
#5.配置环境变量
ENV JAVA_HOME=/xhx/jdk/jdk1.8.0_251
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH
ENTRYPOINT /opt/apache-tomcat-9.0.35/bin/startup.sh && tail -f /opt/apache-tomcat-9.0.35/logs/catalina.out
#容器启动时需要执行的命令
#CMD ["java","-version"]
构建镜像
[root@localhost tmp]# docker build -t cemtos7tomcat9jdk8 .
Sending build context to Docker daemon 670.7MB
Step 1/10 : FROM centos:7
---> b5b4d78bc90c
Step 2/10 : MAINTAINER xhx "1135014515@qq.com"
---> Using cache
---> e9339ad8c365
Step 3/10 : ADD jdk-8u251-linux-x64.tar.gz /xhx/jdk/
---> c825cfd6c84a
Step 4/10 : WORKDIR /opt
---> Running in 35dad8279b79
Removing intermediate container 35dad8279b79
---> fb45a3b8e075
Step 5/10 : ADD apache-tomcat-9.0.35.tar.gz /opt
---> e518d183c100
Step 6/10 : EXPOSE 8080
---> Running in 0428392d6e2a
Removing intermediate container 0428392d6e2a
---> 69483c392d32
Step 7/10 : ENV JAVA_HOME=/xhx/jdk/jdk1.8.0_251
---> Running in 86332eec01e9
Removing intermediate container 86332eec01e9
---> 0da4d2a38fb0
Step 8/10 : ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in 748872cd9edb
Removing intermediate container 748872cd9edb
---> 35056bbb255e
Step 9/10 : ENV PATH=$JAVA_HOME/bin:$PATH
---> Running in 3b1bcada03bb
Removing intermediate container 3b1bcada03bb
---> b2ba2e952339
Step 10/10 : ENTRYPOINT /opt/apache-tomcat-9.0.35/bin/startup.sh && tail -f /opt/apache-tomcat-9.0.35/logs/catalina.out
---> Running in 7eebb17a6d3c
Removing intermediate container 7eebb17a6d3c
---> eb90066f69df
Successfully built eb90066f69df
Successfully tagged cemtos7tomcat9jdk8:latest
查看镜像
[root@localhost tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
cemtos7tomcat9jdk8 latest eb90066f69df 48 seconds ago 625MB
运行镜像
[root@localhost tmp]# docker run -itd --name cemtos7tomcat9jdk1.8 cemtos7tomcat9jdk8 -p 80:8080 /bin/bash
3bc3ff364d64811fc7145a9d116f1a1b722faab0ae8c5d6b3baf450e98a376c0
查看容器
[root@localhost tmp]# docker run -itd --name cemtos7tomcat9jdk18 -p 80:8080 cemtos7tomcat9jdk8 /bin/bash
1530d502372ad1b76c28768d0b88098ca1fe15aca6045ac8889d69223645fbcf
[root@localhost tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1530d502372a cemtos7tomcat9jdk8 "/bin/sh -c '/opt/ap…" 2 seconds ago Up 2 seconds 0.0.0.0:80->8080/tcp cemtos7tomcat9jdk18
查看效果
登录镜像仓库
[root@localhost tmp]# sudo docker login --username=17606224944 registry.cn-hangzhou.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
将镜像推送到Registry
$ sudo docker login --username=17606224944 registry.cn-hangzhou.aliyuncs.com
$ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/xinhuixu/xinhuixu_repositories:[镜像版本号]
$ sudo docker push registry.cn-hangzhou.aliyuncs.com/xinhuixu/xinhuixu_repositories:[镜像版本号]
镜像打标签
[root@localhost tmp]# sudo docker tag eb90066f69df registry.cn-hangzhou.aliyuncs.com/xinhuixu/xinhuixu_repositories:v1
[root@localhost tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
cemtos7tomcat9jdk8 latest eb90066f69df 28 minutes ago 625MB
registry.cn-hangzhou.aliyuncs.com/xinhuixu/xinhuixu_repositories v1 eb90066f69df 28 minutes ago 625MB
推送到镜像仓库
[root@localhost tmp]# docker push registry.cn-hangzhou.aliyuncs.com/xinhuixu/xinhuixu_repositories:v1
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/xinhuixu/xinhuixu_repositories]
4d737679f9b7: Pushed
c4f068581146: Pushed
edf3aa290fb3: Pushed
v1: digest: sha256:1001df08bab80d90f87c9c8317d560a548a37992a1b257f0614a929754cf2939 size: 954