Docker - DockerFile

Docker - DockerFile

在这里插入图片描述

DockerFile 描述

dockerfile 是用来构建docker镜像的文件!命令参数脚本!

构建步骤:

  1. 编写一个dockerfile 文件
  2. docker build 构建成为一个镜像
  3. docker run 运行脚本
  4. docker push 发布镜像(dockerhub,阿里云镜像仓库!)

通过 Docker Hub 查看Centos,可以跳转到github查看构建代码

在这里插入图片描述

DockerFIle 构建

基础知识

  1. 每个保留关键字(指令)都是必须是大写字母

  2. 执行从上到下顺序执行

  3. #表示注释

  4. 每一个指令都会创建提交一个新的镜像层,并提交!

    在这里插入图片描述

dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件。

docker镜像逐渐成为企业交付的标准,必须掌握!

步骤:开发,部署,运维

Dockerfile:构建文件,定义了一切的步骤,源代码

DockerImages:通过DockerFile构建生成的镜像,最终发布和运行的产品!

Docke容器:容器就是镜像运行起来提供服务器

DockerFile 指令

FROM			# 基础镜像,一切从这里开始构建
MAINTAINER		# 镜像是谁写的,姓名+邮箱
RUN				# 镜像构建的时候需要运行的命令
ADD				# 步骤:tomcat镜像,这个tomcat压缩包!添加内容
WORKDIR			# 镜像的工作目录
VOLUME			# 挂载目录
EXPOSE			# 保留端口配置
CMD				# 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT		# 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD			# 当构建一个被继承 Dockerfile 这个时候就会运行 ONBUILD 的指定。触发指令
COPY			# 类似ADD,将我们文件拷贝到镜像中
ENV				# 构建的时候设置环境变量

在这里插入图片描述

DockerFile 测试

# 1 编写Dockerfile文件
[root@iZ2zeg7mctvft5renx1qvbZ dockerfiles]# cat mycentos                                                                                                                                                    
FROM centos:7                                                                                                                                                                                               
MAINTAINER wys<199@163.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

# 2 构建 Docker 镜像
[root@iZ2zeg7mctvft5renx1qvbZ dockerfiles]# docker build -f mycentos -t mycentos:0.1 .
[+] Building 67.7s (8/8) FINISHED                                                                                                                                                            docker:default
 => [internal] load build definition from mycentos                                                                                                                                                     0.0s
 => => transferring dockerfile: 237B                                                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                                                      0.0s
 => => transferring context: 2B                                                                                                                                                                        0.0s
 => [internal] load metadata for docker.io/library/centos:7                                                                                                                                            3.0s
 => [1/4] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4                                                                                     39.5s
 => => resolve docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4                                                                                      0.0s
 => => sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 1.20kB / 1.20kB                                                                                                         0.0s
 => => sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f 529B / 529B                                                                                                             0.0s
 => => sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9 2.75kB / 2.75kB                                                                                                         0.0s
 => => sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 76.10MB / 76.10MB                                                                                                      35.8s
 => => extracting sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc                                                                                                              3.3s
 => [2/4] WORKDIR /usr/local                                                                                                                                                                           0.9s
 => [3/4] RUN yum -y install vim                                                                                                                                                                      19.9s
 => [4/4] RUN yum -y install net-tools                                                                                                                                                                 2.0s
 => exporting to image                                                                                                                                                                                 2.3s 
 => => exporting layers                                                                                                                                                                                2.2s 
 => => writing image sha256:afbe5424adac09891eefa5feabf9584f01638e7475eaca1789535fbe499c5ebd                                                                                                           0.0s 
 => => naming to docker.io/library/mycentos:0.1
 
 
 # 查看镜像的生成过程
 [root@iZ2zeg7mctvft5renx1qvbZ dockerfiles]# docker history afbe5424adac
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
afbe5424adac   6 minutes ago   CMD ["/bin/sh" "-c" "/bin/bash"]                0B        buildkit.dockerfile.v0
<missing>      6 minutes ago   CMD ["/bin/sh" "-c" "echo \"----end----\""]     0B        buildkit.dockerfile.v0
<missing>      6 minutes ago   CMD ["/bin/sh" "-c" "echo $MYPATH"]             0B        buildkit.dockerfile.v0
<missing>      6 minutes ago   EXPOSE map[80/tcp:{}]                           0B        buildkit.dockerfile.v0
<missing>      6 minutes ago   RUN /bin/sh -c yum -y install net-tools # bu…   198MB     buildkit.dockerfile.v0
<missing>      6 minutes ago   RUN /bin/sh -c yum -y install vim # buildkit    284MB     buildkit.dockerfile.v0
<missing>      6 minutes ago   WORKDIR /usr/local                              0B        buildkit.dockerfile.v0
<missing>      6 minutes ago   ENV MYPATH=/usr/local                           0B        buildkit.dockerfile.v0
<missing>      6 minutes ago   MAINTAINER wys<199@163.com>                     0B        buildkit.dockerfile.v0
<missing>      2 years ago     /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      2 years ago     /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      2 years ago     /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4…   204MB

