docker的介绍

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:表示以“交互模式”运行容器
t:表示容器启动后会进入其命令行
-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 exec 可以进入到已经启动的容器中docker exec -ti container-id /bin/bash
容器的工作是建立在镜像的基础之上的,如果需要删除镜像的话,需要先删除使用该镜像的容器,然后才能删除镜像
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的镜像

[root@linfan ~]# docker pull 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 ~]# vim 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服务器环境