docker安裝及docker中nginx配置方法
機器 test3 192.168.23.103
1機器初始化配置
1.1關(guān)閉防火墻,清空防火墻規(guī)則
systemctl stop firewalld iptables -F setenforce 0
1.2部署時間同步
yum install ntp ntpdate -y
1.3安裝基礎(chǔ)軟件包
yum install -y wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack
1.4構(gòu)建docker-ce源
https://developer.aliyun.com/mirror/ yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo yum makecache fast yum -y install docker-ce
1.5啟動docker
systemctl restart docker && systemctl enable docker
2.docker的配置
2.1要是想要docker能被其他服務(wù)器訪問
要是想要docker相互之間通信沒有問題,需要修改內(nèi)核參數(shù),開啟包轉(zhuǎn)發(fā)功能,內(nèi)核參數(shù)修改,br_netfilter 模塊用于將橋接流量轉(zhuǎn)發(fā)至iptables鏈,
[root@test3 ~]# modprobe br_netfilter 模塊可以通過這個命令看有沒有開啟 [root@test3 ~]# lsmod |grep br_netfilter br_netfilter 22256 0 bridge 151336 1 br_netfilter [root@test3 ~]# cat > /etc/sysctl.d/docker.conf << EOF > net.bridge.bridge-nf-call-ip6tables = 1 > net.bridge.bridge-nf-call-iptables = 1 > net.ipv4.ip_forward = 1 > EOF 具體功能如下 Docker 安裝后出現(xiàn):WARNING: bridge-nf-call-iptables is disabled 的解決辦法: net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1: 將 Linux 系統(tǒng)作為路由或者 VPN 服務(wù)就必須要開啟 IP 轉(zhuǎn)發(fā)功能。當 linux 主機有多個網(wǎng)卡時一個網(wǎng)卡收 到的信息是否能夠傳遞給其他的網(wǎng)卡 ,如果設(shè)置成 1 的話 可以進行數(shù)據(jù)包轉(zhuǎn)發(fā),可以實現(xiàn) VxLAN 等功 能。不開啟會導(dǎo)致 docker 部署應(yīng)用無法訪問。 使模塊生效 [root@test3 ~]# sysctl -p /etc/sysctl.d/docker.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 重啟后模塊失效,下面是開機自動加載模塊的腳本 在/etc/新建 rc.sysinit 文件 cat /etc/rc.sysinit #!/bin/bash for file in /etc/sysconfig/modules/*.modules ; do [ -x $file ] && $file done cat /etc/sysconfig/modules/br_netfilter.modules modprobe br_netfilter 增加權(quán)限 chmod 755 /etc/sysconfig/modules/br_netfilter.modules 這樣即使重啟也會加載模塊
配置阿里云鏡像加速器地址
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
docker鏡像相關(guān)的操作
從dockerhub上查找鏡像 [root@test3 ~]# docker search centos NAME DESCRIPTION STARS OFFICIAL centos DEPRECATED; The official build of CentOS. 7721 [OK] kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 43 bitnami/centos-base-buildpack Centos base compilation image 0 dokken/centos-7 CentOS 7 image for kitchen-dokken 10 dokken/centos-8 CentOS 8 image for kitchen-dokken 6 spack/centos7 CentOS 7 with Spack preinstalled 2 dokken/centos-6 EOL: CentOS 6 image for kitchen-dokken 0 atlas/centos7-atlasos ATLAS CentOS 7 Software Development OS 3 ustclug/centos Official CentOS Image with USTC Mirror 0 spack/centos6 CentOS 6 with Spack preinstalled 1 dokken/centos-stream-8 5 eclipse/centos_jdk8 CentOS, JDK8, Maven 3, git, curl, nmap, mc, … 5 dokken/centos-stream-9 10 corpusops/centos-bare https://github.com/corpusops/docker-images/ 0 corpusops/centos centos corpusops baseimage 0 eclipse/centos_go Centos + Go 0 spack/centos-stream 2 fnndsc/centos-python3 Source for a slim Centos-based Python3 image… 0 eclipse/centos_spring_boot Spring boot ready image based on CentOS 0 openmicroscopy/centos-systemd-ip centos/systemd with iproute, for testing mul… 0 eclipse/centos CentOS based minimal stack with only git and… 1 eclipse/centos_nodejs CentOS based nodejs4 stack 0 eclipse/centos_vertx CentOS + vertx 0 eclipse/centos_wildfly_swarm CentOS, WildFly, Swarm 0 dockette/centos My Custom CentOS Dockerfiles 1 解釋說明 NAME: 鏡像倉庫的名稱 DESCRIPTION:鏡像的描述 STARS:類似 github 里面的star,表示點贊,喜歡的意思 OFFICIAL:是否 docker 官方發(fā)布
拉取鏡像
[root@test3 ~]# docker pull centos
查看有哪些鏡像
[root@test3 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 2 years ago 231MB
把鏡像做成離線壓縮包
[root@test3 ~]# ls anaconda-ks.cfg [root@test3 ~]# docker save -o centos.tar.gz centos [root@test3 ~]# ls anaconda-ks.cfg centos.tar.gz
解壓
[root@test3 ~]# docker load -i centos.tar.gz Loaded image: centos:latest
刪除鏡像
docker rmi -f centos:latest
容器相關(guān)的操作 以交互式啟動并運行容器
[root@test3 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 2 years ago 231MB [root@test3 ~]# docker run --name=hello -it centos /bin/bash [root@573ed31d77d8 /]# #docker run 運行并創(chuàng)建容器 --name 容器的名字 -i 交互式 -t 分配終端 centos:啟動docker 需要的鏡像 bin/bash 說明你的shell 類型為bash bash是一種最常用的shell 是大多數(shù) linux 發(fā)行版默認的shell 此外還有 c shell 等其他的shell #在起一個終端看一下 docker ps 是查看正在運行的容器 [root@test3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 573ed31d77d8 centos "/bin/bash" 6 seconds ago Up 5 seconds hello CONTAINER ID:容器的id IMAGE:容器使用的鏡像 COMMAND:容器運行的命令 STATUS:容器啟動的時間 PORTS : 容器的名字 #以守護式進程方式啟動容器 [root@test3 ~]# docker run --name=hello1 -itd centos /bin/bash ae2fe03e778aa2fbd0b9ba2ac412ff3b2f17eaf04772028e1ab4670fd9bdd7fe d 表示后臺運行 #進入hello1的容器 [root@test3 ~]# docker exec -it hello1 /bin/bash [root@ae2fe03e778a /]# #我們退出會發(fā)現(xiàn) 容器他依然運行 [root@test3 ~]# docker exec -it hello1 /bin/bash [root@ae2fe03e778a /]# exit exit [root@test3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ae2fe03e778a centos "/bin/bash" 3 minutes ago Up 3 minutes hello1 [root@test3 ~]# docker ps 會列出正在運行的容器 docker ps -a 會列出正在運行的和已經(jīng)停止的容器全部都列出來 #查看容器日志 [root@test3 ~]# docker logs hello1 [root@test3 ~]# 運行和停止容器 [root@test3 ~]# docker stop hello1 hello1 [root@test3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@test3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ae2fe03e778a centos "/bin/bash" 7 minutes ago Exited (0) 8 seconds ago hello1 573ed31d77d8 centos "/bin/bash" 26 minutes ago Exited (0) 8 minutes ago hello [root@test3 ~]# docker start hello1 hello1 [root@test3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ae2fe03e778a centos "/bin/bash" 8 minutes ago Up 2 seconds hello1 [root@test3 ~]# docker rm -f hello hello [root@test3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ae2fe03e778a centos "/bin/bash" 8 minutes ago Up 48 seconds hello1
docker部署nginx
[root@test3 ~]# docker run --name nginx -p 80 -itd centos 3bd4aa598dd007dc8aba6f43f67d9bd5610e885b724910679edb2694325c8a78 -p 把容器斷端口 隨機在物理機映射一個端口 #進入容器 [root@test3 ~]# docker exec -it nginx /bin/bash [root@3bd4aa598dd0 /]# #此時安裝你會發(fā)現(xiàn)他會報錯 [root@3bd4aa598dd0 /]# yum install wget Failed to set locale, defaulting to C.UTF-8 CentOS Linux 8 - AppStream 78 B/s | 38 B 00:00 Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist [root@3bd4aa598dd0 /]# #解決,刪除/etc/yum.repos.d/所有文件,把阿里云的鏡像重寫進去 [root@3bd4aa598dd0 /]# rm -rf /etc/yum.repos.d/* [root@3bd4aa598dd0 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2495 100 2495 0 0 25202 0 --:--:-- --:--:-- --:--:-- 25202 #此時下載就可以了 [root@3bd4aa598dd0 yum.repos.d]# yum install nginx vim -y Failed to set locale, defaulting to C.UTF-8 CentOS-8.5.2111 - Base - mirrors.aliyun.com 1.2 MB/s | 4.6 MB 00:03 CentOS-8.5.2111 - Extras - mirrors.aliyun.com 64 kB/s | 10 kB 00:00 CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 2.1 MB/s | 8.4 MB 00:04 Dependencies resolved. ================================================================================================================================================================================================================== Package Architecture Version Repository Size ================================================================================================================================================================================================================== Installing: nginx x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 570 k Upgrading: #在容器里更改配置文件,重啟nginx [root@3bd4aa598dd0 yum.repos.d]# echo 'docker is nginx' > /usr/share/nginx/html/index.html [root@3bd4aa598dd0 yum.repos.d]# /usr/sbin/nginx #在宿主機上通過 docker ps 可以查看把容器的 80 端口映射到宿主機的那個端口上,可以看出映射到宿主機的32768端口 [root@test3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3bd4aa598dd0 centos "/bin/bash" 23 hours ago Up 23 hours 0.0.0.0:32768->80/tcp, :::32768->80/tcp nginx #訪問測試,或者直接請求容器的ip也行 [root@test3 ~]# curl 127.0.0.1:32768 docker is nginx [root@test3 ~]# curl 172.17.0.3 docker is nginx #流量走向 訪問物理節(jié)點ip:port(容器在物理節(jié)點映射的端口) ——> 容器 ip:port (容器里部署的服務(wù)器端口) ——> 就可以訪問到容器里部署的應(yīng)用了
直接運行nginx
[root@test3 ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx a2abf6c4d29d: Pull complete a9edb18cadd1: Pull complete 589b7251471a: Pull complete 186b1aaa4aa6: Pull complete b4df32aa5a72: Pull complete a0bcbecc962e: Pull complete Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest #運行nginx [root@test3 ~]# docker run -d -p 28877:80 nginx 0c0313595bf4632610ae2692a7d8b1efd6321b996c61209d0e8e9f9493881a92 -p 28877:80 將宿主機的 28877端口映射到容器的80端口上 指定版本 # 查詢centos鏡像版本,也可以在瀏覽器上訪問,看看有哪些版本 curl -s https://registry.hub.docker.com/v1/repositories/centos/tags | jq # 領(lǐng)導(dǎo)讓你 基于mysql5.7 部署個鏡像 curl -s https://registry.hub.docker.com/v1/repositories/mysql/tags | jq #查看容器ip [root@test3 ~]# docker inspect nginx|grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.3", "IPAddress": "172.17.0.3",
對外訪問nginx1.19.7,直接訪問宿主機的80端口就能看到nginx
[root@test3 ~]# docker pull nginx:1.19.7 1.19.7: Pulling from library/nginx 45b42c59be33: Pull complete 8acc495f1d91: Pull complete ec3bd7de90d7: Pull complete 19e2441aeeab: Pull complete f5a38c5f8d4e: Pull complete 83500d851118: Pull complete Digest: sha256:f3693fe50d5b1df1ecd315d54813a77afd56b0245a404055a946574deb6b34fc Status: Downloaded newer image for nginx:1.19.7 docker.io/library/nginx:1.19.7 [root@test3 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 2 years ago 141MB centos latest 5d0da3dc9764 2 years ago 231MB nginx 1.19.7 35c43ace9216 3 years ago 133MB [root@test3 ~]# netstat -tunlp|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6937/nginx: master [root@test3 ~]# systemctl stop nginx [root@test3 ~]# netstat -tunlp|grep 80 [root@test3 ~]# docker run -d -p 80:80 35c43ace9216 474c65449759c39c108f020bbe54c37f781620b9968f22f0896427dc597a3585 #批量停止正在運行中的容器 [root@test3 ~]# docker ps -q 474c65449759 0c0313595bf4 3bd4aa598dd0 [root@test3 ~]# docker stop $(docker ps -q) 474c65449759 0c0313595bf4 3bd4aa598dd0
docker創(chuàng)建鏡像
[root@test3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 474c65449759 35c43ace9216 "/docker-entrypoint.…" 2 days ago Exited (0) 2 days ago flamboyant_agnesi 0c0313595bf4 nginx "/docker-entrypoint.…" 4 days ago Exited (0) 2 days ago gifted_pasteur 3bd4aa598dd0 centos "/bin/bash" 5 days ago Exited (0) 2 days ago nginx [root@test3 ~]# docker --help|grep comm exec Execute a command in a running container commit Create a new image from a container's changes Run 'docker COMMAND --help' for more information on a command. [root@test3 ~]# docker commit 3bd linux0224.cc sha256:b8dad6dbee789f541fb34736ee4f55708f9685a7dcec3c45ce7340f7d3d70bea [root@test3 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE linux0224.cc latest b8dad6dbee78 26 seconds ago 368MB nginx latest 605c77e624dd 2 years ago 141MB centos latest 5d0da3dc9764 2 years ago 231MB nginx 1.19.7 35c43ace9216 3 years ago 133MB [root@test3 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE linux0224.cc latest b8dad6dbee78 42 seconds ago 368MB nginx latest 605c77e624dd 2 years ago 141MB centos latest 5d0da3dc9764 2 years ago 231MB nginx 1.19.7 35c43ace9216 3 years ago 133MB [root@test3 ~]# docker run -it linux0224.cc bash [root@bc873f3b5d4f /]#
到此這篇關(guān)于docker的安裝以及docker中nginx配置的文章就介紹到這了,更多相關(guān)docker nginx配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解利用nginx和docker實現(xiàn)一個簡易的負載均衡
本篇文章主要介紹了利用nginx和docker實現(xiàn)一個簡易的負載均衡 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Docker 部署HAProxy v2.2.29 并暴露指標接口的問題解決
haproxy提供高可用性、負載均衡以及基于TCP(第四層)和HTTP(第七層)應(yīng)用的代理,支持虛擬主機,它是免費、快速并且可靠的一種解決方案,這篇文章主要介紹了Docker 部署HAProxy v2.2.29 并暴露指標接口,需要的朋友可以參考下2023-04-04Linux下使用Docker部署MinIO存儲服務(wù)實現(xiàn)遠程上傳的方法
MinIO是一個開源的對象存儲服務(wù)器,可以在各種環(huán)境中運行,例如本地、Docker容器、Kubernetes集群等,本文主要是Linux下通過Docker部署MinIO存儲服務(wù)實現(xiàn)遠程上傳的文章,感興趣的朋友一起看看吧2023-11-11docker-compose 配置spring環(huán)境的方法步驟
這篇文章主要介紹了docker-compose 配置spring環(huán)境的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11Docker?compose啟動服務(wù)遇到的問題小結(jié)
這篇文章主要介紹了Docker?compose啟動服務(wù)遇到的問題小結(jié),這個是檢驗我們的docker?compose掌握程度,因此里面的鏡像沒有提前拉取,掛載數(shù)據(jù)卷的目錄也沒有提前創(chuàng)建,本文給大家介紹的非常詳細,需要的朋友可以參考下2023-11-11docker實現(xiàn)將本地文件復(fù)制到docker容器內(nèi)
這篇文章主要介紹了docker實現(xiàn)將本地文件復(fù)制到docker容器內(nèi)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Docker安裝Nginx并部署及MySQL容器構(gòu)建全過程
眾所周知Docker是一種容器化技術(shù),可以用來快速部署和管理應(yīng)用程序,這篇文章主要給大家介紹了關(guān)于Docker安裝Nginx并部署及MySQL容器構(gòu)建的相關(guān)資料,需要的朋友可以參考下2024-02-02Docker安裝ElasticSearch和Kibana的問題及處理方法
這篇文章主要介紹了Docker安裝ElasticSearch和Kibana的問題及遇到問題解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02