-----
创建一个dockerfile文件
[root@docker dockerfile]# vi Dockerfile #必须文件为Dockerfile
FROM nginx:latest #使用那个镜像作为底包
USER nginx #指定那个用户运行
WORKDIR /usr/share/nginx/html #指定运行的时的目录位置
-----
生成一个镜像
[root@docker dockerfile]# docker build . -t myselfnginx:latest
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM nginx:latest
---> f7bb5701a33c
Step 2/3 : USER nginx
---> Running in 846447177e73
Removing intermediate container 846447177e73
---> 98d5ad96fdfa
Step 3/3 : WORKDIR /usr/share/nginx/html
---> Running in e500aea99e2c
Removing intermediate container e500aea99e2c
---> a6e377307258
Successfully built a6e377307258
Successfully tagged myselfnginx:latest
[root@docker dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myselfnginx latest a6e377307258 26 seconds ago 126MB
zhoumingkang/nginx curl d1b81c37853c 25 hours ago 152MB
myalpine latest_with_hello.txt 3ce9b8b899ba 2 days ago 5.59MB
nginx latest f7bb5701a33c 11 days ago 126MB
zhoumingkang/alpine v3.10.3 cc0abc535e36 2 weeks ago 5.59MB
alpine latest cc0abc535e36 2 weeks ago 5.59MB
alpine 3.9.4 055936d39205 8 months ago 5.53MB
hello-world latest fce289e99eb9 12 months ago 1.84kB
[root@docker dockerfile]# docker run -ti --rm myselfnginx:latest /bin/bash
nginx@51ddb8d7c3e3:/usr/share/nginx/html$ whoami
nginx
nginx@51ddb8d7c3e3:/usr/share/nginx/html$ pwd
/usr/share/nginx/html
nginx@51ddb8d7c3e3:/usr/share/nginx/html$
-----
创建一个dockerfile文件
[root@docker dockerfile]# cat Dockerfile
FROM nginx:latest
ADD index.html /usr/share/nginx/html
EXPOSE 80
-----
生成一个镜像
docker build . -t nginx:with_add_expose
Sending build context to Docker daemon 5.12kB
Step 1/3 : FROM nginx:latest
---> f7bb5701a33c
Step 2/3 : ADD index.html /usr/share/nginx/html
---> 7e2b905eca85
Step 3/3 : EXPOSE 80
---> Running in f13c2538a707
Removing intermediate container f13c2538a707
---> d44bd64abd82
Successfully built d44bd64abd82
Successfully tagged nginx:with_add_expose
[root@docker dockerfile]# docker build . -t centos7:with_install_bind
-----
通过该镜像运行容器
[root@docker dockerfile]# docker run --rm -d -P nginx:with_add_expose
#大写P为宿主机随机生成一个端口指向内部80端口
f19df2f5124538faf3660079e9d6bd5dbb10d54a75f3233847164caa0ef7c44c
[root@docker dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f19df2f51245 nginx:with_add_expose "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:32770->80/tcp nervous_colden
-----
登陆容器查看index.html文件是否存在
[root@docker dockerfile]# docker exec -ti nervous_colden /bin/bash
root@f19df2f51245:/# cd /usr/share/nginx/html/
root@f19df2f51245:/usr/share/nginx/html# ls
50x.html index.html
-----
创建Dockerfile文件
[root@docker dockerfile]# cat Dockerfile
FROM centos:centos7
ENV VER 9.11.4
RUN yum -y install bind-$VER
#RUN 命令是在我们构建镜像时运行的命令,并不是指在容器里运行命令
-----
通过该镜像运行容器,并查环境变量与安装包是否已经成功
[root@docker dockerfile]# docker run -ti centos7:with_install_bind /bin/bash
[root@647dbdb37662 /]# env
HOSTNAME=647dbdb37662
TERM=xterm
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
SHLVL=1
HOME=/root
VER=9.11.4
_=/usr/bin/env
[root@647dbdb37662 /]# rpm -qa bind
bind-9.11.4-9.P2.el7.x86_64
[root@647dbdb37662 /]#
-----
创建Dockerfile文件
[root@docker dockerfile]# cat Dockerfile
FROM centos:centos7
RUN yum -y install httpd
CMD ["httpd","-D","FOREGROUND"]
#关于RUN与CMD的区别
#RUN是创建镜像是运行的命令
#CMD是启动容器后,帮你运行的命令
[root@docker dockerfile]# docker build . -t centos7:httpd
#通过Dockerfile文件创建镜像
-----
通过该容器运行镜像
[root@docker dockerfile]# docker run -d --rm -p80:80 --name myhttpd centos7:httpd
2f2c3328befbbd6fffe14d2887c7d3c334e50218de027881659aa30276a0e8bf
[root@docker dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f2c3328befb centos7:httpd "httpd -D FOREGROUND" 5 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp myhttpd
-----
创建Dockerfile文件
[root@docker dockerfile]# cat Dockerfile
FROM centos:centos7
ADD entrypoint.sh /entrypoint.sh #上传当前目录上的entrypoint.sh文件,要有执行权限
RUN yum -y install epel-release -q && yum -y install nginx #镜像构建时执行命令
ENTRYPOINT /entrypoint.sh
#定义ENTRYPOINT,并开机运行
-----
定义entrypoint.sh文件
[root@docker dockerfile]# cat entrypoint.sh
#!/bin/bash
/sbin/nginx -g "daemon off;"
-----
构建镜像
[root@docker dockerfile]# docker build . -t centos7:mynginx
-----
运行容器查看效果
[root@docker dockerfile]# docker run -d -p80:80 --name mynginx centos7:mynginx
b8c8899bf0eb1012c925746f8d544159bbca4b99013f0aea7a7a625483d11868
[root@docker dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8c8899bf0eb centos7:mynginx "/bin/sh -c /entrypo…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp mynginx
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。