一個完整的Docker鏡像能夠支撐一個Docker容器的運行,在Docker容器運行過程當中主要提供文件系統數據支撐。Docker鏡像時分層結構的,是由多個層級組成,每一個層級分別存儲各類軟件實現某個功能。javascript
Docker鏡像做爲Docker中最基本的概念,有如下特性:php
1. 鏡像是分層的,每一個鏡像都是由一個或多個鏡像層組成,可經過在某個鏡像加上必定的鏡像層來獲得新鏡像; 2. 每一個鏡像層擁有惟一鏡像ID,Docker引擎默認經過鏡像ID來識別鏡像; 3. 鏡像在存儲和使用時,共享相同的鏡像層,在PULL鏡像時,已有的鏡像層會自動跳過下載; 4. 每一個鏡像層都只讀,即便啓動層容器,也沒法進行真正的修改,修改只會做用於最上層的容器層。
做爲運維人員,咱們能夠製做內部的Docker鏡像,Docker鏡像方法有兩種:css
基於原始文件和目錄從0開始製做鏡像 基於Docker官網倉庫鏡像製做疊加鏡像
# source /usr/share/bash-completion/completions/docker# source /usr/share/bash-completion/completions/docker-compose# yum install -y bash-completion# source /usr/share/bash-completion/bash_completion
# cd /root# tar --numeric-owner --exclude=/proc --exclude=/sys -cvf centos7-base.tar /# lsanaconda-ks.cfg centos7-base.tar
# cat centos7-base.tar | docker import - centos7sha256:d25481af31ead8067dc11f2b949767ddbe82d50ecbd26a753b9a454d9656f64c# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE centos7 latest d25481af31ea 19 seconds ago 1.43GB
# docker run -itd centos7:latest /bin/bashde2579408a3e43838b2dbb8f98847f1b69407a7e344b136efc0b1149f4d71934# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de2579408a3e# docker exec -it de cat /etc/redhat-releaseCentOS Linux release 7.6.1810 (Core) # docker exec -it de ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 16 bytes 1296 (1.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
根據上面的方法,咱們能夠製做各類企業應用軟件和程序鏡像,而且將鏡像方便的遷移至各個平臺。html
徹底從0開始製做基礎鏡像一般比較繁瑣、複雜,在生產環境下,能夠基於Docker官網提供的各類基礎鏡像模板來製做鏡像,這樣能夠節省時間、人力成本等。java
基於Docker官網倉庫鏡像製做新鏡像主要有三種方式:node
Docker commit方式 Docker export方式 Dockerfile方式
基於基礎鏡像,經過Docker run啓動新的容器,exec或者遠程進入新容器,根據企業中的需求,部署相關的軟件、修改相應的配置,而後將整個容器系統commit提交成一個全新的鏡像。linux
# docker exec -it de bash[root@de2579408a3e /]# passwd root[root@de2579408a3e /]# mkdir /data[root@de2579408a3e /]# wget -P /data/ http://nginx.org/download/nginx-1.14.2.tar.gz[root@de2579408a3e /]# exit# docker commit de centos7:v1sha256:556dd5d8972969c0e54d1d5733b2bc9cca4ced1de974377011b79c04ab618433# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE centos7 v1 556dd5d89729 4 seconds ago 1.43GB centos7 latest d25481af31ea 25 minutes ago 1.43GB
新生成的容器中有新建的/data目錄,目錄下有nginx的tar包。nginx
基於基礎鏡像,經過Docker run啓動新的容器,exec或者遠程進入新容器,根據企業中的需求,部署相關的軟件、修改相應的配置,而後將整個容器系統export導出成一個全新的鏡像文件,而後能夠該鏡像文件拷貝到其它主機上,再import導入成鏡像。web
# docker export de -o centos7-v2.tar# lsanaconda-ks.cfg centos7-base.tar centos7-v2.tar# docker import centos7-v2.tar centos7:v2sha256:f4fddd805f795d4b0a6f08df7cc05f5a59eaac09d36e141f7b510a706b7c9856# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE centos7 v2 f4fddd805f79 16 seconds ago 1.43GB centos7 v1 556dd5d89729 7 minutes ago 1.43GB centos7 latest d25481af31ea 32 minutes ago 1.43GB
上面兩種方法,只有docker export製做的鏡像默承認以拷貝到其它的平臺,docker commit製做的鏡像只在images列表中,而不是一個tar文件,如何將images列表中的鏡像導出呢?docker
# docker save centos7:v1 -o centos7-v1.tar# lsanaconda-ks.cfg centos7-base.tar centos7-v1.tar centos7-v2.tar# docker load -i centos7-v1.tarLoaded image: centos7:v1
企業生產環境中,推薦使用Dockerfile製做鏡像,Docker製做原理是基於一個基礎鏡像,經過編寫Dockerfile文件的方式,將各個功能進行疊加,最終造成新的Docker鏡像,是目前互聯網企業中打包鏡像最爲推薦的方式。
Dockerfile 是一個鏡像的表示,也是一個鏡像的原材料,能夠經過Dockerfile來描述構建鏡像,並自動構建一個容器。
下面是Dockerfile製做鏡像的指令和參數:
FROM 指定基於哪一個基礎鏡像 MAINTAINER 指定做者信息 RUN 鏡像操做指令 CMD 指定容器啓動時執行的命令,只能有一條,寫多條也只有最後一條生效 EXPOSE 指定鏡像內服務監聽的端口 ENV 爲後續的RUN指令提供一個環境變量 ADD 將本地的一個文件或目錄拷貝到容器的某個目錄裏 COPY 將本地的一個文件或目錄拷貝到容器的某個目錄裏,推薦使用COPY而不是ADD ENTRYPOINT 指定容器啓動時執行的命令,只能有一條,寫多條也只有最後一條生效 VOLUME 建立一個能夠從本機或者其餘容器掛載的掛載點 USER 指定運行容器的用戶或UID WORKDIR 爲後續的RUN、CMD或者ENTERPOINT指定工做目錄 ARG 指定鏡像內使用的參數(如版本號信息等) ONBUILD 配置當前所建立的鏡像做爲其它鏡像的基礎鏡像時,所執行的建立操做的命令 STOPSIGNAL 容器退出的信號 HEALTHCHECK 如何進行健康檢查 SHELL 指定使用shell時的默認shell類型
ENTRYPOINT和CMD的區別在於ENTRYPOINT可使用CMD做爲參數,一般都是用來啓動後臺服務。
# cat nginx.confuser nobody nobody;worker_processes 2;error_log /usr/local/nginx/logs/nginx_error.log crit;pid /usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 51200;events{ use epoll; worker_connections 6000;}http{ include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml;server{ listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; }}}
# vim Dockerfile
## Set the base image to CentOSFROM centos7:latest# File Auther / MaintainerMaintainer lzx lzx@lzxlinux.com# Install necessary toolsRUN rpm --rebuilddb && \ yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel && \ yum clean all && \ rm -rf /var/cache/yum/*# Install NginxADD http://nginx.org/download/nginx-1.8.0.tar.gz . RUN tar zxf nginx-1.8.0.tar.gz && \ mkdir -p /usr/local/nginx && \ cd nginx-1.8.0 && \ ./configure --prefix=/usr/local/nginx && \ make && make install && \ rm -fv /usr/local/nginx/conf/nginx.conf COPY nginx.conf /usr/local/nginx/conf/nginx.conf# Expose portsEXPOSE 80# Set the default command to execute when creating a new containerENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd #加上tail -f防止容器啓動完nginx就自動中止
# docker build -t centos7:nginx .Successfully built e20207cefc88 Successfully tagged centos7:nginx# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7 nginx e20207cefc88 4 minutes ago 1.68GB centos7 v2 f4fddd805f79 About an hour ago 1.43GB centos7 v1 556dd5d89729 About an hour ago 1.43GB centos7 latest d25481af31ea About an hour ago 1.43GB
# docker run -itd -p 80:80 centos7:nginx bash38b1e0876f834b78f2f8223449c2cca42fa4ab87d047b2dc5b7831a247cab871# curl 127.0.0.1:80<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>
訪問本地80端口,能夠訪問到nginx歡迎頁,說明容器內nginx服務已經啓動並監聽80端口。