首頁 > 運維 > Nginx > CentOS7 Docker Nginx部署及運行的方法

CentOS7 Docker Nginx部署及運行的方法

WBOY
發布: 2023-05-21 18:19:06
轉載
1350 人瀏覽過

1、資源準備

dockerfile檔案

# "ported" by adam miller <maxamillion@fedoraproject.org> from 
#  https://github.com/fedora-cloud/fedora-dockerfiles 
# 
# originally written for fedora-dockerfiles by 
#  scollier <scollier@redhat.com> 
 
from centos:centos7 
maintainer the centos project <cloud-ops@centos.org> 
 
run yum -y update; yum clean all 
run yum -y install epel-release tar ; yum clean all 
run yum -y install nginx ; yum clean all 
add nginx.conf /opt/deploy/nginx/nginx.conf 
run echo "daemon off;" >> /opt/deploy/nginx/nginx.conf 
#run curl https://git.centos.org/sources/httpd/c7/acf5cccf4afaecf3afeb18c50ae59fd5c6504910 \ 
#  | tar -xz -c /usr/local/nginx/html \ 
#  --strip-components=1 
#run sed -i -e &#39;s/apache/nginx/g&#39; -e &#39;/apache_pb.gif/d&#39; \  
#  /usr/local/nginx/html/index.html 
 
expose 80 
 
#cmd [ "/usr/local/nginx/sbin" ]
登入後複製

注意:路徑需要在系統上面存在以及對應

nginx.conf檔案

#
# for more information on configuration, see: 
#  * official english documentation: http://nginx.org/en/docs/ 
#  * official russian documentation: http://nginx.org/ru/docs/ 
 
user nginx; 
worker_processes 1; 
 
error_log /usr/logs/nginx/error.log; 
#error_log /var/log/nginx/error.log notice; 
#error_log /var/log/nginx/error.log info; 
 
pid    /run/nginx.pid; 
 
 
events { 
  worker_connections 1024; 
} 
 
 
http { 
  include    mime.types; 
  default_type application/octet-stream; 
 
  log_format main &#39;$remote_addr - $remote_user [$time_local] "$request" &#39; 
           &#39;$status $body_bytes_sent "$http_referer" &#39; 
           &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;; 
 
  access_log /usr/logs/nginx/access.log main; 
 
  sendfile    on; 
  #tcp_nopush   on; 
 
  #keepalive_timeout 0; 
  keepalive_timeout 65; 
 
  #gzip on; 
 
  # load modular configuration files from the /etc/nginx/conf.d directory. 
  # see http://nginx.org/en/docs/ngx_core_module.html#include 
  # for more information. 
  #include /etc/nginx/conf.d/*.conf; 
 
  index  index.html index.htm; 
 
  server { 
    listen    80; 
    server_name localhost; 
    root     /usr/share/nginx/html; 
 
    #charset koi8-r; 
 
    #access_log /var/log/nginx/host.access.log main; 
 
    location / { 
      autoindex on; 
    } 
 
    # redirect server error pages to the static page /40x.html 
    # 
    error_page 404       /404.html; 
    location = /40x.html { 
    } 
 
    # redirect server error pages to the static page /50x.html 
    # 
    error_page  500 502 503 504 /50x.html; 
    location = /50x.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  127.0.0.1:9000; 
    #  fastcgi_index index.php; 
    #  fastcgi_param script_filename /scripts$fastcgi_script_name; 
    #  include    fastcgi_params; 
    #} 
 
    # deny access to .htaccess files, if apache&#39;s document root 
    # concurs with nginx&#39;s one 
    # 
    #location ~ /\.ht { 
    #  deny all; 
    #} 
  } 
 
 
  # another virtual host using mix of ip-, name-, and port-based configuration 
  # 
  #server { 
  #  listen    8000; 
  #  listen    somename:8080; 
  #  server_name somename alias another.alias; 
  #  root     html; 
 
  #  location / { 
  #  } 
  #} 
 
 
  # https server 
  # 
  #server { 
  #  listen    443; 
  #  server_name localhost; 
  #  root     html; 
 
  #  ssl         on; 
  #  ssl_certificate   cert.pem; 
  #  ssl_certificate_key cert.key; 
 
  #  ssl_session_timeout 5m; 
 
  #  ssl_protocols sslv2 sslv3 tlsv1; 
  #  ssl_ciphers high:!anull:!md5; 
  #  ssl_prefer_server_ciphers  on; 
 
  #  location / { 
  #  } 
  #} 
 
}
登入後複製

注意:路徑需要在系統上面存在以及對應

2、執行建置鏡像指令

##複製程式碼 程式碼如下:

[root@localhost nginx]# sudo docker build --rm --tag os7/nginx:centos7 . 

執行結果截圖:

CentOS7 Docker Nginx部署及运行的方法

# 3.檢視映像是否安裝建置成功docker images

CentOS7 Docker Nginx部署及运行的方法

4、建立容器docker run -i -t -d -p 192.168.32.129:81:80 os7/nginx / bin/bash


注意:192.168.32.129這個ip的話,則需要在/etc/hosts中加入


192.168.32.129     lohosthost


#5、查看容器是否建立成功並啟動docker ps

CentOS7 Docker Nginx部署及运行的方法

6、測試是否成功存取curl http://192.168.32.129:81

CentOS7 Docker Nginx部署及运行的方法

會出現這個拒絕連接,那該怎麼辦呢?有辦法解決的,我們先進入該容器裡面

7、進入容器docker exec -i -t small_hodgkin /bin/sh

CentOS7 Docker Nginx部署及运行的方法

8、接著在容器裡面執行(直接輸入即可)


nginx


9、在容器外執行curl http://192.168.32.129:81

CentOS7 Docker Nginx部署及运行的方法

成功了。

10、再到虛擬機器外面透過瀏覽器存取

CentOS7 Docker Nginx部署及运行的方法

以上是CentOS7 Docker Nginx部署及運行的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板