Docker基本操作

docker的基础操作

查看镜像

[root@localhost ~]# docker image ls

拉取镜像到本地

  • 格式:docker pull 镜像名:标签
[root@localhost ~]# docker pull centos:7

删除镜像

  • 格式:docker rmi 镜像名:标签
[root@localhost ~]# docker rmi docker.io/mysql:latest
  • 如果有容器基于镜像在运行,那么这个镜像无法删除

删除全部的镜像文件

[root@localhost ~]# docker rmi -f $(docker image ls -q)

制作镜像

  • 方法1:导出现有的镜像文件,导入到其他主机来使用

    • 镜像内容没有改变
  • 方法2:基于现有容器做镜像

    • 先启动一个容器,然后在容器中安装、配置需要的软件,最后将这个容器导出成镜像文件
    • 这个镜像中就有安装和配置好的软件
    • 基于这个新镜像启动容器的时候,这个软件就安装配置好了
  • 方法3:基于makefile做镜像

    • 重要,但是复杂
基于现有镜像做镜像

案例:导出centos7镜像文件

商业版:
[root@localhost ~]# docker save -o centos7.tar.gz docker.io/centos:7
社区版
[root@localhost ~]# docker save docker.io/centos:7>centos7.tar.gz

案例:导入centos7镜像

[root@localhost ~]# docker load -i centos7.tar.gz 
基于容器做镜像
  • 启动centos7容器
[root@localhost ~]# docker run -it --name base1 docker.io/centos:7 /bin/bash

在centos7容器安装nginx

[root@78522ab6819c /]# yum install epel-release -y
[root@78522ab6819c /]# yum install nginx -y
[root@78522ab6819c /]# cd /usr/share/nginx/html
[root@78522ab6819c html]# rm -rf index.html 
[root@78522ab6819c html]# echo "test page for nginx">index.html
[root@78522ab6819c html]# /usr/sbin/nginx
[root@78522ab6819c html]# yum install iproute
[root@78522ab6819c html]# ss -tnl | grep 80
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128       [::]:80                    [::]:*  

最后将容器保存成镜像

[root@localhost ~]# docker commit 78522ab6819 mynginx
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mynginx             latest              2b4d0889c40b        34 seconds ago      392 MB
docker.io/centos    7                   b5b4d78bc90c        2 weeks ago         203 MB

可以基于这个新镜像启动容器

[root@localhost ~]# docker run --rm -it --name myos mynginx /bin/bash
[root@edb69a55df92 /]# 

端口映射

格式:

  • -p 宿主机端口:容器端口 将宿主机的端口和容器的端口进行绑定
  • -p 容器端口 将宿主的任意端与容器的端口进行绑定

案例:启动nginx容器,要求访问宿主机的8081端口,可以访问到容器的中的网站

[root@localhost ~]# docker run --name nginx --rm -d -p 8081:80 nginx

案例:启动nginx容器,将宿主机的任意端口映射到容器的80

[root@localhost ~]# docker run --name nginx04 --rm -d -p 80 nginx

查看宿主机的端口

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
b75ad162c78a        nginx               "nginx -g 'daemon ..."   3 seconds ago       Up 2 seconds        0.0.0.0:32770->80/tcp   nginx04

volume容器挂载

  • docker容器中数据的永久存储

案例:将宿主的/opt/html挂载到容器的/var/www/html

# 在宿主机上传创目录
[root@localhost ~]# mkdir /opt/html

# 启动容器
[root@localhost ~]# docker run --name os01 -it -v /opt/html:/var/www/html --rm centos:7 /bin/bash

# 在容器的/var/www/html下创建文件
[root@1d676e1b539a /]# cd /var/www/html/
[root@1d676e1b539a html]# touch bbb.txt

# 此时在宿主机的/opt/html中就可以看到这个文件

容器挂载宿主机目录的方法

  • 方法1:-v 宿主机目录:容器目录
  • 方法2:-v 容器目录

案例:将宿主机默认目录挂载到容器的/var/www/html

  • 此时系统会自动生成一个挂载目录
  • 随机目录的位置:/var/lib/docker/volumes/ 下会自动创建一个目录
