我们自定义个自己的centos,由于Docker中基础centos镜像是一个centos最小内核版本,大小只有几百兆。因此,该centos中是没有 vim 和 ifconfig 命令的,我们自定义的 centos 将安装vim 和 ifconfig(即net-tools)工具包。
Dockerfile文件内容如下:
# 继承基础的centos镜像 FROM centos # 指定作者信息 MAINTAINER myName<myName@163.com> # 定义环境变量 ENV mypath /tmp # 设置当前的工作目录,通过环境变量指定 WORKDIR $mypath # 运行 yum 命令,安装 vim 和 net-tools 工具 RUN yum -y install vim RUN yum -y install net-tools # 暴露 80 端口,运行容器时就可以使用“-p 宿主机端口:容器中端口”指定端口映射 EXPOSE 80 # 容器启动时运行的指令 # 启动容器时,只运行最后一条CMD指令 CMD echo $mypath CMD echo "success-------------------------ok" CMD /bin/bash
通过 docker build -f Dockerfile2 -t mycentos . 命令构建镜像。其中,-f 用来指定你的 Dockerfile 文件路径;-t 用来指定创建镜像的名称;运行过程如下:
$ docker build -f Dockerfile2 -t mycentos . Sending build context to Docker daemon 30.21kB Step 1/10 : FROM centos latest: Pulling from library/centos 8ba884070f61: Pull complete Digest: sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c Status: Downloaded newer image for centos:latest ---> 9f38484d220f Step 2/10 : MAINTAINER myName<myName@163.com> ---> Running in a6a9d55e4d45 Removing intermediate container a6a9d55e4d45 ---> d7381d0b5a77 Step 3/10 : ENV mypath /tmp ---> Running in 8869a2f9705b Removing intermediate container 8869a2f9705b ---> 2fb2ec363a4a Step 4/10 : WORKDIR $mypath ---> Running in 2034e28d3c11 Removing intermediate container 2034e28d3c11 ---> ed1ec63c5f95 Step 5/10 : RUN yum -y install vim ---> Running in a410721bf127 Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.163.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ...more... Dependency Installed: gpm-libs.x86_64 0:1.20.7-5.el7 ...more... which.x86_64 0:2.20-7.el7 Complete! Removing intermediate container a410721bf127 ---> 14d5b8e356be Step 6/10 : RUN yum -y install net-tools ---> Running in 9e42eb5b854c Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.163.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ...more... Installed: net-tools.x86_64 0:2.0-0.24.20131004git.el7 Complete! Removing intermediate container 9e42eb5b854c ---> 7488c7837498 Step 7/10 : EXPOSE 80 ---> Running in 59fb82c9351a Removing intermediate container 59fb82c9351a ---> 2208c2b5c5a4 Step 8/10 : CMD echo $mypath ---> Running in 4e6e1f1f38b0 Removing intermediate container 4e6e1f1f38b0 ---> 79ffde662850 Step 9/10 : CMD echo "success-------------------------ok" ---> Running in f3da03cff7e4 Removing intermediate container f3da03cff7e4 ---> 3c2beb3a7025 Step 10/10 : CMD /bin/bash ---> Running in 79e70ac3bb38 Removing intermediate container 79e70ac3bb38 ---> 1a7a1d7e2839 Successfully built 1a7a1d7e2839 Successfully tagged mycentos:latest
注意:如果安装 vim 和 net-tools 过程中出现告警信息,不要理会。
使用“docker images”查看刚刚构建的镜像。如下:
# 查看我们刚才创建的镜像 mycentos $ docker images mycentos REPOSITORY TAG IMAGE ID CREATED SIZE mycentos latest 0fffcc4c0210 18 seconds ago 467MB
最后我们可以使用“docker run -it”命令运行镜像。如下:
# 运行容器,仔细查看,进入容器直接进入了 /tmp 目录 $ docker run -it mycentos [root@e8c209d5b105 tmp]$ # 映射端口到 8080 $ docker run -it -p 8080:80 mycentos [root@e7609681763d tmp]$