【Docker篇之三】Docker鏡像實戰
- 創建Nginx鏡像創建專用目錄
cd /opt
mkdir nginx
cd nginx
vim Dockerfile
FROM centos //基於基礎鏡像為centos MAINTAINER this is nginx image <[email protected]> //描述信息 RUN yum install -y wget proc-devel net-tools gcc zlib zlib-devel make openssl-devel //安裝環境包 RUN wget http://nginx.org/download/nginx-1.9.7.tar.gz //容器中下載Nginx軟件包 RUN tar zxvf nginx-1.9.7.tar.gz //解壓壓縮包 WORKDIR nginx-1.9.7 //切換到文件目錄中 RUN ./configure --prefix=/usr/local/nginx && make && make install //指定安裝目錄,執行安裝 EXPOSE 80 //打開80端口 EXPOSE 443 //打開443端口 RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf //關閉守護進程 ADD run.sh /run.sh //添加啟動腳本 RUN chmod 755 /run.sh //為啟動腳本增加執行權限 CMD ["/run.sh"] //啟動容器時執行啟動服務腳本
- 編寫啟動腳本
vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx //此處Nginx啟動腳本位置為容器中位置
docker build -t nginx:new . //生成新鏡像
docker run -d -P nginx:new //運行容器
docker ps -a //查看容器運行狀態以及端口映射情況
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 535b90a8ec25 nginx:new "/run.sh" 11 seconds ago Up 10 seconds 0.0.0.0:32770->80/tcp, 0.0.0.0:32769->443/tcp practical_ride
- 客戶端訪問測試,根據端口映射情況,直接訪問宿主機的IP地址加端口。
- 註:若是遇到Nginx軟件包wget不到的情況導致Dockerfile程序異常退出,首先查看docker images 是否存在<none>的鏡像,有兩種方法可以刪除none錯誤鏡像。
- 方法一:
docker rmi none鏡像ID --force
docker images //發現none鏡像已經刪除
docker rm 容器ID //刪除多余無用的容
- 方法二:
編寫腳本刪除,然後執行腳本即可:
vim none.sh
docker ps -a | grep "Exited" | awk ‘{print $1 }‘|xargs docker stop docker ps -a | grep "Exited" | awk ‘{print $1 }‘|xargs docker rm docker images|grep none|awk ‘{print $3 }‘|xargs docker rmi
source none.sh
- 註:若由於wget不到Nginx軟件包導致Dockefile無法繼續執行而退出,可提前將Nginx軟件包下載到宿主機,解壓至與Dockerfile在同一級別的目錄中,在Dockerfile中使用ADD復制到容器中執行編譯安裝。那麽Dockerfile編寫方式可更改如下:
tar zxvf nginx-1.12.0.tar.gz -C /opt/nginx/
ls
vim Dockerfile
FROM centos
MAINTAINER this is Nginx image<[email protected]>
RUN yum install -y wget proc-devel net-tools gcc zlib zlib-devel make openssl-devel
ADD nginx-1.12.0 /nginx-1.12.0 //與上面不同的地方,直接將解壓好的軟件包復制到容器中根目錄下
# RUN tar zxvf nginx-1.9.7.tar.gz
WORKDIR /nginx-1.12.0
RUN ./configure --prefix=/usr/local/nginx && make && make install
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod +x /run.sh //增加執行權限
CMD ["/run.sh"]
- 然後執行生產新鏡像,以及容器運行
docker build -t nginx:new .
docker images
docker run -d -P nginx:new
docker ps -a
構建tomcat鏡像
- 由於tomcat安裝需要有JDK環境支持,因此在此之前需要準備jdk軟件環境。
mkdir -p /opt/tomcat
tar zxvf jdk-8u91-linux-x64.tar.gz
tar zxvf apache-tomcat-8.5.16.tar.gz
vim Dockerfile
FROM centos
MAINTAINER this is tomcat image <[email protected]>
ADD jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-8.5.16 /usr/local/tomcat8
EXPOSE 8080
docker build -t tomcat:centos .
docker run --name tomcat01 -p 80:8080 -it tomcat:centos /bin/bash
[root@87ba5cead994 bin]#cd /usr/local/tomcat8/bin
[root@87ba5cead994 bin]#./startup.sh //啟動tomcat服務
- 檢測
http://192.168.144.111/
構建MySQL鏡像(推薦使用centos6)
mkdir -p /opt/mysql
cd mysql
vim Dockerfile
FROM guyton/centos6
MAINTAINER this is msyql images <[email protected]>
RUN yum install mysql mysql-server
RUN /etc/init.d/mysqld start &&mysql -e "grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘abc123‘;" &&mysql -e "grant all privileges on *.* to ‘root‘@‘localhost‘ identified by ‘abc123‘;"
EXPOSE 3306
CMD ["mysqld_safe"]
docker build -t centos6:mysql .
docker run --name=mysqlserver -d -P centos6:mysql
docker ps -a
- 找個裝有MySQL的客戶端驗證訪問
mysql -h 192.168.144.111 -u root -P 32768 -pabc123
【Docker篇之三】Docker鏡像實戰