docker設置固定ip地址
Docker安裝后,默認會創(chuàng)建下面三種網(wǎng)絡類型
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
9781b1f585ae bridge bridge local
1252da701e55 host host local
237ea3d5cfbf none null local
啟動 Docker的時候盒延,用 --network 參數(shù)校翔,可以指定網(wǎng)絡類型
docker run -itd --name test1 --network bridge --ip 172.17.0.10 centos:latest /bin/bash
bridge:橋接網(wǎng)絡
默認情況下啟動的Docker容器,都是使用 bridge曲饱,Docker安裝時創(chuàng)建的橋接網(wǎng)絡悠抹,每次Docker容器重啟時,會按照順序獲取對應的IP地址扩淀,這個就導致重啟下楔敌,Docker的IP地址就變了
none:無指定網(wǎng)絡
使用 --network=none ,docker 容器就不會分配局域網(wǎng)的IP
host: 主機網(wǎng)絡
使用 --network=host驻谆,此時卵凑,Docker 容器的網(wǎng)絡會附屬在主機上庆聘,兩者是互通的。
例如勺卢,在容器中運行一個Web服務伙判,監(jiān)聽8080端口,則主機的8080端口就會自動映射到容器中黑忱。
創(chuàng)建自定義網(wǎng)絡:(設置固定IP)
啟動Docker容器的時候宴抚,使用默認的網(wǎng)絡是不支持指派固定IP的,如下
docker run -itd --net bridge --ip 172.17.0.10 centos:latest /bin/bash
6eb1f228cf308d1c60db30093c126acbfd0cb21d76cb448c678bab0f1a7c0df6
docker: Error response from daemon: User specified IP address is supported on user defined networks only.
步驟1: 創(chuàng)建自定義網(wǎng)絡
docker network create --subnet=172.18.0.0/16 mynetwork
? ~ docker network ls
NETWORK ID NAME DRIVER SCOPE
9781b1f585ae bridge bridge local
1252da701e55 host host local
4f11ae9c85de mynetwork bridge local
237ea3d5cfbf none null local
步驟2: 創(chuàng)建Docker容器
docker run -itd --name networkTest1 --net mynetwork --ip 172.18.0.2 centos:latest /bin/bash
Docker簡介
Docker項目提供了構(gòu)建在Linux內(nèi)核功能之上杨何,協(xié)同在一起的的高級工具酱塔。其目標是幫助開發(fā)和運維人員更容易地跨系統(tǒng)跨主機交付應用程序和他們的依賴。Docker通過Docker容器危虱,一個安全的羊娃,基于輕量級容器的環(huán)境,來實現(xiàn)這個目標埃跷。這些容器由鏡像創(chuàng)建蕊玷,而鏡像可以通過命令行手工創(chuàng)建或 者通過Dockerfile自動創(chuàng)建。
Dockerfile
Dockerfile是由一系列命令和參數(shù)構(gòu)成的腳本弥雹,這些命令應用于基礎鏡像并最終創(chuàng)建一個新的鏡像垃帅。它們簡化了從頭到尾的流程并極大的簡化了部署工作。Dockerfile從FROM命令開始剪勿,緊接著跟隨者各種方法贸诚,命令和參數(shù)。其產(chǎn)出為一個新的可以用于創(chuàng)建容器的鏡像厕吉。
Dockerfile 語法
在我們深入討論Dockerfile之前酱固,讓我們快速過一下Dockerfile的語法和它們的意義。
什么是語法头朱?
非常簡單运悲,在編程中,語法意味著一個調(diào)用命令项钮,輸入?yún)?shù)去讓應用執(zhí)行程序的文法結(jié)構(gòu)班眯。這些語法被規(guī)則或明或暗的約束。程序員遵循語法規(guī)范以和計算機 交互烁巫。如果一段程序語法不正確署隘,計算機將無法識別。Dockerfile使用簡單的亚隙,清楚的和干凈的語法結(jié)構(gòu)磁餐,極為易于使用。這些語法可以自我釋義恃鞋,支持注釋崖媚。
Dockerfile 語法示例
Dockerfile語法由兩部分構(gòu)成,注釋和命令+參數(shù)
1. # Line blocks used for commenting
2. command argument argument ..
一個簡單的例子:
1. # Print "Hello docker!"
2. RUN echo "Hello docker!"
Dockerfile命令
Dockerfile有十幾條命令可用于構(gòu)建鏡像恤浪,下文將簡略介紹這些命令畅哑。
ADD
ADD命令有兩個參數(shù),源和目標水由。它的基本作用是從源系統(tǒng)的文件系統(tǒng)上復制文件到目標容器的文件系統(tǒng)荠呐。如果源是一個URL,那該URL的內(nèi)容將被下載并復制到容器中砂客。
1. # Usage: ADD [source directory or URL] [destination directory]
2. ADD /my_app_folder /my_app_folder
CMD
和RUN命令相似泥张,CMD可以用于執(zhí)行特定的命令。和RUN不同的是鞠值,這些命令不是在鏡像構(gòu)建的過程中執(zhí)行的媚创,而是在用鏡像構(gòu)建容器后被調(diào)用。
1. # Usage 1: CMD application "argument", "argument", ..
2. CMD "echo" "Hello docker!"
ENTRYPOINT
配置容器啟動后執(zhí)行的命令彤恶,并且不可被 docker run 提供的參數(shù)覆蓋钞钙。
每個 Dockerfile 中只能有一個 ENTRYPOINT,當指定多個時声离,只有最后一個起效芒炼。
ENTRYPOINT 幫助你配置一個容器使之可執(zhí)行化,如果你結(jié)合CMD命令和ENTRYPOINT命令术徊,你可以從CMD命令中移除“application”而僅僅保留參數(shù)本刽,參數(shù)將傳遞給ENTRYPOINT命令。
1. # Usage: ENTRYPOINT application "argument", "argument", ..
2. # Remember: arguments are optional. They can be provided by CMD
3. # or during the creation of a container.
4. ENTRYPOINT echo
5. # Usage example with CMD:
6. # Arguments set with CMD can be overridden during *run*
7. CMD "Hello docker!"
8. ENTRYPOINT echo
**ENV **
ENV命令用于設置環(huán)境變量赠涮。這些變量以”key=value”的形式存在子寓,并可以在容器內(nèi)被腳本或者程序調(diào)用。這個機制給在容器中運行應用帶來了極大的便利世囊。
1. # Usage: ENV key value
2. ENV SERVER_WORKS 4
EXPOSE
EXPOSE用來指定端口别瞭,使容器內(nèi)的應用可以通過端口和外界交互。
1. # Usage: EXPOSE [port]
2. EXPOSE 8080
FROM
FROM命令可能是最重要的Dockerfile命令株憾。改命令定義了使用哪個基礎鏡像啟動構(gòu)建流程蝙寨。基礎鏡像可以為任意鏡 像嗤瞎。如果基礎鏡像沒有被發(fā)現(xiàn)墙歪,Docker將試圖從Docker image index來查找該鏡像。FROM命令必須是Dockerfile的首個命令贝奇。
1. # Usage: FROM [image name]
2. FROM ubuntu
MAINTAINER
我建議這個命令放在Dockerfile的起始部分虹菲,雖然理論上它可以放置于Dockerfile的任意位置。這個命令用于聲明作者掉瞳,并應該放在FROM的后面毕源。
1. # Usage: MAINTAINER [name]
2. MAINTAINER authors_name
RUN
RUN命令是Dockerfile執(zhí)行命令的核心部分浪漠。它接受命令作為參數(shù)并用于創(chuàng)建鏡像。不像CMD命令霎褐,RUN命令用于創(chuàng)建鏡像(在之前commit的層之上形成新的層)址愿。
1. # Usage: RUN [command]
2. RUN aptitude install -y riak
USER
USER命令用于設置運行容器的UID。
1. # Usage: USER [UID]
2. USER 751
VOLUME
VOLUME命令用于讓你的容器訪問宿主機上的目錄冻璃。
1. # Usage: VOLUME ["/dir_1", "/dir_2" ..]
2. VOLUME ["/my_files"]
WORKDIR
WORKDIR命令用于設置CMD指明的命令的運行目錄响谓。
1. # Usage: WORKDIR /path
2. WORKDIR ~/
如何使用Dockerfiles
使用Dockerfiles和手工使用Docker Daemon運行命令一樣簡單。腳本運行后輸出為新的鏡像ID省艳。
1. # Build an image using the Dockerfile at current location
2. # Example: sudo docker build -t [name] .
3. sudo docker build -t my_mongodb .
Dockerfile示例一:創(chuàng)建一個MongoDB的鏡像
在這部分中娘纷,我們講一步一步創(chuàng)建一個Dockfile,這個Dockerfile可用于構(gòu)建MongoDB鏡像進而構(gòu)建MongoDB容器跋炕。
創(chuàng)建一個Dockerfile
使用nano文本編輯器赖晶,讓我們創(chuàng)建Dockerfile。
- sudo nano Dockerfile
定義文件和它的目的
讓閱讀者明確Dockerfile的目的永遠是必要的辐烂。為此嬉探,我們通常從注釋開始寫Dockerfile。
1. ############################################################
2. # Dockerfile to build MongoDB container images
3. # Based on Ubuntu
4. ############################################################
設置基礎鏡像
1. # Set the base image to Ubuntu
2. FROM ubuntu
定義作者
1. # File Author / Maintainer
2. MAINTAINER Example McAuthor
設置命令與參數(shù)下載****MongoDB
1. ################## BEGIN INSTALLATION ######################
2. # Install MongoDB Following the Instructions at MongoDB Docs
3. # Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
4. # Add the package verification key
5. RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
6. # Add MongoDB to the repository sources list
7. RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
8. # Update the repository sources list once more
9. RUN apt-get update
10. # Install MongoDB package (.deb)
11. RUN apt-get install -y mongodb-10gen
12. # Create the default data directory
13. RUN mkdir -p /data/db
14. ##################### INSTALLATION END #####################
設置****MongoDB****端口
1. # Expose the default port
2. EXPOSE 27017
3. # Default port to execute the entrypoint (MongoDB)
4. CMD ["--port 27017"]
5. # Set default container command
6. ENTRYPOINT usr/bin/mongod
保存Dockerfile棉圈。
構(gòu)建鏡像
使用上述的Dockerfile涩堤,我們已經(jīng)可以開始構(gòu)建MongoDB鏡像
sudo docker build -t my_mongodb .
Dockerfile示例二:創(chuàng)建一個Nginx的鏡像
Nginx簡述
Nginx是一個高性能的 HTTP 和 反向代理 服務器。它因為它的輕量級分瘾,易用胎围,易于擴展而流行于業(yè)界〉抡伲基于優(yōu)良的架構(gòu)設計白魂,它能夠比之前的類似軟件處理更多的請求。它也可以用來提供靜態(tài)文件服務上岗,比如圖片福荸,腳本和CSS。
和上個例子一樣肴掷,我們還是從基礎鏡像開始敬锐,運用FROM命令和MAINTAINER命令
1. ############################################################
2. # Dockerfile to build Nginx Installed Containers
3. # Based on Ubuntu
4. ############################################################
5. # Set the base image to Ubuntu
6. FROM ubuntu
7. # File Author / Maintainer
8. MAINTAINER Maintaner Name
安裝Nginx
1. # Install Nginx
2. # Add application repository URL to the default sources
3. RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
4. # Update the repository
5. RUN apt-get update
6. # Install necessary tools
7. RUN apt-get install -y nano wget dialog net-tools
8. # Download and Install Nginx
9. RUN apt-get install -y nginx
**Bootstrapping **
安裝Nginx后,我們需要配置Nginx并且替換掉默認的配置文件
1. # Remove the default Nginx configuration file
2. RUN rm -v /etc/nginx/nginx.conf
3. # Copy a configuration file from the current directory
4. ADD nginx.conf /etc/nginx/
5. # Append "daemon off;" to the beginning of the configuration
6. RUN echo "daemon off;" >> /etc/nginx/nginx.conf
7. # Expose ports
8. EXPOSE 80
9. # Set the default command to execute
10. # when creating a new container
11. CMD service nginx start
保存 dockfile呆瞻。
使用Dockerfile自動構(gòu)建Nginx容器
因為我們命令Docker用當前目錄的Nginx的配置文件替換默認的配置文件台夺,我們要保證這個新的配置文件存在。在Dockerfile存在的目錄下痴脾,創(chuàng)建nginx.conf:
- sudo nano nginx.conf
然后用下述內(nèi)容替換原有內(nèi)容:
1. worker_processes 1;
2. events { worker_connections 1024; }
3. http {
4. sendfile on;
5. server {
6. listen 80;
7. location {
8. proxy_pass http://httpstat.us/;
9. proxy_set_header X-Real-IP $remote_addr;
10. }
11. }
12. }
讓我們保存nginx.conf颤介。之后我們就可以用Dockerfile和配置文件來構(gòu)建鏡像。
Dockerfile 實踐
先查看下本地的鏡像,選一個作為base image:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wadeson/centos_nginx v1 210a202d37b8 2 hours ago 464MB
nginx latest c59f17fe53b0 4 days ago 108MB
ubuntu latest 747cb2d60bbe 3 weeks ago 122MB
centos latest 196e0ce0c9fb 6 weeks ago 197MB
在某一個目錄下面創(chuàng)建一個專門存放此demo的目錄滚朵,也就是Dockerfile所在的context:
[root@docker ~]# mkdir docker_demo
[root@docker ~]# cd docker_demo/
[root@docker docker_demo]# touch Dockerfile
[root@docker docker_demo]# pwd
/root/docker_demo
[root@docker docker_demo]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 1 04:34 Dockerfile
接下來就開始編寫Dockerfile文件了(注意Dockerfile的D需要大寫)
這里以編譯nginx提供web服務來構(gòu)建新的鏡像
1冤灾、下載nginx源碼包到docker_demo這個目錄下:
[root@docker docker_demo]# ll
total 960
-rw-r--r--. 1 root root 0 Nov 1 04:34 Dockerfile
-rw-r--r--. 1 root root 981687 Oct 17 09:20 nginx-1.12.2.tar.gz
2、以下是編寫好的Dockerfile v1版:
[root@docker docker_demo]# cat Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@163.com
# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
EXPOSE 80
3辕近、查看docker_demo目錄情況:
[root@docker docker_demo]# ll
total 964
-rw-r--r--. 1 root root 1112 Nov 1 04:58 Dockerfile
-rw-r--r--. 1 root root 981687 Oct 17 09:20 nginx-1.12.2.tar.gz
4瞳购、執(zhí)行docker build進行構(gòu)建:
docker build -t centos_nginx:v1 .
后面的.代表的是相對路徑的當前目錄,如果需要全路徑則為/root/docker_demo(就是找到Dockerfile文件)
構(gòu)建成功后亏推,查看新構(gòu)建的鏡像:
[root@docker docker_demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v1 78d18f16e757 4 hours ago 464MB
wadeson/centos_nginx v1 210a202d37b8 7 hours ago 464MB
nginx latest c59f17fe53b0 4 days ago 108MB
ubuntu latest 747cb2d60bbe 3 weeks ago 122MB
centos latest 196e0ce0c9fb 6 weeks ago 197MB
5、然后使用構(gòu)建的鏡像啟動一個container并開啟nginx服務:
[root@docker docker_demo]# docker run -d centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"
ea5af922378356a5ebff60992f000b186b09d1e8d6a4915b0b8ccf997ca12404
然后查看啟動的container是否在運行:
[root@docker docker_demo]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea5af9223783 centos_nginx:v1 "/usr/local/nginx/..." 7 seconds ago Up 6 seconds 80/tcp flamboyant_carson
雖然nginx服務開啟了年堆,但是port并沒有進行映射到本機host吞杭,所以這個container并不能進行訪問,重新啟動一個進行了映射端口的容器
[root@docker docker_demo]# docker run -d -p80:80 centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"
e4b6e4846dedc6f130e028701c84828a635f3b367c0d500ebd947de16b1be0b2
再次查看端口映射信息:
[root@docker docker_demo]# docker port e4b6e4846ded
80/tcp -> 0.0.0.0:80
于是進行訪問:
于是基于Dockerfile的一個簡單實例構(gòu)建完成变丧,現(xiàn)在基于這個Dockerfile文件依次添加其他的指令進行構(gòu)建
添加ENV環(huán)境變量指令:
[root@docker docker_demo]# cat Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@163.com
# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
然后進行構(gòu)建:
[root@docker docker_demo]# docker build -t centos_nginx:v2 .
Sending build context to Docker daemon 985.6kB
Step 1/10 : FROM centos
---> 196e0ce0c9fb
Step 2/10 : MAINTAINER json_hc@163.com
---> Using cache
---> cde1d7830106
Step 3/10 : ADD nginx-1.12.2.tar.gz /usr/local/src
---> Using cache
---> 1e4d16340af0
Step 4/10 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
---> Using cache
---> 405835ad9b0b
Step 5/10 : RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
---> Using cache
---> 4002738cf7a6
Step 6/10 : RUN useradd -M -s /sbin/nologin nginx
---> Using cache
---> 02961c5c564d
Step 7/10 : WORKDIR /usr/local/src/nginx-1.12.2
---> Using cache
---> f1da71a93c5e
Step 8/10 : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
---> Using cache
---> cd2ad4c45004
Step 9/10 : ENV PATH /usr/local/nginx/sbin:$PATH
---> Running in 07ba2f7129bc
---> 9588fa1058aa
Removing intermediate container 07ba2f7129bc
Step 10/10 : EXPOSE 80
---> Running in 473cd847154a
---> 2031faf8894a
Removing intermediate container 473cd847154a
Successfully built 2031faf8894a
Successfully tagged centos_nginx:v2
由于在構(gòu)建的過程中docker會采用緩存的機制芽狗,上面的構(gòu)建過程中包含很多using cache,所以這次構(gòu)建非逞髋睿快童擎,如果需要重新構(gòu)建,不想使用cache需要添加--no-cache
--no-cache Do not use cache when building the image
查看v2版本的鏡像:
[root@docker docker_demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v2 2031faf8894a 2 minutes ago 464MB
使用v2版本的鏡像啟動一個container:
[root@docker docker_demo]# docker run -d -p81:80 centos_nginx:v2 nginx -g "daemon off;"
da48b465b1b1a14824497d724eee52b8408270b3b5223c5dd7094b7c0cef211d
查看container運行狀態(tài):
[root@docker docker_demo]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da48b465b1b1 centos_nginx:v2 "nginx -g 'daemon ..." 23 seconds ago Up 22 seconds 0.0.0.0:81->80/tcp determined_neumann
進行訪問:
上述啟動容器的過程中使用的命令docker run -d -p81:80 centos_nginx:v2 nginx -g "daemon off;"為什么這里使用的nginx而不是
/usr/local/nginx/sbin/nginx攻晒,因為在Dockerfile文化中執(zhí)行了PATH=/usr/local/nginx/sbin:$PATH顾复,添加到了環(huán)境變量
添加指令CMD:
[root@docker docker_demo]# cat Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@163.com
# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD /bin/sh -c 'nginx -g "daemon off;"'
然后進行構(gòu)建:
[root@docker docker_demo]# docker build -t centos_nginx:v3 .
查看v3版本的鏡像:
[root@docker docker_demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v3 0e49a2c0562f 11 seconds ago 464MB
然后基于v3版本的鏡像啟動一個container:
[root@docker docker_demo]# docker run -d -p82:80 centos_nginx:v3
d988c04d04f49b909f28e7b664be3959a0d51b951f1c1b04fcf5c716552b7c41
查看啟動的容器狀態(tài):
[root@docker docker_demo]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d988c04d04f4 centos_nginx:v3 "/bin/sh -c '/bin/..." 6 seconds ago Up 5 seconds 0.0.0.0:82->80/tcp optimistic_saha
最后進行訪問:
新增加的CMD /bin/sh -c 'nginx -g "daemon off;"'表示
當啟動一個container時默認運行的命令,如果在啟動container時賦予了command的化鲁捏,那么
定義的CMD中的命令將不會被執(zhí)行芯砸,而會去執(zhí)行command的命令
添加entrypoint指令:
[root@docker docker_demo]# cat Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@163.com
# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]
當ENTRYPOINT和CMD連用時,CMD的命令是ENTRYPOINT命令的參數(shù)给梅,兩者連用相當于nginx -g "daemon off;"
而當一起連用的時候命令格式最好一致(這里選擇的都是json格式的是成功的假丧,如果都是sh模式可以試一下)
開始進行構(gòu)建v4版本:
[root@docker docker_demo]# docker build -t centos_nginx:v4 .
查看新建的鏡像:
[root@docker docker_demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v4 6c5128aaff05 4 minutes ago 464MB
為v4版本的鏡像啟動一個container:
[root@docker docker_demo]# docker run -d -p83:80 centos_nginx:v4
6933c78255f3cebe44d4d5d080caf8a2fde45ded2f9b333ec01cdfe98cd5f417
然后查看容器狀態(tài):
[root@docker docker_demo]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6933c78255f3 centos_nginx:v4 "nginx -g 'daemon ..." 6 seconds ago Up 5 seconds 0.0.0.0:83->80/tcp zealous_euclid
最后進行訪問:
這里增加一個案例,如果Dockerfile修改成下面:
[root@docker docker_demo]# cat Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@163.com
# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon on;"]
CMD的命令修改為了后臺动羽,我們知道如果容器內(nèi)的進程在后臺運行那么容器將不會運行包帚,現(xiàn)在以此構(gòu)建v5版本鏡像:
[root@docker docker_demo]# docker build -t centos_nginx:v5 .
查看v5版本的新鏡像:
[root@docker docker_demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v5 5c1131306686 29 seconds ago 464MB
現(xiàn)在利用v5版本鏡像啟動一個container,但是在啟動的時候添加后面的command:
[root@docker docker_demo]# docker run -d -p85:80 centos_nginx:v5 -g "daemon off;"
5e11edbbd2a0e184f1766c435c0d9b01ef5d74b57e2e2c3a1efed0cf2a2e037b
可以看見在后面新增了-g "daemon off;"运吓,前面說過如果增加了命令那么Dockerfile中的CMD中的命令將不會生效
查看容器運行狀態(tài):
[root@docker docker_demo]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e11edbbd2a0 centos_nginx:v5 "nginx -g 'daemon ..." 3 seconds ago Up 3 seconds 0.0.0.0:85->80/tcp nifty_bartik
可以看見容器的運行絲毫沒有問題渴邦,于是nginx的服務依然還是在前臺運行,沒有被CMD影響拘哨,而新增加的command(-g "daemon off;")
將作為ENTRYPOINT的新的參數(shù)以此為準几莽,于是進行訪問端口85
添加VOLUME指令:
[root@docker docker_demo]# cat Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@163.com
# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# mount a dir to container
VOLUME ["/data"]
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
# setup PATH
ENV PATH /usr/local/nginx/sbin:$PATH
# EXPOSE
EXPOSE 80
# the command of entrypoint
ENTRYPOINT ["nginx"]
CMD ["-g"]
開始進行構(gòu)建:
[root@docker docker_demo]# docker build -t centos_nginx:v6 .
查看v6版本的鏡像:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v6 959fdf4d4288 35 seconds ago 464MB
利用該鏡像啟動一個container:
[root@docker ~]# docker run -d -p 86:80 --name=nginx6 centos_nginx:v6 -g "daemon off;"
6c15a9e93fb1421bdb7eddaabe439996e97415e85a003f80c1d8b4b2c5ee3ffa
查看啟動的容器狀態(tài):
[root@docker ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c15a9e93fb1 centos_nginx:v6 "nginx -g 'daemon ..." 4 seconds ago Up 4 seconds 0.0.0.0:86->80/tcp nginx6
利用docker exec進入到container中,查看是否存在卷/data:
[root@6c15a9e93fb1 /]# ll
total 16
-rw-r--r--. 8 root root 15836 Sep 11 15:53 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Sep 11 15:51 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Nov 2 01:42 data
這個/data掛載的目錄對應本機host的這個目錄:
[root@docker _data]# pwd
/var/lib/docker/volumes/3490a9b7f9773b020343e82c1c4236d976b69eb6a80121eb80612d8c39e02820/_data
現(xiàn)在在本機host上的這個目錄創(chuàng)建一個文件:
[root@docker _data]# touch wadeson.sh
[root@docker _data]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 1 21:45 wadeson.sh
然后切換到container中查看是否存在這個文件:
[root@6c15a9e93fb1 /]# ll /data/
total 0
-rw-r--r--. 1 root root 0 Nov 2 01:45 wadeson.sh
添加ONBUILD指令:
Dockerfile1中base image為A鏡像宅静,并在Dockerfile1中定義ONBUILD指令章蚣,構(gòu)建成新鏡像B鏡像
Dockerfile2中base image為B鏡像,構(gòu)建成新鏡像C
當使用鏡像B啟動的container1不會執(zhí)行OBNUILD中定義的內(nèi)容,而使用C鏡像啟動的container2則會執(zhí)行ONBUILD定義的內(nèi)容
[root@docker docker_demo]# cat Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@163.com
# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# mount a dir to container
ONBUILD VOLUME ["/data"]
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
# setup PATH
ENV PATH /usr/local/nginx/sbin:$PATH
# EXPOSE
EXPOSE 80
# the command of entrypoint
ENTRYPOINT ["nginx"]
CMD ["-g"]
使用上面Dockerfile構(gòu)建鏡像版本v7:
[root@docker docker_demo]# docker run -d -p 87:80 --name=nginx7 centos_nginx:v7 -g "daemon off;"
48f1fe5c71aefc0f9513e8085af8f5b7cdf14fa986fb3b11f2050f18ceefd26e
現(xiàn)在進入到容器內(nèi)查看是否存在/data:
[root@docker ~]# docker exec -it nginx7 /bin/bash
[root@48f1fe5c71ae nginx-1.12.2]# ll /data
ls: cannot access /data: No such file or directory
現(xiàn)在修改上面Dockerfile的FROM基于的base image:
[root@docker docker_demo]# cat Dockerfile
# base image
FROM centos_nginx:v7
# MAINTAINER
MAINTAINER json_hc@163.com
利用此Dockerfile構(gòu)建鏡像v8:
[root@docker docker_demo]# docker build -t centos_nginx:v8 .
Sending build context to Docker daemon 986.6kB
Step 1/2 : FROM centos_nginx:v7
# Executing 1 build trigger...
Step 1/1 : VOLUME /data
---> Running in a6187867513d
---> 5ac07930be4c
Removing intermediate container a6187867513d
Step 2/2 : MAINTAINER json_hc@163.com
---> Running in e02dbf8219cf
---> 6f792dc07c35
Removing intermediate container e02dbf8219cf
Successfully built 6f792dc07c35
Successfully tagged centos_nginx:v8
可以看見卷/data已經(jīng)執(zhí)行了操作纤垂,現(xiàn)在啟動一個container:
[root@docker docker_demo]# docker run -d -p 88:80 --name=nginx8 centos_nginx:v8 -g "daemon off;"
6c2a847c5f6b59b02f91afecadbfc15c88d1217a477c0421a424bce6e5eb317a
查看容器狀態(tài)矾策,并進入到容器驗證/data目錄:
[root@docker docker_demo]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c2a847c5f6b centos_nginx:v8 "nginx -g 'daemon ..." 37 seconds ago Up 37 seconds 0.0.0.0:88->80/tcp nginx8
[root@docker docker_demo]# docker exec -it nginx8 /bin/bash
[root@6c2a847c5f6b nginx-1.12.2]# ll /data/
total 0
由此可見鏡像v8包含了v7 的所有內(nèi)容,并且增加了ONBUILD的內(nèi)容