上一節咱們詳解Dockerfile以後,如今來進行實戰。咱們經過docker build來進行鏡像製做。html
build有以下選項:nginx
[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 ~]#
主要參數有:c++
-t, --tag list #給鏡像打tag,好比nginx:v1
-f, --file string # -f Dockerfile文件名字,默認爲當前路徑下的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;"]
a、儘可能讓鏡像文件更小web
清理殘留文件,好比build完成後,要刪掉源碼包、yum緩存等。docker
b、儘可能減小Dockerfile指令shell
由於咱們知道,一個Dockerfile指令,就是一層鏡像,咱們使用shell裏面的&&符號進行拼接成一行,讓RUN指令儘量少。flask
c、在測試中編寫Dockerfilecentos
咱們隨便啓動一個容器:docker run -it centos,進入容器後,就能夠對咱們寫的Dockerfile指令進行逐行執行緩存
d、Dockerfile經常使用指令app
基本上按照表格中的順序進行編寫
docker build -t custom_nginx:v1 -f dockerfile-nginx .
注意:命令最後面一個點,用於指定docker build的上下文。
能夠看到,最後咱們構建成功了。
咱們能夠基於此鏡像做爲基礎鏡像進行容器構建:
[root@localhost src]# docker run -itd -h web001 -p 8889:80 custom_nginx:v1
2ad7070295ed0d4cc97319696176e3bfaf75dde9ccc54e3990e251907ce16ebd
嘗試訪問咱們本身寫的主頁,status.html:
說明咱們的啓動的docker容器是預想中的效果,very good!
咱們製做了基礎鏡像,此時,咱們能夠基於此基礎鏡像進行測試環境發佈了。
編寫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端口
一切都是指望的樣子!