1.容器的生命周期:
* 檢查本地是否存在鏡像刁岸,如果不存在即從遠端倉庫檢索
* 利用鏡像啟動容器
* 分配一個文件系統(tǒng)展鸡,并在只讀的鏡像層外掛載一層可讀寫層
* 從宿主機配置的網(wǎng)橋接口中橋接一個虛擬接口到容器
* 從地址池配置一個IP地址給容器
* 執(zhí)行用戶指定的指令
* 執(zhí)行完畢后容器終止
2.Dockerfile的規(guī)則:
?* 格式:
? 1)#為注釋
? 2)指令(大寫)內(nèi)容(小寫)
? 3)盡管指令是大小寫不敏感的,但是宪睹,我們強烈建議指令用大寫慎冤,內(nèi)容用小寫表示
* Docker是按順序執(zhí)行Dockerfile里的指令集合的(從上到下依次執(zhí)行)
* 每一個Dockerfile的第一個非注釋行指令整葡,必須是“FROM”指令,用于為鏡像文件構(gòu)建過程中良姆,指定基準鏡像肠虽,后續(xù)的指令運行于此基準鏡像所提供的運行環(huán)境中。
*實踐中玛追,基準鏡像可以是任何可用鏡像文件税课,默認情況下,docker build 會在docker 主機(本地)上查找指定的鏡像文件痊剖,當其他本地不存在時韩玩,則會從Docker registry(遠端)上拉取所需鏡像文件。
4組 核心的Dockerfile 指令
* USER/WORKDIR指令
*ADD/EXPOSE指令
*RUN/ENV指令
*CMD/ENTRYPOINT指令
===========================================================
(1)USER/WORKDIR指令:
? ? ? /data/dockerfile/Dockerfile
? ? FROM? stanleyws/nginx:v1.12.2?
? ? USER? nginx?
? ? WORKDIR? /usr/share/nginx/html?
? ? ?WORKDIR 類似于 cd:
[root@docker? dockerfile]#? docker build . -t docker.io/oldboy1103/nginx:v1.12.2_with_user_workdir? ?
[root@docker? dockerfile]# docker run --rm -ti --name nginx123 oldboy1103/nginx:v1.12.2_with_user_workdir /bin/bash? ??
===========================================================
(2)ADD/EXPOSE指令
? ? ?/data/dockerfile/Dockerfile?
? ? FROM stanleyws/nginx:v1.12.2?
? ? ADD index.html /usr/share/nginx/html/index.html?
? ? EXPOSE 80
[root@docker? dockerfile]# vi Dockerfile
? ? FROM stanleyws/nginx:v1.12.2
? ? ADD index.html /usr/share/nginx/html/index.html?
? ? EXPOSE 80
[root@docker? dockerfile]# docker build . -t stableyws/nginx:v1.12.2_with_index_expose?
[root@docker? dockerfile]# docker images?
===========================================================
(3)RUN/ENV 指令
? ? ?/data/dockerfile/Dockerfile?
? ? FROM centos?
? ? ENV VER 9.9.4-74.el7_6.1?
? ? RUN yum install bind-$VER -y?
# 構(gòu)建鏡像:
[root@docker? dockerfile]#? docker build . -t stanleyws/bind:v9.9.4_with_env_run?
# 運行容器:
[root@docker? dockerfile]# docker run --rm stanleyws/bind:v9.9.4_with_env_run rpm -qa bind
bind-9.9.4-74.el7_6.1.x86_64
===========================================================
(4)CMD/ENTRYPOINT指令
## CMD和ENTRYPOINT指令作用相同陆馁,使用方法略有不同
1.CMD指令
/data/dockerfile/Dockerfile?
FROM centos?
RUN yum install httpd -y?
CMD ["httpd"找颓,"-D","FOREGROUND"]
構(gòu)建鏡像:
[root@docker? dockerfile]#? docker build . -t stanleyws/httpd:myhttpd
[root@docker? dockerfile]#? vi Dockerfile?
? FROM centos?
? RUN yum install httpd -y?
? CMD ["httpd","-D","FOREGROUND"]
[root@docker? dockerfile]# docker run -d --rm --name myhttpd -p83:88 oldboy1103/httpd:test?
2e12804350b12b0916fe
[root@docker? dockerfile]# docker ps -a?
2.ENTRYPOINT指令:
/data/dockerfile/Dockerfile?
FROM centos
ADD entrypoint.sh /entrypoint.sh?
RUN yum install epel-release -q -y && yum install nginx -y?
ENTRYPOINT /entrypoint.sh?
entrypoint.sh?
/data/dockerfile/entrypoinot.sh?
#!/bin/bash?
/sbin/nginx -g "daemon off;"
[root@docker? dockerfile]#? docker exec -ti compassionate_solomon /bin/bash?
[root@docker? dockerfile]#? vi dockerfile?
? FROM centos
? ADD entrypoint.sh /entrypoint.sh?
? RUN yum install epel-release -q -y && yum install nginx -y?
? ENTRYPOINT /entrypoint.sh?
? ?entrypoint.sh?
===========================================================
(5)綜合實驗:
? ? ? 運行一個docker容器叮贩,在瀏覽器打開demon.od.com能訪問到百度首頁
準備Docker 鏡像:
/data/dockerfile/nginx/Dockerfile?
FROM stanleyws/nginx:v1.12.2?
USER root?
ENV www /usr/share/zoneinfo/Asia/shanghai /etc/localtime &&\
echo 'Asia/Shanghai' >/etc/timezone?
WORKDIR $www?
ADD index.html? $www/index.html
ADD demo.od.com.conf $CONF /demo.od.com.conf?
EXPOSE 80?
CMD ["nginx","g","damon off;"]
index.html?
/data/dockerfile/nginx/index.html
[root@docker? dockerfile]#? wget www.baidu.com -O index.html?
[root@docker? dockerfile]#? vi demo.od.com.conf?
server {
listen 80;
server_name demo.od.com;
root /usr/share/nginx/html;
}
[root@docker? dockerfile]#? docker run --rm -P oldboy1103/nginx:baidu?