docker安裝步驟及基於docker容器部署web應用LNMP服務器環境
docker的定義:
1.docker的三個概念:Docker是一個開源的引擎,可以輕松的為任何應用創建一個輕量級的、可移植的、自給自足的容器
Docker 鏡像 - Docker images: Docker 鏡像是 Docker 容器運行時的只讀模板
Docker 倉庫 - Docker registeries: Docker 倉庫用來保存鏡像,可以理解為代碼控制中的代碼倉庫(Docker Hub)
Docker 容器 - Docker containers:每一個 Docker 容器都是從 Docker 鏡像創建的。Docker 容器可以運行、開始、停止、移動和刪除。每一個 Docker 容器都是獨立和安全的應用平臺
docker的使用
1.檢查docker是否安裝成功,命令:docker version,一般服務端和客戶端在同一臺機器上
2.搜索可用的docker鏡像,docker search XXX
3.下載鏡像:docker pull XXX(鏡像名稱)
4.這時查看本地鏡像列表:docker images ,就可以看到下載好的鏡像了
5.啟動容器docker run -i -t -v /root/software/:/mnt/software/ 83ee /bin/bash
命令包括三個部分:docker run <相關參數> <鏡像 ID> <初始命令>
其中參數有:
-i:表示以“交互模式”運行容器
-d:讓容器在後臺運行
-v:表示需要將本地哪個目錄掛載到容器中,格式:-v <宿主機目錄>:<容器目錄>
本例中,所有安裝程序都放在了宿主機的/root/software/目錄下,現在需要將其掛載到容器的/mnt/software/目錄下
初始命令表示一旦容器啟動,需要運行的命令,此時使用“/bin/bash”,表示啟動後直接進入bash shell
6.容器退出,命令:exit
另一種啟動:
啟動容器&進入容器&拷貝文件
Docker start (container ID)
Docker attach (container ID)
docker cp D:\docker_test\gradle-2.13-bin.zip d6e3e7845eb2:/tmp
容器的工作是建立在鏡像的基礎之上的,如果需要刪除鏡像的話,需要先刪除使用該鏡像的容器,然後才能刪除鏡像
docker rm刪除容器
docker rmi 刪除鏡像
通過容器生成新的鏡像
docker commit <container-id> <image-name>
docker commit -m "wangcreatecontan" -a "wangjccreate" 14628ddc3b57 wjc/test
安裝docker
官方提供了兩種方式一種是使用存儲庫進行安裝 ,一種是從包中進行安裝 ,兩種方式都可以進行安裝 ,只是第一種是官方推薦的。
安裝網絡yum源
[root@linfan ~]# cd /etc/yum.repos.d/
[root@linfan yum.repos.d]# curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@linfan yum.repos.d]# sed -i ‘s/\$releasever/7/g‘ /etc/yum.repos.d/163.repo
[root@linfan yum.repos.d]# sed -i ‘s/enabled=0/enabled=1/g‘ /etc/yum.repos.d/163.repo
[root@linfan yum.repos.d]# yum -y install epel-release
[root@linfan yum.repos.d]# yum clean all
安裝依賴環境
[root@linfan ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@linfan ~]# yum-config-manager --enable extras
[root@linfan ~]# yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
安裝docker
[root@linfan ~]# yum install docker
//中間會出來1個選擇 直接yes就好啦
開啟docker並設置為開機自啟動
[root@linfan ~]# systemctl restart docker
[root@linfan ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@linfan ~]# ps -ef |grep docker
root 3731 1 0 09:37 ? 00:00:00 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json --selinux-enabled --log-driver=journald --signature-verification=false --storage-driver overlay2
root 3736 3731 0 09:37 ? 00:00:00 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc --runtime-args --systemd-cgroup=true
root 3870 2881 0 09:38 pts/3 00:00:00 grep --color=auto docker
基於docker容器部署web應用LNMP服務器環境
關閉防火墻及SELINX
[root@linfan ~]# systemctl stop firewalld
[root@linfan ~]# systemctl disable firewalld
[root@linfan ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/selinux/config
[root@linfan ~]# setenforce 0
setenforce: SELinux is disabled
鏡像下載
下載nginx的鏡像
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
802b00ed6f79: Pull complete
e9d0e0ea682b: Pull complete
d8b7092b9221: Pull complete
Digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
Status: Downloaded newer image for docker.io/nginx:latest
下載mysl的鏡像
[root@linfan ~]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
802b00ed6f79: Already exists
30f19a05b898: Pull complete
3e43303be5e9: Pull complete
94b281824ae2: Pull complete
51eb397095b1: Pull complete
54567da6fdf0: Pull complete
bc57ddb85cce: Pull complete
d6cd3c7302aa: Pull complete
d8263dad8dbb: Pull complete
780f2f86056d: Pull complete
8e0761cb58cd: Pull complete
7588cfc269e5: Pull complete
Digest: sha256:038f5f6ea8c8f63cfce1bce9c057ab3691cad867e18da8ad4ba6c90874d0537a
Status: Downloaded newer image for docker.io/mysql:latest
下載php的鏡像
[root@linfan ~]# docker pull php:7.1.0-fpm
Trying to pull repository docker.io/library/php ...
7.1.0-fpm: Pulling from docker.io/library/php
5040bd298390: Pull complete
568dce68541a: Pull complete
6a832068e64c: Pull complete
c3300aac0007: Pull complete
ae3d537a7fbb: Pull complete
248628681235: Pull complete
a8c69fc28ae2: Pull complete
214901489c7f: Pull complete
ca5d85c64ddd: Pull complete
Digest: sha256:98ebed9994365d145961e48e4d1414633a52d37f50e7cacbb4f370d1bbfb6817
Status: Downloaded newer image for docker.io/php:7.1.0-fpm
編寫生成nginx容器腳本並啟動腳本
[root@linfan ~]# cat docker_nginx.sh
#!/usr/bin bash
docker run -p 80:80 --name nginx -v /docker/code:/usr/share/nginx/html -v /var/log/nginx:/var/log/nginx -v /docker/nginx/conf.d:/etc/nginx/conf.d --privileged=true -d nginx
[root@linfan ~]# sh docker_nginx.sh
WARNING: IPv4 forwarding is disabled. Networking will not work.//此處有報錯,不影響此次操作,為了下面部署順利 下一步還是解決一下
98066b91e351761bb3100c74e61eca1fe39f37d2eadad8625f37dfff0bbb722c
解決報錯
[root@linfan ~]# vim /usr/lib/sysctl.d/00-system.conf
# Kernel sysctl configuration file
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
添加以下內容
net.ipv4.ip_forward=1
[root@linfan ~]# systemctl restart network //重啟網卡
編寫生成mysql容器腳本並啟動腳本
[root@linfan ~]# vim docker_mysql.sh
# /usr/bin bash
docker run --name mysql \ //創建的容器名稱
-e MYSQL_ROOT_PASSWORD=linfan123 \ //設置mysql的密碼
-v /usr/lib/mysql:/usr/lib/mysql -p 3306:3306 \ //容器的3306端口映射到宿主機
-d mysql //後臺運行容器
[root@linfan ~]# sh docker_mysql.sh
46c7ca203469fd5735b21999ab02455a666b5f7596912b175fbadf2af345943d
編寫生成php容器腳本
[root@linfan ~]# vim docker_php.sh
#! /usr/bin bash
docker run -p 9000:9000 --name php -v /docker/code/:/var/www/html/ --privileged=true -d php:7.1.0-fpm
[root@linfan ~]# sh docker_php.sh
b54287d8d154871ebc1192a97ec4220f4b5f96bd8ed382149ae9730870472e7a
創建項目的文件夾並設置nginx配置
創建項目的文件夾
[root@linfan ~]# mkdir /docker/code //提示文件夾已存在,忽悠即可
mkdir: cannot create directory ‘/docker/code’: File exists
[root@linfan ~]# mkdir /docker/nginx/conf.d//提示文件夾已存在,忽悠即可
mkdir: cannot create directory ‘/docker/nginx/conf.d’: File exists
nginx配置
[root@linfan ~]# vim /docker/nginx/conf.d/default.conf
server {
listen 80 default_server;
server_name localhost;
root /usr/share/nginx/html/;
location / {
index index.html index.htm index.php;
autoindex off;
}
location ~ \.php(.*)$ {
root /var/www/html/;
fastcgi_pass 172.17.0.3:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
重啟nginx容器
[root@linfan ~]# docker restart nginx
nginx
[root@linfan ~]# ss -natl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 :::9000 :::*
LISTEN 0 128 :::3306 :::*
創建測試頁面
[root@linfan ~]# echo "<?php phpinfo(); ?>" > /docker/code/index.php
驗證:
在瀏覽器輸入192.168.24.128
docker安裝步驟及基於docker容器部署web應用LNMP服務器環境