1. 程式人生 > >使用Dockerfile創建帶編譯安裝znginx服務的Centos Docker鏡像

使用Dockerfile創建帶編譯安裝znginx服務的Centos Docker鏡像

使用dockerfile創建帶編譯安裝z

使用Dockerfile創建帶nginx服務的Centos Docker鏡像

【Dockerfile】使用Dockerfile創建帶nginx服務的Centos Docker鏡像


在宿主機上準備的文件清單:

Dockerfile
#啟動ssh和apache服務的腳本
run.sh

以上文件都放到/root/nginx_centos目錄下

mkdir -p /root/nginx_centos
cd /root/nginx_centos

一、準備run.sh文件
在/root/nginx_centos目錄新建run.sh

vim run.sh

腳本內容如下:

#!/bin/bash
/usr/sbin/sshd &
/usr/local/nginx/sbin/nginx

二、準備Dockerfile
在/root/nginx_centos目錄新建Dockerfile

vim Dockerfile

文件內容如下:

#新生成的鏡像是基於sshd:dockerfile鏡像FROM sshd-centos
MAINTAINER by cmzsteven
WORKDIR /usr/local/src
#安裝wget
RUN yum install -y  wget
#下載並解壓源碼包
RUN wget  
RUN tar -zxvf nginx-1.8.0.tar.gz
WORKDIR nginx-1.8.0
#編譯安裝nginx
RUN yum install -y gcc make pcre-devel zlib-devel
RUN ./configure   --prefix=/usr/local/nginx   --with-pcre
RUN make
RUN make install
#啟動Nginx服務
RUN /usr/local/nginx/sbin/nginx
#修改Nginx配置文件,以非daemon方式啟動
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
#復制服務啟動腳本並設置權限
ADD run.sh /usr/local/sbin/run.sh
RUN chmod 755 /usr/local/sbin/run.sh
#設置生成容器時需要執行的腳本
CMD ["/usr/local/sbin/run.sh"]
#開放22、80、443端口
EXPOSE 22
EXPOSE 80
EXPOSE 443

需要註意的是:在Dockerfile文件中更換當前目錄不可以用“cd”命令,而要改用“WORKDIR”.
三、根據Dockerfile生成鏡像

docker build -t nginx_dockerfile:centos .11

查看鏡像:

[root@localhost nginx_centos]# docker images
REPOSITORY         TAG         IMAGE ID            CREATED          VIRTUAL SIZE
nginx_dockerfile    centos        9ad55461b2fe        5 minutes ago       386.1 MB
nginx           centos         b738cec02b29        47 minutes ago      369.9 MB
sshd-centos       latest         64136bdc0cc8        46 hours ago        261.8 MB
centos           latest        0f73ae75014f        5 weeks ago         172.3 MB

四、根據鏡像生成的容器並進行測試
1、生成新的容器

[root@localhost nginx_centos]#docker run -d -p 2224:22 -p 8001:80 -p 4443:443 nginx_dockerfile:centos /usr/local/sbin/run.sh11

將容器的22端口、80端口和443端口分別映射到到宿主機上的2224端口、8001端口和4443端口,並運行服務腳本。
也可以使用-P參數來讓系統隨機指定端口映射到22、80和443端口:

docker run -d -P nginx_dockerfile:centos11

因為在Dockerfile中指定了EXPOSE所以系統會自動將指定的端口映射出來;同時使用CMD來指定生成容器時所需要執行的角本,所以這裏省略了“/usr/local/sbin/run.sh”。
2、查看新生成的容器:

[root@localhost nginx_centos]# docker ps -a
CONTAINER ID   IMAGE             COMMAND            CREATED        STATUS        PORTS                                            NAMES
c69d42541f52  nginx_dockerfile:centos  "/usr/local/sbin/run  26 seconds ago  Up 25 seconds  0.0.0.0:2224->22/tcp, 0.0.0.0:8001->80/tcp, 0.0.0.0:4443->443/tcp  high_colden
f5a87e085a0b  nginx:centos         "/usr/local/sbin/run   49 minutes ago   Up 49 minutes  0.0.0.0:2223->22/tcp, 0.0.0.0:8000->80/tcp               stoic_kirch
ed9361b598c8  sshd-centos         "/usr/sbin/sshd -D"   About an hour ago   Up About an hour    0.0.0.0:2222->22/tcp                        distracted_mclean

3、測試
測試nignx:

[root@localhost nginx_centos]# curl localhost:8001
<!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>12345678910111213141516171819202122232425261234567891011121314151617181920212223242526

測試成功!

測試ssh

[root@localhost nginx_centos]# ssh localhost -p 2224
The authenticity of host '[localhost]:2224 ([::1]:2224)' can't be established.RSA key fingerprint is d7:fd:3d:40:46:b6:0c:c9:ee:f1:fb:9e:08:c4:12:57.Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:2224' (RSA) to the list of known hosts.root@localhost's password:12345671234567

測試成功!


使用Dockerfile創建帶編譯安裝znginx服務的Centos Docker鏡像