Docker入門 - 安裝docker並使用docker搭建PHP環境,初步瞭解Dockerfile

首先咱們需先安裝docker環境,這個比較簡單,以centos7爲例php

  docker在centos7上安裝須要系統內核版本3.10+,能夠經過uname -r查看內核版本號,若是版本不符請自行查閱資料更換。html

  準備好以後使用如下命令安裝docker服務mysql

yum install -y docker

  安裝完成後啓動docker服務,下面是docker啓動、中止、重啓等命令nginx

systemctl restart docker #重啓 systemctl stop docker #中止 systemctl start docker #啓動 systemctl enable docker #開機自啓動 systemctl status docker #服務狀態

  至此docker服務就準備好了,咱們能夠經過docker -v查看docker版本redis

 

而後咱們使用docker搭建php環境sql

  咱們先簡單介紹一下docker的一些經常使用命令,以下所示: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環境流程大體能夠理解爲三步,獲取鏡像->建立容器->運行容器。centos

 

下載php鏡像,php-fpm瀏覽器

  首先咱們須要一個php的鏡像,鏡像獲取有兩種方式,一種是直接獲取docker鏡像庫裏面已有的鏡像,另外一種是本身編輯dockerfile文件自定義一個鏡像,首先咱們採用第一種方式,由於比較簡單,後面再說第二種方式bash

  咱們使用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不在一個容器內,這個時候咱們就須要想辦法讓容器之間能互相訪問,這裏有幾種辦法

  一、在啓動容器時使用 --link實現容器之間的鏈接,而後在nginx容器內修改nginx配置文件裏面的fastcgi_pass對應地址

  二、獲取到php-fpm容器的內網ip,而後在nginx容器內修改nginx配置文件裏面的fastcgi_pass對應地址

  三、建立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等服務容器啦,若是遇到什麼問題能夠在下方給我評論進行詢問

原文出處:https://www.cnblogs.com/Taurus12C/p/taurus12c-docker-php-lnmp.html

相關文章
相關標籤/搜索