ENTRYPOINT & CMD 区别

【ENTRYPOINT】:追加执行 【CMD】:替换执行

CMD				# 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT		# 指定这个容器启动的时候要运行的命令,可以追加命令

# CMD 测试
[root@iZ2zeg7mctvft5renx1qvbZ dockerfiles]# cat mycentoscmd 
FROM centos
CMD ["ls","-a"]
[root@iZ2zeg7mctvft5renx1qvbZ dockerfiles]# docker run mycentoscmd
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@iZ2zeg7mctvft5renx1qvbZ dockerfiles]# docker run mycentoscmd -l
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container:

# CMD 测试
[root@iZ2zeg7mctvft5renx1qvbZ dockerfiles]# cat mycentos-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
[root@iZ2zeg7mctvft5renx1qvbZ dockerfiles]# docker run  mycentos-entrypoint 
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@iZ2zeg7mctvft5renx1qvbZ dockerfiles]# docker run  mycentos-entrypoint -l
total 0
drwxr-xr-x   1 root root   6 Nov 10 08:06 .
drwxr-xr-x   1 root root   6 Nov 10 08:06 ..
-rwxr-xr-x   1 root root   0 Nov 10 08:06 .dockerenv
lrwxrwxrwx   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root 340 Nov 10 08:06 dev
drwxr-xr-x   1 root root  66 Nov 10 08:06 etc
drwxr-xr-x   2 root root   6 Nov  3  2020 home
lrwxrwxrwx   1 root root   7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root   9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root   6 Sep 15  2021 lost+found
drwxr-xr-x   2 root root   6 Nov  3  2020 media
drwxr-xr-x   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x 132 root root   0 Nov 10 08:06 proc
dr-xr-x---   2 root root 162 Sep 15  2021 root
drwxr-xr-x  11 root root 163 Sep 15  2021 run
lrwxrwxrwx   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x  13 root root   0 Nov 10 08:06 sys
drwxrwxrwt   7 root root 171 Sep 15  2021 tmp
drwxr-xr-x  12 root root 144 Sep 15  2021 usr
drwxr-xr-x  20 root root 262 Sep 15  2021 var

构建 - tomcat

  1. 准备镜像文件 tomcat 压缩包,jdk 压缩包
  2. 编写dockerfile文件
[root@iZ2zeg7mctvft5renx1qvbZ tomcat]wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.82/bin/apache-tomcat-9.0.82.tar.gz

[root@iZ2zeg7mctvft5renx1qvbZ tomcat]wget --no-check-certificate --no-cookies --header "Cookie:oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm
[root@iZ2zeg7mctvft5renx1qvbZ tomcat]wget --no-check-certificate --no-cookies --header "Cookie:oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz


[root@iZ2zeg7mctvft5renx1qvbZ tomcat]# pwd
/root/dockerfiles/tomcat
[root@iZ2zeg7mctvft5renx1qvbZ tomcat]# ls
apache-tomcat-9.0.82.tar.gz  jdk-8u131-linux-x64.tar.gz  readme.txt


# 编写dockerfile文件,官方命名 `Dockerfile`, build会自动寻找这个文件,就不需要-f 指定了
FROM centos:7
MAINTAINER maomao<1213213@qq.com>

# 复制文件
COPY readme.txt /usr/local/readme.txt

# 将压缩包复制到镜像
ADD jdk-8u131-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.82.tar.gz /usr/local/

# 安装vim
RUN yum -y install vim

# 设置工具目录
ENV MYPATH /usr/local
WORKDIR $MYPATH

# 设置环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_131
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.82
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.82
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

# 暴露端口
EXPOSE 8080

# 执行tomcat
CMD /usr/local/apache-tomcat-9.0.82/bin/startup.sh && tail -F /url/local/apache-tomcat-9.0.82/bin/logs/catalina.out


