上一節我們詳解Dockerfile之后,現在來進行實戰。我們通過docker build來進行鏡像制作。
build有如下選項:
[root@localhost ~a]# docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
--build-arg list Set build-time variables
--cache-from strings Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--iidfile string Write the image ID to the file
--isolation string Container isolation technology
--label list Set metadata for an image
-m, --memory bytes Memory limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--network string Set the networking mode for the RUN instructions during build (default "default")
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--rm Remove intermediate containers after a successful build (default true)
--security-opt strings Security options
--shm-size bytes Size of /dev/shm
-t, --tag list Name and optionally a tag in the 'name:tag' format
--target string Set the target build stage to build.
--ulimit ulimit Ulimit options (default [])
[root@localhost ~]#
主要參數有:
-t, --tag list #給鏡像打tag,比如nginx:v1 -f, --file string # -f Dockerfile文件名字,默認為當前路徑下的Dockerfile
一、構建Nginx鏡像
1、 Nginx安裝步驟
- 安裝依賴包
- 編譯安裝nginx三步驟:編譯、make、make install
- 配置配置文件和環境變量
2、Dockerfile文件編寫
FROM centos:7
MAINTAINER QUNXUE
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/*
RUN wget http://nginx.org/download/nginx-1.15.5.tar.gz && \
tar zxf nginx-1.15.5.tar.gz && \
cd nginx-1.15.5 &&\
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module && \
make -j 4 && make install && \
rm -rf /usr/local/nginx/html/* && \
echo "ok" >> /usr/local/nginx/html/status.html && \
cd / && rm -rf nginx-1.15.5* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
3、注意事項及dockerfile編寫最佳實踐
a、盡量讓鏡像文件更小
清理殘留文件,比如build完成后,要刪掉源碼包、yum緩存等。
b、盡量減少Dockerfile指令
因為我們知道,一個Dockerfile指令,就是一層鏡像,我們使用shell里面的&&符號進行拼接成一行,讓RUN指令盡可能少。
c、在測試中編寫Dockerfile
我們隨便啟動一個容器:docker run -it centos,進入容器后,就可以對我們寫的Dockerfile指令進行逐行執行
d、Dockerfile常用指令
基本上按照表格中的順序進行編寫
4、構建基礎鏡像(nginx)
docker build -t custom_nginx:v1 -f dockerfile-nginx .
注意:命令最后面一個點,用於指定docker build的上下文,.為當前目錄,就是在build鏡像時,如果有涉及到需要拷貝文件之類的,都是從當前目錄拷貝。
可以看到,最后我們構建成功了。
我們可以基於此鏡像作為基礎鏡像進行容器構建:
[root@localhost src]# docker run -itd -h web001 -p 8889:80 custom_nginx:v1
2ad7070295ed0d4cc97319696176e3bfaf75dde9ccc54e3990e251907ce16ebd
嘗試訪問我們自己寫的主頁,status.html:
說明我們的啟動的docker容器是預想中的效果,very good!
5、基於基礎鏡像測試
我們制作了基礎鏡像,此時,我們可以基於此基礎鏡像進行測試環境發布了。
編寫Dockerfile
FROM custom_nginx:v1
COPY index.html /usr/local/nginx/html
這里我們把index.html文件作為整個測試環境的內容,當然,測試環境肯定不止一個文件,我們這里用於測試。
echo "This is My First Project,Welcome! Guy." >index.html
基於基礎鏡像進行新鏡像構建:
基於新鏡像構建容器:
[root@localhost src]# docker run -itd -h custom_nginxv2 -p 1234:80 custom_nginx:v2
155225793ee06151601047152a1fe25f02691001de0b85d3d017c5d4f657ad7f
[root@localhost src]#
測試:訪問宿主機的1234端口
一切都是期望的樣子!