Usage: docker build [OPTIONS] PATH | URL | - [flags]
Options: -t,
--tag list # 镜像名称 -f,
--file string # 指定Dockerfile文件位置
前提:已经准备好了 如下文件(谁需要相关文件的,最下面有我微信,欢迎添加获取):
#现把文件从windows上传输到linux,发现xshell没有安装 rz命令,那么就不支持把windows文件拖进linux机器
[root@ ~]# rz
-bash: rz: 未找到命令
#安装rz工具
[root@ yum install lrzsz
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
base | 3.6 kB ::
docker-ce-stable | 3.5 kB ::
extras | 3.4 kB ::
updates | 3.4 kB ::
正在解决依赖关系
--> 正在检查事务
---> 软件包 lrzsz.x86_64.0.0.12.20-.el7 将被 安装
--> 解决依赖关系完成 依赖关系解决 =================================================================================================================
Package 架构 版本 源 大小
=================================================================================================================
正在安装:
lrzsz x86_64 0.12.-.el7 base k 事务概要
=================================================================================================================
安装 软件包 总下载量: k
安装大小: k
Is this ok [y/d/N]: y
Downloading packages:
lrzsz-0.12.-.el7.x86_64.rpm | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : lrzsz-0.12.-.el7.x86_64 /
验证中 : lrzsz-0.12.-.el7.x86_64 / 已安装:
lrzsz.x86_64 :0.12.-.el7 完毕!
#将windows文件拖进xshell, 会出现图形界面,显示传输进度
[root@ ~]# rz -E
rz waiting to receive.
#确认 dockerfile_.zip传输进了linux宿主机
[root@ ~]# ls
anaconda-ks.cfg dockerfile_.zip nginx.tar
# 发现unzip工具没有安装
[root@ ~]# unzip
-bash: unzip: 未找到命令
#安装unzip工具
[root@ ~]# yum install unzip -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 unzip.x86_64.0.6.-.el7 将被 安装
--> 解决依赖关系完成 依赖关系解决 =================================================================================================================
Package 架构 版本 源 大小
=================================================================================================================
正在安装:
unzip x86_64 6.0-.el7 base k 事务概要
=================================================================================================================
安装 软件包 总下载量: k
安装大小: k
Downloading packages:
unzip-6.0-.el7.x86_64.rpm | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : unzip-6.0-.el7.x86_64 /
验证中 : unzip-6.0-.el7.x86_64 / 已安装:
unzip.x86_64 :6.0-.el7 完毕!
解压 dockerfile_.zip 文件
[root@ ~]# unzip dockerfile_.zip
Archive: dockerfile_.zip
inflating: Dockerfile-nginx
inflating: Dockerfile-php
inflating: Dockerfile-tomcat
inflating: nginx.conf
inflating: php-fpm.conf
inflating: php.ini
[root@ ~]# ls -l
总用量
-rw-------. root root 3月 : anaconda-ks.cfg
-rw-r--r-- root root 11月 : Dockerfile-nginx
-rw-r--r-- root root 11月 : Dockerfile-php
-rw-r--r-- root root 11月 : Dockerfile-tomcat
-rw-r--r-- root root 11月 : dockerfile_.zip
-rw-r--r-- root root 10月 : nginx.conf
-rw-r--r--. root root 3月 : nginx.tar
-rw-r--r-- root root 10月 : php-fpm.conf
-rw-r--r-- root root 11月 : php.ini
[root@ ~]# vi Dockerfile-nginx
[root@ ~]# docker build -t nginx:v1 -f Dockerfile-nginx .
Sending build context to Docker daemon .1MB
Step / : FROM centos:
---> 1e1148e4cc2c
Step / : MAINTAINER www.ctnrs.com
---> Running in bf77ab5a1997
Removing intermediate container bf77ab5a1997
--->
Step / : RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel gd-devel iproute net-tools telnet wget curl && yum clean all && rm -rf /var/cache/yum/*
......
Step 8/9 : EXPOSE 80
---> Running in 53d27a40eef7
Removing intermediate container 53d27a40eef7
---> 39fe3e3e4c0c
Step 9/9 : CMD ["nginx", "-g", "daemon off;"]
---> Running in 17796c30c838
Removing intermediate container 17796c30c838
---> a5412fe37cac
Successfully built a5412fe37cac
Successfully tagged nginx:v1
[root@192 ~]# docker image Usage: docker image COMMAND Manage images Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE Run 'docker image COMMAND --help' for more information on a command. #确认nginx v1镜像构建成功
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1 a5412fe37cac About a minute ago 361MB
nginx latest 881bd08c0b08 9 days ago 109MB
busybox latest d8233ab899d4 3 weeks ago 1.2MB
centos 7 1e1148e4cc2c 3 months ago 202MB
centos latest 1e1148e4cc2c 3 months ago 202MB
java 8 d23bdf5b1b1b 2 years ago 643MB
#构建php镜像,构建比较慢,大约持续 7~8分钟
[root@192 ~]# docker build -t php:v1 -f Dockerfile-php .
......
Successfully built d48e00d7de94
Successfully tagged php:v1
#确认php v1 构建成功
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php v1 d48e00d7de94 34 seconds ago 514MB
nginx v1 a5412fe37cac 16 minutes ago 361MB
nginx latest 881bd08c0b08 9 days ago 109MB
busybox latest d8233ab899d4 3 weeks ago 1.2MB
centos 7 1e1148e4cc2c 3 months ago 202MB
centos latest 1e1148e4cc2c 3 months ago 202MB
java 8 d23bdf5b1b1b 2 years ago 643MB
[root@192 ~]# ls
anaconda-ks.cfg Dockerfile-php dockerfile_.zip nginx nginx.tar php php-fpm.conf php.ini tomcat
[root@192 ~]# cd tomcat/
[root@192 tomcat]# ls
Dockerfile-tomcat
#构建tomcat前,先到 http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/ 查看版本,修改VERSION=为你需要的版本
[root@192 tomcat]# vi Dockerfile-tomcat
[root@192 tomcat]# docker build -t tomcat:v1 -f Dockerfile-tomcat .
Sending build context to Docker daemon 2.56kB
Step 1/9 : FROM centos:7
---> 1e1148e4cc2c
....
Successfully built 5f8fe4ca82ba
Successfully tagged tomcat:v1
#现在已经构建好了 tomcat, php,nginx 标签都是v1的镜像
[root@192 tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v1 5f8fe4ca82ba 2 minutes ago 427MB
php v1 d48e00d7de94 16 minutes ago 514MB
nginx v1 a5412fe37cac 32 minutes ago 361MB
nginx latest 881bd08c0b08 9 days ago 109MB
busybox latest d8233ab899d4 3 weeks ago 1.2MB
centos 7 1e1148e4cc2c 3 months ago 202MB
centos latest 1e1148e4cc2c 3 months ago 202MB
java 8 d23bdf5b1b1b 2 years ago 643MB
作者: 梅梅~
出处: https://www.cnblogs.com/keeptesting
关于作者:专注软件测试,测试运维相关工作,请多多赐教!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 欢迎沟通交流加微信联系。 微信:yangguangkg20140901 暗号:博客园.