# 执行 build docker镜像
[root@iZ2zeg7mctvft5renx1qvbZ tomcat]# docker build -t tomcat .
[+] Building 26.1s (11/11) FINISHED                                                                                                                                                             docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                                      0.0s
 => => transferring dockerfile: 804B                                                                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                                                                         0.0s
 => => transferring context: 2B                                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/centos:7                                                                                                                                               0.7s
 => CACHED [1/6] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4                                                                                  0.0s
 => [internal] load build context                                                                                                                                                                         0.0s
 => => transferring context: 126B                                                                                                                                                                         0.0s
 => [2/6] COPY readme.txt /usr/local/readme.txt                                                                                                                                                           0.0s
 => [3/6] ADD jdk-8u131-linux-x64.tar.gz /usr/local/                                                                                                                                                      5.3s
 => [4/6] ADD apache-tomcat-9.0.82.tar.gz /usr/local/                                                                                                                                                     0.3s
 => [5/6] RUN yum -y install vim                                                                                                                                                                         15.6s
 => [6/6] WORKDIR /usr/local                                                                                                                                                                              0.1s
 => exporting to image                                                                                                                                                                                    3.9s
 => => exporting layers                                                                                                                                                                                   3.9s
 => => writing image sha256:b1ef32328c3c25f035586a53f3ee10778892a593780232e2c6cf139e9ca8659e                                                                                                              0.0s
 => => naming to docker.io/library/tomcat                                                                                                                                                                 0.0s
[root@iZ2zeg7mctvft5renx1qvbZ tomcat]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
tomcat       latest    b1ef32328c3c   10 seconds ago   881MB


# 启动该镜像的窗口
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -d -p 8080:8080 --name tomcat -v /home/build/tomcat/test/:/usr/local/apache-tomcat-9.0.82/webapps/test -v /home/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.82/logs tomcat
06f1d3e98ec482bbba48e57d96c11867bbdbe61474d0674e81556da0ffc9d226

# 编写 test/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID"
         version="2.5">

</web-app>

# 编写 test/index.jsp 进行测试
<% System.out.println("----"); %>

发布镜像 -待注册账号

网络原因:这里没有实际操作过

  1. 地址:https://hub.docker.com/ 注册自己的账号!
  2. 确定这个账号可以登录
  3. 在服务器上提交自己的镜像
[root@iZ2zeg7mctvft5renx1qvbZ tomcatlogs]# docker login --help

Usage:  docker login [OPTIONS] [SERVER]

Log in to a registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username
  1. 登录完毕后就可以提交镜像了,就是一步 docker push

阿里云发布

  1. 登录阿里云

  2. 进入【容器镜像服务】并 开启个人实例 【创建命名空间,创建镜像仓库】

  3. 按操作指南进行操作即可

    镜像仓库名好比于当前你发布的容器名

# 登录
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker login --username=*** registry.cn-beijing.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
[root@iZ2zeg7mctvft5renx1qvbZ ~]#

# 拉取镜像
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker pull registry.cn-beijing.aliyuncs.com/am98/tomcat:1.0
1.0: Pulling from am98/tomcat
2d473b07cdd5: Already exists
2045650e548c: Already exists
ca2b114dc7e9: Already exists
fa427a442e19: Already exists
1e0f8f1a4b3d: Already exists
4f4fb700ef54: Already exists
Digest: sha256:c7011cb830a34e26902ae7ac1c5f6c379088d9fde276f7c93942328a85f9f3b7
Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/am98/tomcat:1.0
registry.cn-beijing.aliyuncs.com/am98/tomcat:1.0
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker images
REPOSITORY                                     TAG       IMAGE ID       CREATED       SIZE
registry.cn-beijing.aliyuncs.com/am98/tomcat   1.0       b1ef32328c3c   2 hours ago   881MB

# 修改镜像名:版本号
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker tag b1ef32328c3c  registry.cn-beijing.aliyuncs.com/am98/tomcat:1.0

# 提交镜像
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker push registry.cn-beijing.aliyuncs.com/am98/tomcat:1.0
The push refers to repository [registry.cn-beijing.aliyuncs.com/am98/tomcat]
5f70bf18a086: Pushed
bb6cd3e44e82: Pushed
801a43a0316f: Pushed
b3f9f3df333b: Pushed
1d2219e9e671: Pushed
174f56854903: Pushed
1.0: digest: sha256:c7011cb830a34e26902ae7ac1c5f6c379088d9fde276f7c93942328a85f9f3b7 size: 1579
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值