首先我們需先安裝docker環境,這個比較簡單,以centos7為例
docker在centos7上安裝需要系統內核版本3.10+,可以通過uname -r查看內核版本號,如果版本不符請自行查閱資料更換。
准備好之后使用以下命令安裝docker服務
yum install -y docker
安裝完成后啟動docker服務,下面是docker啟動、停止、重啟等命令
systemctl restart docker #重啟
systemctl stop docker #停止
systemctl start docker #啟動
systemctl enable docker #開機自啟動
systemctl status docker #服務狀態
至此docker服務就准備好了,我們可以通過docker -v查看docker版本
然后我們使用docker搭建php環境
我們先簡單介紹一下docker的一些常用命令,如下所示:
#從docker鏡像倉庫搜索鏡像,示例docker search mysql docker search [需要查找的鏡像名] #拉取遠程鏡像到本地,示例docker pull mysql:5.7.23 docker pull [鏡像名]:[版本號] #自定義創建一個鏡像,注意需要當前目錄下有一個Dockerfile文件 #注意結尾處有個 “.”是必須的 docker build -t [自定義鏡像名] . #查看本地鏡像,可以看到鏡像名,鏡像id,版本號,創建時間及鏡像大小 docker images #刪除鏡像,需注意刪除的鏡像要沒有container占用,鏡像id不用全部輸入完整 #示例docker image rm 7bc docker image rm [鏡像id] #查看所有container及詳情,可以看到容器名,容器id、容器狀態等信息 docker ps -a #啟動容器,容器id也不需要寫全,示例docker start 88d docker start [容器id] #停止容器 docker stop [容器id] #重啟容器 docker restart [容器id] #創建一個鏡像,后續會對詳細說明 docker run [參數1] [參數2] ... 鏡像名 #進入容器內部交互 docker exec -it [容器名/容器id] /bin/bash
整個docker環境流程大致可以理解為三步,獲取鏡像->創建容器->運行容器。
下載php鏡像,php-fpm
首先我們需要一個php的鏡像,鏡像獲取有兩種方式,一種是直接獲取docker鏡像庫里面已有的鏡像,另一種是自己編輯dockerfile文件自定義一個鏡像,首先我們采用第一種方式,因為比較簡單,后面再說第二種方式
我們使用docker pull 拉去一個已有的php鏡像,這里我推薦 leleos/php-fpm:7.1 鏡像,基本能使用上的擴展都配置好了
docker pull leleos/php-fpm:7.1
構建nginx鏡像
之前我們是直接下載別人的鏡像拿來用,這次我們自己使用dockerfile文件創建一個自定義的鏡像,首先在目錄下創建一個 "Dockerfile" 文件(建議Dockerfile作為文件名),寫入以下內容
#設置容器基礎鏡像,這里我們以centos系統為基礎鏡像 FROM hub.c.163.com/public/centos:latest #設置作者信息 MAINTAINER Taurus12C<1402410174@qq.com> #安裝依賴 RUN rpm --rebuilddb && yum install -y autoconf automake make wget proc-devel net-tools zlib zlib-devel make gcc g++ openssl-devel pcre pcre-devel tar #下載nginx軟件壓縮包 RUN wget http://nginx.org/download/nginx-1.17.1.tar.gz # 解壓到當前目錄 RUN tar -zxvf nginx-1.17.1.tar.gz # 設置當前操作目錄 WORKDIR nginx-1.17.1 # 配置nginx RUN ./configure --prefix=/usr/local/nginx && make && make install RUN rm -rf ../nginx* && yum clean all \ && echo "${TIME_ZOME}" > /etc/timezone \ && ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime # 設置當前操作目錄 WORKDIR /usr/local/nginx #暴露容器端口 EXPOSE 80 #啟動容器后執行語句 CMD ["./sbin/nginx","-g","daemon off;"]
這里我們大致說一下上面文件的命令含義,注意命令必須全部大寫
FROM —— 設置基礎鏡像
MAINTAINER —— 設置這個dockerfile的作者信息
RUN —— 這個比較關鍵,就是你接下來對於容器的配置或者是依賴的一步步操作,因為這里我們是創建一個nginx的鏡像,所以上面的RUN操作都是在容器里面安裝nginx服務
WORKDIR —— 設置當前操作容器內的路徑,類似於cd操作
EXPOSE —— 這個是給容器暴露端口讓宿主機完成映射
CMD —— 這是容器創建啟動后自動執行的命令,這里的命令是啟動nginx服務的意思
其他的命令可以去查閱資料自行了解,這里我們只講解這些,讓大家有個概念
編輯好了之后我們在當前目錄下執行build來構建一個鏡像,如果你的文件是Dockerfile直接執行就好了。注意后面的 "."不要忘了
docker build -t nginx .
執行完畢之后我們使用 docker images 查看本地鏡像已經有了nginx和leleos/php-fpm兩個鏡像了。然后我們就可以使用容器創建一個php的環境運行php項目了
創建dcoker自定義網絡
有的時候我們需要容器之間能夠連接,比如nginx就需要使用fpm,但是nginx和leleos/php-fpm不在一個容器內,這個時候我們就需要想辦法讓容器之間能互相訪問,這里有幾種辦法
1、在啟動容器時使用 --link實現容器之間的連接,然后在nginx容器內修改nginx配置文件里面的fastcgi_pass對應地址
2、獲取到php-fpm容器的內網ip,然后在nginx容器內修改nginx配置文件里面的fastcgi_pass對應地址
3、創建docker自定義網絡,然后在nginx容器內修改nginx配置文件里面的fastcgi_pass對應地址
這里作者推薦使用第三種方法,其他兩種如果需要可以自行了解
創建docker自定義網絡命令,docker network create [網絡組名稱]
docker network create lnmp
執行之后我們可以用docker network ls 查看剛剛創建的網絡組
創建容器,使用docker搭建PHP環境
上面說了那么多都是准備工作,現在我們正式把環境搭建起來
創建nginx容器
docker run -itd --name nginx --network lnmp -p 80:80 -v /var/www/html:/var/www/html -v /usr/local/nginx/conf/vhost:/usr/local/nginx/conf/vhost nginx
解釋一下上面指令的意思
docker run:創建啟動一個容器
-itd:這里是縮寫,對應 -i -t -d ,-i 交互式操作、-t 終端 、-d后台運行
--name:給容器指定名稱
--network:給容器指定網絡組
-p:映射宿主機與容器內的端口
-v:給容器掛載目錄,通俗理解來說就是給容器加了個存儲文件的空間,並且空間內的文件宿主機和容器是共用的。這里我掛載了兩個目錄,一個是項目目錄html,一個是配置文件目錄vhost,這樣就不用進入容器內就能修改了
nginx:指定容器使用的鏡像為nginx鏡像
創建php服務容器
docker run -itd --name php-fpm --network lnmp -p 9000:9000 -v /var/www/html:/var/www/html leleos/php-fpm:7.1
需要注意的是項目目錄需要nginx和php容器配置在體哦那個一個目錄下。至此我們容器也啟動了,可以通過docker ps -a 查看我們剛剛創建並啟動的容器狀態。
到了這里還沒完還有最后一步操作,那就是進行正確的nginx配置。首先我們進到nginx容器里面docker exec -id [容器名/容器id] /bin/bash
docker exec -it nginx /bin/bash
輸入上面這段命令我們就能進入到nginx容器里面,如果需要退出返回到宿主機里面,只需要在容器內輸入exit就可以了
進入/usr/local/nginx/conf目錄編輯nginx.conf文件,文件內容如下
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #域名 index index.html index.htm index.php; root /var/www/html; #這里填寫剛剛掛載的項目目錄 #charset koi8-r; #access_log logs/host.access.log main; #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root html; fastcgi_pass lnmp_php:9000; #這里注意使用php容器的網絡組內端口,我們創建php容器時使用的網絡組和映射的端口 fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } include /usr/loca/nginx/conf/vhost/*.conf; #這里是我們掛載配置文件目錄地址,這樣我們添加新的項目就不需要到容器內來修改nginx.conf文件,只需要在宿主機上修改重啟容器即可 # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
編輯好了之后我們重載nginx配置文件,進入/usr/local/nginx/sbin目錄下執行 ./nginx reload 。然后退出容器exit
進入/var/www/html目錄下創建index.php測試一下換進是否搭建成功,index.php文件內寫入
<?php echo 'Hello World!';
打開瀏覽器訪問服務器地址或域名,如果輸出”Hello World!“就代表環境搭建成功
其他
至此我們已經掌握了docker的一些用法,可以嘗試自己搭建redis、mysql等服務容器啦,如果遇到什么問題可以在下方給我評論進行詢問
