- dockerfile已逐渐成为企业交付的标准,我们以后要发布项目,做镜像,就需要编写dockerfile文件。
- DockerFile:构建文件,定义了一切的步骤,源代码
- DockerImage:通过DockerFile构建生成的镜像,最终发布和运行产品
- Docker容器:容器就是为镜像运行提供服务
DockerFile的指令
FROM: # 基础镜像
MAINTAINER: # 开发者:姓名+邮箱
WORKDIR: # 镜像的工作目录
LABEL: # 设置镜像的标签
VOLUME: # 镜像挂载的目录
EXPOSE: # 镜像的端口
CMD: # 指定这个容器启动时要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT: # 指定这个容器启动时要运行的命令,可以追加命令(可以在命令行后加指令)
COPY: # 类似ADD,将文件拷贝到镜像中
ONBUILD: # 当构建一个被继承DockerFile,这个时候会运行ONBUILD指令(触发指定)
ENV: # 构建的时候设置环境变量
- 查看centos的DockerFile;
- 尝试构建自己的centos,当然也可选用别的,先尝试,没什么可以一步到位
[root@swl123 ~]#
[root@swl123 ~]# cd /home/
[root@swl123 home]# ls
aaaaa.java admin ceshi docker-test-volume mysql
[root@swl123 home]# mkdir dockerfile
[root@swl123 home]# cd dockerfile/
[root@swl123 dockerfile]# ls
[root@swl123 dockerfile]# vim swl-centos
[root@swl123 dockerfile]# cat swl-centos
FROM centos
MAINTAINER swl<651930625@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "--------end--------"
CMD /bin/bash
[root@swl123 dockerfile]#
- 构建,命令后面有个 .
[root@swl123 dockerfile]# docker build -f swl-centos -t swlcentos:1.0 .
....
....
Successfully built 0191db3c9405
Successfully tagged swlcentos:1.0
[root@swl123 dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
swlcentos 1.0 0191db3c9405 2 minutes ago 302MB
mysql 8.0.26 95db2e2bd882 30 hours ago 514MB
nginx latest 4cdc5dd7eaad 2 weeks ago 133MB
tomcat 9.0 36ef696ea43d 2 weeks ago 667MB
tomcat latest 36ef696ea43d 2 weeks ago 667MB
elasticsearch 7.13.3 84840c8322fe 2 weeks ago 1.02GB
redis latest 08502081bff6 3 weeks ago 105MB
portainer/portainer latest 580c0e4e98b0 4 months ago 79.1MB
hello-world latest d1165f221234 4 months ago 13.3kB
-f:dockerfile文件
-t:target目标镜像名
- 测试运行,成功运行
[root@swl123 dockerfile]# docker run -it swlcentos:1.0
[root@7b1c5235cb97 local]# pwd
/usr/local
[root@7b1c5235cb97 local]#
关于docker run 的-it与-d的关系,请看:
这位小哥的博客;
- 查看镜像历史:docker history xxx
[root@swl123 dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
swlcentos 1.0 0191db3c9405 28 minutes ago 302MB
tomcat02 1.0 95e5803af676 19 hours ago 672MB
mysql 5.7 9f1d21c1025a 30 hours ago 448MB
mysql 8.0.26 95db2e2bd882 30 hours ago 514MB
nginx latest 4cdc5dd7eaad 2 weeks ago 133MB
tomcat 9.0 36ef696ea43d 2 weeks ago 667MB
tomcat latest 36ef696ea43d 2 weeks ago 667MB
elasticsearch 7.13.3 84840c8322fe 2 weeks ago 1.02GB
redis latest 08502081bff6 3 weeks ago 105MB
portainer/portainer latest 580c0e4e98b0 4 months ago 79.1MB
hello-world latest d1165f221234 4 months ago 13.3kB
centos latest 300e315adb2f 7 months ago 209MB
[root@swl123 dockerfile]# docker history 0191db3c9405
IMAGE CREATED CREATED BY SIZE COMMENT
0191db3c9405 28 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
6f7ecfe1baec 28 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
9c5035e3862a 28 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
4e5f5743c5f9 28 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
dada750b045b 28 minutes ago /bin/sh -c yum -y install net-tools 27.5MB
cd49f49ee407 28 minutes ago /bin/sh -c yum -y install vim 64.8MB
d07c692132f0 28 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
86cd8f795951 28 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
72805c080bd8 28 minutes ago /bin/sh -c #(nop) MAINTAINER swl<651930625@… 0B
300e315adb2f 7 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 7 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 7 months ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB
[root@swl123 dockerfile]#
实战:
- 准备镜像文件,Tomcat压缩包,jdk的压缩包
[root@swl123 hzl-1]# pwd
/usr/local/hzl-1
[root@swl123 hzl-1]# ls
apache-tomcat-9.0.50.tar.gz Dockerfile hzl-wx-3.6.0.jar jdk-8u291-linux-x64.tar.gz readme.txt
[root@swl123 hzl-1]# ll
total 152800
-rw-r--r-- 1 root root 11507318 Jul 21 14:29 apache-tomcat-9.0.50.tar.gz
-rw-r--r-- 1 root root 144935989 Jun 22 15:43 jdk-8u291-linux-x64.tar.gz
-rw-r--r-- 1 root root 47 Jul 21 14:45 Dockerfile
-rw-r--r-- 1 root root 16 Jul 21 14:46 readme.txt
[root@swl123 hzl-1]#
Dockerfile和readme.txt是新建的,jdk8和Tomcat9有需要下载到这里:
jdk8链接:https://pan.baidu.com/s/1vMWH4ksYLbj3vmSk4ac1rA
提取码:iqyg
–来自百度网盘超级会员V6的分享
Tomcat9链接:https://pan.baidu.com/s/1bpvxSvZZo1scRAU950RLEg
提取码:idm1
–来自百度网盘超级会员V6的分享
- 编写DockerFile文件,官方命令
DockerFile
,build会自动寻找这个文件,就不需要-f
指定了
[root@swl123 hzl-1]# vim Dockerfile
[root@swl123 hzl-1]# cat Dockerfile
FROM centos
MAINTAINER swl<651930625@qq.com>
COPY readme.txt /usr/local/hzl-1/readme.txt
ADD jdk-8u291-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.50.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local/
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_291
ENV CLASSPATH $JAVA_HOME/lib/
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.50
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.50
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.50/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.50/bin/logs/catalina.out
- 构建镜像:docker build -t mytomcat .
不用-f指定dockerfile文件,docker会自动寻找 Dockerfile
[root@swl123 hzl-1]# docker build -t mytomcat .
Sending build context to Docker daemon 156.5MB
Step 1/15 : FROM centos
---> 300e315adb2f
Step 2/15 : MAINTAINER swl<651930625@qq.com>
---> Using cache
---> 72805c080bd8
Step 3/15 : COPY readme.txt /usr/local/hzl-1/readme.txt
---> 242dc439b1c7
....
....
....
Step 15/15 : CMD /usr/local/apache-tomcat-9.0.50/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.50/bin/logs/catalina.out
---> Running in c4b8b1e6a4ac
Removing intermediate container c4b8b1e6a4ac
---> 33f852eb9e8a
Successfully built 33f852eb9e8a
Successfully tagged mytomcat:latest
[root@swl123 hzl-1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat latest 33f852eb9e8a About a minute ago 650MB
swlcentos 1.0 0191db3c9405 5 hours ago 302MB
swl/centos 1.0 a281d33bf80c 18 hours ago 209MB
tomcat02 1.0 95e5803af676 23 hours ago 672MB
mysql 5.7 9f1d21c1025a 35 hours ago 448MB
mysql 8.0.26 95db2e2bd882 35 hours ago 514MB
nginx latest 4cdc5dd7eaad 2 weeks ago 133MB
tomcat 9.0 36ef696ea43d 2 weeks ago 667MB
tomcat latest 36ef696ea43d 2 weeks ago 667MB
elasticsearch 7.13.3 84840c8322fe 2 weeks ago 1.02GB
redis latest 08502081bff6 3 weeks ago 105MB
portainer/portainer latest 580c0e4e98b0 4 months ago 79.1MB
hello-world latest d1165f221234 4 months ago 13.3kB
centos latest 300e315adb2f 7 months ago 209MB
[root@swl123 hzl-1]#
- 启动镜像:
docker run -d -p 9090:8080 --name mytomcat -v /usr/local/build/tomcat/test:/usr/local/apache-tomcat-9.0.50/webapps/test -v /usr/local/build/tomcat/logs/:/usr/local/apache-tomcat-9.0.50/logs mytomcat
[root@swl123 hzl-1]# docker run -d -p 9090:8080 --name mytomcat -v /usr/local/build/tomcat/test:/usr/local/apache-tomcat-9.0.50/webapps/test -v /usr/local/build/tomcat/logs/:/usr/local/apache-tomcat-9.0.50/logs mytomcat
[root@swl123 hzl-1]# docker exec -it d649be98abe1 /bin/bash
[root@d649be98abe1 local]# pwd
/usr/local
[root@d649be98abe1 local]# ls
apache-tomcat-9.0.50 bin etc games hzl-1 include jdk1.8.0_291 lib lib64 libexec sbin share src
- 测试:curl localhost:9090
[root@swl123 hzl-1]# curl localhost:9090
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Apache Tomcat/9.0.50</title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<link href="tomcat.css" rel="stylesheet" type="text/css" />
</head>
- 编辑镜像(由于做了卷挂载,可以直接在本地编写项目发布)
- 发布镜像
DockerHub
- 注册账号
- 登录
报错:Error response from daemon: Get https://registry-1.docker.io/v2/: read tcp 172.25.22.136:35350->107.23.149.57:443: read: connection reset by peer
关闭防火墙试试?systemctl stop firewalld
[root@swl123 docker]# docker login -u hzlhub
Password:
Error response from daemon: Get https://registry-1.docker.io/v2/: read tcp 172.25.22.133:45326->54.236.163.63:443: read: connection reset by peer
[root@swl123 docker]# firewall-cmd --query-port=443/tcp
no
[root@swl123 docker]# systemctl stop firewalld
[root@swl123 docker]# docker login -u hzlhub
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
- 我又奇怪了,也不是没有尝试过网上的方法,只有关闭防火墙有效
- 发布:docker push mytomcat
[root@swl123 docker]# docker push mytomcat
Using default tag: latest
The push refers to repository [docker.io/library/mytomcat]
af4e845b1054: Retrying in 1 second
7ed05236e4be: Preparing
ea22a385d602: Preparing
e3a459387469: Retrying in 1 second
2653d992f4ef: Preparing
denied: requested access to the resource is denied
[root@swl123docker]#
阿里云镜像服务器上
- 容器镜像服务->实例列表->+号创建
- 创建命名空间
- 创建镜像实例
- docker流程