[root@localhost ~]# docker run --name os01 -it -v /var/www/html --rm centos:7 /bin/bash

查看宿主机目录方法

[root@localhost ~]# docker inspect os01
  • 找mounts–>source

Docker应用1

1) 配置安装docker

[root@localhost ~]# yum install docker -y

2) 设置镜像加速

[root@localhost ~]# vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://mzxx8xy8.mirror.aliyuncs.com"]
}
[root@master ~]# systemctl restart docker

3) 拉取centos镜像

[root@localhost ~]# docker pull centos:7

4) 使用命令查看当前镜像

[root@localhost ~]# docker image ls

5) 交互方式运行基于centos镜像的容器,命名为centos1

[root@localhost ~]# docker run -it --name centos1 centos:7 /bin/bash

6) 在容器内安装网络工具并查询网络

[root@77c6af2618f7 /]# yum install net-tools -y
[root@77c6af2618f7 /]# netstat -an
[root@77c6af2618f7 /]# ifconfig

7) 退出容器并使用命令查看当前所有容器信息

[root@77c6af2618f7 /]# exit
[root@localhost ~]# docker ps -a
[root@localhost ~]# docker inspect centos1

8) 删除容器centos1

[root@localhost ~]# docker rm centos1

9) 删除centos镜像

[root@localhost ~]# docker rmi centos:7

Docker应用2

  1. 准备一台Centos7的服务器,检测网络正常
  1. 检查服务器docker运行是否正常,如没有请安装
  1. 请检查本机是否有mysql的镜像,如没有请下载
[root@master ~]# docker pull mysql
  1. 请创建mysql容器,root密码为123456
[root@master ~]# docker run --name mysql --rm -d -e MYSQL_ROOT_PASSWORD=123456 mysql
  1. 请检查本机是否有Nignx和php-fpm镜像,如没有请下载
[root@master ~]# docker pull nginx
[root@master ~]# docker pull php:7.1-fpm
  1. 在创建/opt/html目录,并将此目录挂载到Nginx容器的网站根目录下,同时也挂载到php容器的/www目录下
# 创建网站根目录
[root@master ~]# mkdir /opt/html

# 启动php容器
[root@master ~]# docker run --name php-fpm -v /opt/html:/www -d php:7.1-fpm

# 查看php容器的IP地址
[root@master html]# docker inspect php-fpm | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

# 准备nginx的配置文件
[root@master ~]# yum install epel-release -y
[root@master ~]# yum install nginx -y
[root@master ~]# mv /etc/nginx/nginx.conf.default /tmp/nginx.conf

# 修改nginx的配置文件
[root@master ~]# vi /tmp/nginx.conf
		# 修改四个位置
	    location / {
            root   /var/www/html;    <<< 修改nginx 网站根目录
            index  index.html index.htm;
        }

        location ~ \.php$ {
            root           /www;    <<< 修改网站根目录
            fastcgi_pass   172.17.0.2:9000;    <<< 这个iP是php容器的ip
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; <<< 此处
            include        fastcgi_params;
        }

# 启动nginx容器
[root@master html]# docker run --name nginx -v /tmp/nginx.conf:/etc/nginx/nginx.conf -v /opt/html:/var/www/html -p 88:80 -d --rm nginx
  1. Nginx和php容器成功运行
[root@master ~]# docker ps
  1. 请修改Nignx的配置文件,配置Nginx和php结合
		# 修改三个位置
        location ~ \.php$ {
            root           /usr/share/html;    <<< 修改网站根目录
            fastcgi_pass   192.168.31.63:9000;    <<< 这个iP是宿主机的ip
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; <<< 此处
            include        fastcgi_params;
        }
  1. 请在/opt/html目录下创建php的测试页面
# 创建测试页面
[root@master html]# cd /opt/html/
[root@master html]# echo "test my new page">index.html
[root@master html]# vim a.php
<?php
phpinfo();
?>
[root@master html]# ls
abc.php  index.html
  1. 测试访问Ngnix容器,可以成功访问到php测试页面

基于ngix+php+mysql部署lnmp

  • centos7
  • 编译安装
  • php和mysql yum安装

如果默认的yum源速度太慢,可以换国内的镜像

