一 Docker安裝步驟解析?
1.1 基礎(chǔ)命令?
1.1.1 Docker安裝前置任務(wù)
? ? #使用uname -r 查看系統(tǒng)內(nèi)核,Docker 要求CentOS系統(tǒng)的內(nèi)核版本高于 3.10
? ? #確認(rèn)yum更新到最新(yum update)
1、卸載舊版本Docker?
```
? ? yum remove docker \
? ? ? ? docker-client \
? ? ? ? docker-client-latest \
? ? ? ? docker-common \
? ? ? ? docker-latest \
? ? ? ? docker-latest-logrotate \
? ? ? ? docker-logrotate \
? ? ? ? docker-selinux \
? ? ? ? docker-engine-selinux \
? ? ? ? docker-engine \
? ? ? ? docker.io
? ? rm -rf /var/lib/docker? ? ? #刪除鏡像/容器/卷配置文件
? ? yum remove docker*? ? ? ? ?
```?
2、安裝Docker CE
```
? ? #安裝工具包?
? ? ? ? yum install -y yum-utils device-mapper-persistent-data lvm2?
? ? #設(shè)置yum源?
? ? ? ? yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo?
? ? #安裝最新版本?
? ? ? ? yum install docker-ce docker-ce-cli containerd.io?
? ? ? ? #存在問題:
? ? ? ? ? ? #安裝最新版本container-selinux:
? ? ? ? ? ? ? ? yum install -y \
? ? ? ? ? ? ? ? http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.74-1.el7.noarch.rpm
? ? ? ? ? ? #上述插件安裝失敗,提示:
? ? ? ? ? ? ? ? Requires: selinux-policy-targeted >= 3.13.1-216.el7
? ? ? ? ? ? ? ? Requires: selinux-policy-base >= 3.13.1-216.el7
? ? ? ? ? ? ? ? Requires: selinux-policy >= 3.13.1-216.el7
? ? ? ? ? ? ? ? Requires: policycoreutils >= 2.5-11
? ? ? ? ? ? ? ? #
? ? #啟動(dòng)Docker服務(wù)?
? ? ? ? systemctl start docker?
? ? #驗(yàn)證Docker啟動(dòng)正常?
? ? ? ? docker run hello-world?
? ? #設(shè)置開機(jī)啟動(dòng)?
? ? ? ? chkconfig docker on? 或 systemctl enable docker.service
? ? #設(shè)置阿里云鏡像加速器?
? ? ? ? #使用配置文件/etc/docker/daemon.json(沒有時(shí)新建該文件)
? ? ? ? {
? ? ? ? ? ? "registry-mirrors": ["https://XXXX.mirror.aliyuncs.com"]
? ? ? ? }
? ? #重啟服務(wù)?
? ? ? ? systemctl daemon-reload
? ? ? ? systemctl restart docker?
```
Docker啟動(dòng)目錄更改
? ? 1>停止docker服務(wù):systemctl stop docker
? ? 2> vim /etc/docker/daemon.json?
? ? ? ? 添加"data-root":"/usr/local/dockerData"(docker存儲(chǔ)路徑)
? ? ? ? {
? ? ? ? ? "data-root":"/usr/local/dockerData",
? ? ? ? ? "registry-mirrors": ["https://XXXX.mirror.aliyuncs.com"]
? ? ? ? }
? ? 3> 查看當(dāng)前docker存儲(chǔ)位置
? ? ? ? docker info
Docker基礎(chǔ)用法?
獲取Nginx鏡像?
? ? ? ? docker pull nginx
刪除鏡像? ?
? ? ? ? #刪除鏡像前,確保沒有容器使用該鏡像?
? ? ? ? ? ? docker ps -a
? ? ? ? #存在則刪除該容器
? ? ? ? ? ? docker rm 容器ID
? ? ? ? #成功后刪除鏡像
? ? ? ? ? ? docker rmi 鏡像ID
鏡像查看?
? ? ? ? docker images
容器查看(進(jìn)程查看)
? ? ? ? docker ps -a
查看docker容器所用ip?
? ? ? ? docker inspect 容器名/序列號(hào) | grep -i address
創(chuàng)建映射到宿主機(jī)的配置文件及目錄
? ? ? ? #logs目錄映射為nginx容器的日志目錄,conf目錄中的配置文件映射為nignx容器的配置文件目錄
? ? ? ? mkdir -p /nginx/logs /nginx/conf.d
? ? ? ? #在/nginx目錄下創(chuàng)建編輯主配置文件nginx.conf
? ? ? ? ? ? vim nginx.conf
? ? ? ? 內(nèi)容如下:
? ? ? ? ? ? #設(shè)置運(yùn)行的用戶
? ? ? ? ? ? user root;
? ? ? ? ? ? #設(shè)置nginx要開啟的子進(jìn)程數(shù)量,一般設(shè)置為和CPU數(shù)量相等值
? ? ? ? ? ? work_processes 1;
? ? ? ? ? ? #設(shè)置全局錯(cuò)誤日志位置和級(jí)別
? ? ? ? ? ? error_log /var/log/nignx/error.log error;
? ? ? ? ? ? #設(shè)置進(jìn)程id的存放位置
? ? ? ? ? ? pid /var/run/nigx/pid;
? ? ? ? ? ? events{
? ? ? ? ? ? ? #設(shè)置線程輪詢的方案,如果是linux2.6+,使用epoll,如果是BSD如Mac請(qǐng)使用Kqueue
? ? ? ? ? ? ? use epoll;
? ? ? ? ? ? ? #設(shè)置單個(gè)work_processes的最大的并發(fā)連接數(shù)
? ? ? ? ? ? ? worker_connections 1024;
? ? ? ? ? ? }
? ? ? ? ? ? http{
? ? ? ? ? ? ? #設(shè)置mime類型,類型由mime.type文件定義
? ? ? ? ? ? ? include mime.types;
? ? ? ? ? ? ? default_type? application/octet-stream;
? ? ? ? ? ? ? #設(shè)置一個(gè)名為main的日志格式
? ? ? ? ? ? ? log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
? ? ? ? ? ? ? #設(shè)置訪問日志的位置,使用main的格式
? ? ? ? ? ? ? access_log? /var/log/nginx/access.log? main;
? ? ? ? ? ? ? #設(shè)置連接超時(shí)時(shí)間
? ? ? ? ? ? ? keepalive_timeout? 65;
? ? ? ? ? ? ? #設(shè)置gzip功能為開啟
? ? ? ? ? ? ? gzip on;
? ? ? ? ? ? ? gzip_disable "msie6";
? ? ? ? ? ? ? #設(shè)置請(qǐng)求緩沖
? ? ? ? ? ? ? client_header_buffer_size? ? 128k;
? ? ? ? ? ? ? large_client_header_buffers? 4 128k;
? ? ? ? ? ? ? #設(shè)置nginx是否使用sendfile函數(shù)(普通應(yīng)用設(shè)on,而下載等磁盤重負(fù)載應(yīng)用為設(shè)off)
? ? ? ? ? ? ? sendfile on;
? ? ? ? ? ? ? #設(shè)置當(dāng)前配置文件包含另一個(gè)子配置文件
? ? ? ? ? ? ? include /etc/nginx/conf.d/*.conf;
? ? ? ? ? ? }
? ? ? ? 在/nginx/conf.d目錄下創(chuàng)建nginxchild.conf文件,內(nèi)容如下:
? ? ? ? ? ? #虛擬主機(jī)配置
? ? ? ? ? ? server{
? ? ? ? ? ? ? listen 80;
? ? ? ? ? ? ? server_name localhost;
? ? ? ? ? ? ? root /usr/share/nginx/html;
? ? ? ? ? ? ? index index.html index.htm;
? ? ? ? ? ? }
? ? #啟動(dòng)Nginx容器,并掛載相應(yīng)文件和目錄
? ? ? ? docker run -d --name nginx01 \
? ? ? ? --mount type=bind,src=/nginx/nginx.conf,dst=/etc/nginx/nginx.conf:ro \
? ? ? ? --mount type=bind,src=/nginx/conf.d,dst=/etc/nginx/conf.d \
? ? ? ? --mount type=bind,src=/nginx/logs,dst=/var/log/nginx \
? ? ? ? -p 8083:80 nginx
? ? PS:
? ? ? ? 1>開放端口8083,并無其他進(jìn)程占用
? ? ? ? 2>-p表示將服務(wù)器端口左80映射到容器內(nèi)端口右80,
? ? ? ? ? -d表示在后臺(tái)運(yùn)行,
? ? ? ? ? --name表示啟動(dòng)后的容器名稱,
? ? ? ? ? 第一個(gè)mount表示將本地自定義目錄的自定義主配置文件映射為容器的主配置文件,
? ? ? ? ? 第二個(gè)mount表示將本地自定義的子配置文件目錄映射為容器的子配置文件目錄,
? ? ? ? ? 第三個(gè)mount表示將本地自定義的日志目錄映射為容器的日志目錄。
? ? #重啟容器
? ? ? ? docker restart 容器ID/容器名稱
? ? #進(jìn)入容器
? ? ? ? docker exec -it 容器ID/別名 /bin/bash?
? ? ? ? PS:
? ? ? ? -d, --detach? ? ? ? ? ? ? Detached mode: run command in the background
? ? ? ? ? ? --detach-keys string? Override the key sequence for detaching a container
? ? ? ? -e, --env list? ? ? ? ? ? Set environment variables
? ? ? ? -i, --interactive? ? ? ? ? Keep STDIN open even if not attached
? ? ? ? ? ? --privileged? ? ? ? ? Give extended privileges to the command
? ? ? ? -t, --tty? ? ? ? ? ? ? ? ? Allocate a pseudo-TTY
? ? ? ? -u, --user string? ? ? ? ? Username or UID (format: <name|uid>[:<group|gid>])
? ? ? ? -w, --workdir string? ? ? Working directory inside the container
? ? #創(chuàng)建鏡像
? ? ? ? docker build -t image_name .
? ? ? ? PS:
? ? ? ? ? ? ? --add-host list? ? ? ? ? Add a custom host-to-IP mapping (host:ip)
? ? ? ? ? ? ? --build-arg list? ? ? ? ? Set build-time variables
? ? ? ? ? ? ? --cache-from strings? ? ? Images to consider as cache sources
? ? ? ? ? ? ? --cgroup-parent string? ? Optional parent cgroup for the container
? ? ? ? ? ? ? --compress? ? ? ? ? ? ? ? Compress the build context using gzip
? ? ? ? ? ? ? --cpu-period int? ? ? ? ? Limit the CPU CFS (Completely Fair Scheduler) period
? ? ? ? ? ? ? --cpu-quota int? ? ? ? ? Limit the CPU CFS (Completely Fair Scheduler) quota
? ? ? ? ? -c, --cpu-shares int? ? ? ? ? CPU shares (relative weight)
? ? ? ? ? ? ? --cpuset-cpus string? ? ? CPUs in which to allow execution (0-3, 0,1)
? ? ? ? ? ? ? --cpuset-mems string? ? ? MEMs in which to allow execution (0-3, 0,1)
? ? ? ? ? ? ? --disable-content-trust? Skip image verification (default true)
? ? ? ? ? -f, --file string? ? ? ? ? ? Name of the Dockerfile (Default is 'PATH/Dockerfile')
? ? ? ? ? ? ? --force-rm? ? ? ? ? ? ? ? Always remove intermediate containers
? ? ? ? ? ? ? --iidfile string? ? ? ? ? Write the image ID to the file
? ? ? ? ? ? ? --isolation string? ? ? ? Container isolation technology
? ? ? ? ? ? ? --label list? ? ? ? ? ? ? Set metadata for an image
? ? ? ? ? -m, --memory bytes? ? ? ? ? ? Memory limit
? ? ? ? ? ? ? --memory-swap bytes? ? ? Swap limit equal to memory plus swap: '-1' to enable unlimited swap
? ? ? ? ? ? ? --network string? ? ? ? ? Set the networking mode for the RUN instructions during build (default "default")
? ? ? ? ? ? ? --no-cache? ? ? ? ? ? ? ? Do not use cache when building the image
? ? ? ? ? ? ? --pull? ? ? ? ? ? ? ? ? ? Always attempt to pull a newer version of the image
? ? ? ? ? -q, --quiet? ? ? ? ? ? ? ? ? Suppress the build output and print image ID on success
? ? ? ? ? ? ? --rm? ? ? ? ? ? ? ? ? ? ? Remove intermediate containers after a successful build (default true)
? ? ? ? ? ? ? --security-opt strings? ? Security options
? ? ? ? ? ? ? --shm-size bytes? ? ? ? ? Size of /dev/shm
? ? ? ? ? -t, --tag list? ? ? ? ? ? ? ? Name and optionally a tag in the 'name:tag' format
? ? ? ? ? ? ? -t 設(shè)置tag名稱, 命名規(guī)則registry/image:tag(若不添加版本號(hào),默認(rèn)latest)
? ? ? ? ? ? . 表示使用當(dāng)前目錄下的Dockerfile文件(注意語句后面有一個(gè)點(diǎn))
? ? ? ? ? ? -f 后加自定義Dockerfile文件
使用Dockerfile創(chuàng)建tomcat鏡像
? ? #準(zhǔn)備工作
? ? ? ? #CentOS鏡像
? ? ? ? ? ? docker pull centos
? ? ? ? #下載jdk,tomcat安裝包,上傳至/usr/local/soft目錄,解壓,創(chuàng)建Dockerfile文件
? ? ? ? ? ? tar -zxvf apache-tomcat-版本號(hào).tar.gz? #解壓tomcat
? ? ? ? ? ? tar -zxvf jdk-版本號(hào)-linux-x64.tar.gz? #解壓jdk
? ? ? ? ? ? rm -rf apache-tomcat-版本號(hào).tar.gz? #刪除安裝包
? ? ? ? ? ? rm -rf jdk-版本號(hào)-linux-x64.tar.gz? #刪除安裝包
? ? ? ? ? ? touch Dockerfile? #創(chuàng)建文件
? ? #編輯Dockerfile
? ? ? ? #指定操作的鏡像
? ? ? ? FROM centos
? ? ? ? # 維護(hù)者信息
? ? ? ? MAINTAINER Jackzz
? ? ? ? #執(zhí)行命令:指定工作目錄,無則創(chuàng)建
? ? ? ? WORKDIR /usr/local/soft
? ? ? ? #將jdk添加到鏡像centos的/usr/local/soft/目錄下,并命名為jdk
? ? ? ? ADD jdk1.8.0_171 /usr/local/soft/jdk
? ? ? ? #將tomcat添加到鏡像centos的/usr/local/soft/目錄下,并命名為tomcat
? ? ? ? ADD apache-tomcat-8.5.31 /usr/local/soft/tomcat
? ? ? ? #添加環(huán)境變量
? ? ? ? ENV JAVA_HOME /usr/local/soft/jdk
? ? ? ? ENV CATALINA_HOME /usr/local/soft/tomcat
? ? ? ? ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
? ? ? ? #暴露8080端口
? ? ? ? EXPOSE 8080
? ? ? ? #啟動(dòng)時(shí)運(yùn)行tomcat
? ? ? ? CMD ["/usr/local/soft/tomcat/bin/catalina.sh","run"]
? ? PS:
? ? ? ? FROM : 指定基礎(chǔ)鏡像,并且必須是第一條指令
? ? ? ? MAINTAINER : 指定作者
? ? ? ? RUN : 運(yùn)行指定的命令
? ? ? ? ADD : 復(fù)制命令,把文件復(fù)制到鏡像中。
? ? ? ? ENV : 設(shè)置環(huán)境變量
? ? ? ? EXPOSE : 功能為暴漏容器運(yùn)行時(shí)的監(jiān)聽端口給外部
? ? ? ? CMD : 指定容器啟動(dòng)時(shí)運(yùn)行的命令
[可參考Dockerfile參數(shù)介紹](http://www.dockerinfo.net/dockerfile%E4%BB%8B%E7%BB%8D)
? ? #構(gòu)建Docker鏡像
? ? ? ? docker build -t jackzz/jdk_tomcat .
? ? ? ? -t 設(shè)置tag名稱, 命名規(guī)則registry/image:tag(若不添加版本號(hào),默認(rèn)latest)
? ? ? ? . 表示使用當(dāng)前目錄下的Dockerfile文件(注意語句后面有一個(gè)點(diǎn))
? ? #啟動(dòng)鏡像,訪問容器
? ? ? ? docker run -d -p 8084:8080 --name tomcat jackzz/jdk_tomcat
? ? ? ? -d 后臺(tái)運(yùn)行?
? ? ? ? -p 端口映射? 宿主機(jī)port : 容器port
? ? ? ? --name 指定容器運(yùn)行名稱
[以上內(nèi)容參考地址](https://blog.csdn.net/qq_37936542/article/details/80824389)
? ? #發(fā)布image文件
? ? ? ? #登錄docker
? ? ? ? ? ? docker login
? ? ? ? ? ? #登錄私有倉庫
? ? ? ? ? ? ? ? docker login ip:5000
? ? ? ? #為本地image標(biāo)注用戶名和版本
? ? ? ? ? ? docker image tag [imageName] [username]/[repository]:[tag]
? ? ? ? 或重構(gòu)image文件
? ? ? ? ? ? docker image build -t [username]/[repository]:[tag] .
? ? ? ? #發(fā)布image文件
? ? ? ? ? ? docker image push [username]/[repository]:[tag]
5 其他命令
? ? #列出當(dāng)前停止運(yùn)行的容器id
? ? ? ? docker ls -f "status=exited" -q
? ? ? ? ? ? -q 只顯示容器id
? ? #批量刪除停止運(yùn)行的容器
? ? ? ? docker rm $(docker ls -f "status=exited" -q)
? ? #Dockerfile語法
? ? ? ? #RUN
? ? ? ? ? # 每個(gè)RUN命令構(gòu)建成鏡像中的一層,復(fù)雜的RUN用反斜線換行,&&合并多條命令成一行
? ? ? ? ? RUN yum update && yum install -y vim \
? ? ? ? ? ? ? python-dev
? ? ? ? #WORKDIR
? ? ? ? ? ? # 設(shè)置工作目錄,不存在則創(chuàng)建
? ? ? ? #ADD/COPY添加本地文件到容器,curl/wget添加遠(yuǎn)程文件到容器
? ? ? ? ? ? # 大部分情況,COPY優(yōu)于ADD!ADD除COPY功能外還有解壓功能!
? ? ? ? ? ? # 添加遠(yuǎn)程文件/目錄使用curl或者wget!
? ? ? ? ? ? WORKDIR /root
? ? ? ? ? ? ADD hello test/? #將宿主機(jī)當(dāng)前目錄下hello文件復(fù)制到容器/root/test/hello
? ? ? ? ? ? ADD test.tar.gz / # 添加到根目錄并解壓
? ? ? ? #ENV 設(shè)置變量
? ? ? ? RUN : 執(zhí)行命令并創(chuàng)建新的Image Layer
? ? ? ? CMD :設(shè)置容器啟動(dòng)后默認(rèn)執(zhí)行的命令和參數(shù)
? ? ? ? ENTRYPOINT :設(shè)置容器啟動(dòng)時(shí)運(yùn)行的命令
? ? ? ? ? ? #Shell格式
? ? ? ? ? ? ? ? RUN apt-get update && apt-get install -y vim
? ? ? ? ? ? ? ? ENV name Docker
? ? ? ? ? ? ? ? CMD echo "hello $name"
? ? ? ? ? ? ? ? ENTRYPOINT echo "Hello $name"
? ? ? ? ? ? #Exec格式
? ? ? ? ? ? ? ? RUN ["apt-get", "install", "-y", "vim"]
? ? ? ? ? ? ? ? NV name Docker
? ? ? ? ? ? ? ? CMD ["/bin/echo", "hello docker"]
? ? ? ? ? ? ? ? ENTRYPOINT ["/bin/echo", "hello docker"]
? ? ? ? ? ? ? ? ENTRYPOINT ["/bin/bash", "-c", "echo hello $name"]
? ? ? ? ? ? Shell格式可識(shí)別$,并替換相應(yīng)變量;Exec不能直接識(shí)別$,需更換/bin/bash命令
? ? ? ? #CMD簡介
? ? ? ? ? ? 1、容器啟動(dòng)時(shí)默認(rèn)執(zhí)行的命令;
? ? ? ? ? ? 2、如果docker run指定了其他命令,CMD命令被忽略;
? ? ? ? ? ? 3、如果定義了多個(gè)CMD,只有最后一個(gè)會(huì)執(zhí)行。
? ? ? ? #ENTRYPOINT
? ? ? ? ? ? 1、讓容器以應(yīng)用程序或服務(wù)的形式運(yùn)行;
? ? ? ? ? ? 2、不會(huì)被忽略,一定會(huì)執(zhí)行。
#### 綜合練習(xí)
##### 一、docker+Nginx+tomcat實(shí)現(xiàn)負(fù)載均衡
? ? #步驟1:
? ? ? ? 啟動(dòng)N個(gè)映射端口號(hào)不同的tomcat容器
? ? #步驟2:
? ? ? ? 在相應(yīng)tomcat的/webapps/ROOT目錄下更改index.jsp,區(qū)分不同tomcat調(diào)用。
? ? ? ? 參考index.jsp如下:
? ? ? ? ? ? <%@ page language="java" contentType="text/html; charset=utf-8"? import="java.net.InetAddress"?
? ? ? ? ? ? ? ? pageEncoding="utf-8"%>?
? ? ? ? ? ? <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">?
? ? ? ? ? ? <html>?
? ? ? ? ? ? <head>?
? ? ? ? ? ? <meta http-equiv="Content-Type" content="text/html; charset=utf-8">?
? ? ? ? ? ? <title>Nginx+Tomcat負(fù)載均衡</title>?
? ? ? ? ? ? </head>?
? ? ? ? ? ? <body>?
? ? ? ? ? ? ? ? <%?
? ? ? ? ? ? ? ? ? ? InetAddress addr = InetAddress.getLocalHost();?
? ? ? ? ? ? ? ? ? ? out.println("主機(jī)地址:"+addr.getHostAddress());?
? ? ? ? ? ? ? ? ? ? out.println("主機(jī)名:"+addr.getHostName());?
? ? ? ? ? ? ? ? ? %>?
? ? ? ? ? ? </body>?
? ? ? ? ? ? </html>
? ? 步驟3:
? ? ? ? 更改上述nginx配置文件/conf.d/nginxchild.conf,詳細(xì)內(nèi)容如下:
? ? ? ? ? ? upstream pic {
? ? ? ? ? ? ? server 172.17.0.1:8084 weight=1;
? ? ? ? ? ? ? server 172.17.0.1:8085 weight=3;
? ? ? ? ? ? }
? ? ? ? ? ? #虛擬主機(jī)配置
? ? ? ? ? ? server{
? ? ? ? ? ? ? listen 80;
? ? ? ? ? ? ? server_name localhost;
? ? ? ? ? ? ? root /usr/share/nginx/html;
? ? ? ? ? ? ? index index.html index.htm;
? ? ? ? ? ? ? location / {
? ? ? ? ? ? ? ? ? ? ? proxy_set_header X-Real-IP $remote_addr;
? ? ? ? ? ? ? ? ? ? ? proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
? ? ? ? ? ? ? ? ? ? ? proxy_set_header Host $http_host;
? ? ? ? ? ? ? ? ? ? ? proxy_set_header X-Nginx-Proxy true;
? ? ? ? ? ? ? ? ? ? ? proxy_pass http://pic/;
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? PS:常見問題nginx 104: Connection reset by peer或111: Connection refused,均是該配置文件地址錯(cuò)誤造成。
? ? ? ? 上述IP地址172.17.0.1是docker自定義網(wǎng)段ip地址,使用ifconfig查看
? ? 步驟4:
? ? ? ? 啟動(dòng)nginx
? ? ? ? docker run -d --name nginx \
? ? ? ? --mount type=bind,src=/usr/local/docker/nginx/nginx.conf,dst=/etc/nginx/nginx.conf:ro \
? ? ? ? --mount type=bind,src=/usr/local/docker/nginx/conf.d,dst=/etc/nginx/conf.d \
? ? ? ? --mount type=bind,src=/usr/local/docker/nginx/logs,dst=/var/log/nginx \
? ? ? ? -p 8081:80 nginx
二、docker+nginx+tomcat+mysql
? ? #MySQL鏡像下載:
? ? ? ? docker search mysql
? ? ? ? docker pull mysql:5.7
? ? #創(chuàng)建宿主機(jī)目錄,作持久化
? ? ? ? mkdir -p data
? ? ? ? mkdir -p conf.d
啟動(dòng)MySQL容器
? ? ? ? docker run --name mysql5.7 -p 3306:3306? /
? ? ? ? ? ? -v /usr/local/docker/mysql/data:/var/lib/mysql /
? ? ? ? ? ? -v /usr/local/docker/mysql/conf.d:/etc/mysql/conf.d /
? ? ? ? ? ? -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
? ?進(jìn)入MySQL容器
? ? ? ? docker exec -it mysql5.7 mysql -uroot -proot
? ? PS:
? ? ? ? -p 3306:3306 #將容器的3306端口映射到主機(jī)的3307端口
? ? ? ? -v $PWD/conf:/etc/mysql/conf.d #將主機(jī)當(dāng)前目錄下的 conf/my.cnf 掛載到容器的 /etc/mysql/my.cnf
? ? ? ? -e MYSQL_ROOT_PASSWORD=root #初始化root用戶的密碼
問題?
#當(dāng)關(guān)閉防火墻后,再啟動(dòng)容器,則會(huì)報(bào)以下錯(cuò)誤?
? ? docker: Error response from daemon: driver failed programming external connectivity on endpoint elastic_pare (89c6d7577b0d1082a9576142748589f7a790723169d457620a237ef0c3b8de03):? (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 6379 -j DNAT --to-destination 172.17.0.3:6379 ! -i docker0: iptables: No chain/target/match by that name?
? ? 重啟防火墻即可