[root@web ~]# cd /etc/yum.repos.d/
[root@web yum.repos.d]# rm -rf *.repo  ^C
[root@web yum.repos.d]# wget https://mirrors.aliyun.com/repo/Centos-7.repo
[root@web yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-7.repo

Docker应用3

第一步:编译安装nginx

1)安装依赖
[root@web ~]# yum install make gcc gcc-c++ zlib-devel pcre-devel ncurses-devel -y

2)编译安装
[root@web ~]# tar xf nginx-1.17.10.tar.gz 
[root@web ~]# cd nginx-1.17.10
[root@web nginx-1.17.10]# ./configure --prefix=/usr/local/nginx 
[root@web nginx-1.17.10]# make && make install

3)修改nginx配置文件
[root@web ~]# cd /usr/local/nginx/conf/ 
[root@web conf]# mv nginx.conf{,.bak}
[root@web conf]# cp nginx.conf.default nginx.conf
[root@web conf]# vi nginx.conf
修改一行
        location / {
            root   /var/www/html;
            index  index.html index.htm;
        }
        
4)启动nginx
[root@web conf]# /usr/local/nginx/sbin/nginx 
[root@web conf]# lsof -i :80
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   4774   root    6u  IPv4  30561      0t0  TCP *:http (LISTEN)
nginx   4775 nobody    6u  IPv4  30561      0t0  TCP *:http (LISTEN)

5)关闭nginx
[root@web conf]# pkill nginx
[root@web conf]# lsof -i :80

6)开机自动启动nginx
[root@web conf]# echo "/usr/local/nginx/sbin/nginx">>/etc/rc.local
[root@web conf]# chmod +x /etc/rc.d/rc.local

7)关闭防火墙和selinux
[root@web ~]# systemctl stop firewalld
[root@web ~]# systemctl disable firewalld
[root@web ~]# setenforce 0
[root@web ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

7)测试
   此时测试会报404错

8)创建nginx网站根目录
[root@web ~]# mkdir /var/www/html -pv
[root@web ~]# echo "test zxhk">/var/www/html/index.html

9)测试

  • 注意:访问被拒绝是防火墙问题

面试:什么情况下会报403

  • nginx做了访问限制,需要输入账号密码才能访问,密码错误会报403
  • 指定了默认首文件,但是网站根目录下,首文件不存在,而且有其他的文件存在,会报403
  • selinux

第二步:安装mysql

  • centos6:mysql-server
  • centos7:mariadb-server
1)安装mysql
[root@web html]# yum install mariadb-server -y

2)启动mysql
[root@web html]# systemctl start mariadb
[root@web html]# systemctl enable mariadb
[root@web html]# ss -tnl | grep 3306
LISTEN     0      50           *:3306                     *:*   

3)登录mysql
[root@web html]# mysql 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> 

第三步:安装php

1)安装php
[root@web html]# yum install php-fpm -y

2)修改配置文件
[root@web ~]# cd /etc/php-fpm.d/
[root@web php-fpm.d]# cp www.conf www.conf.bak
[root@web php-fpm.d]# vi www.conf
修改两行
listen = 192.168.31.63:9000     # 指定php-fpm监听的地址
listen.allowed_clients = 192.168.31.63   # 指定运行那个nginx向当前的php-fpm发请求

3)启动php-fpm
[root@web php-fpm.d]# systemctl start php-ftpm
[root@web php-fpm.d]# systemctl enable php-fpm

4)检查启动状态
[root@web php-fpm.d]# ss -tnl | grep 9000

第四步:整合php和nginx

1)修改nginx的配置文件
[root@web ~]# vi /usr/local/nginx/conf/nginx.conf
        location ~ \.php$ {
           root            /www-php;    # 指定php文件的路径
            fastcgi_pass   192.168.31.64:9000;   # 指定php的ip和端口,需要和第三步保持一致
            fastcgi_index  index.php; 
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

2)重启nginx
[root@web ~]# pkill nginx
[root@web ~]# /usr/local/nginx/sbin/nginx

3)准备php测试页面
[root@web ~]# mkdir /www-php
[root@web ~]# cd /www-php
[root@web ~]# vi test.php
<?php
phpinfo();
?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值