美文网首页linux服务器运维dockerDocker
[Docker]容器基础实用教程

[Docker]容器基础实用教程

作者: RamboL | 来源:发表于2022-07-18 09:42 被阅读0次

    docker安装基础见前篇
    基于Centos7安装Docker

    #查看docker版本
    docker version
    
    #-----------------------------------------控制台输出----------------------------------
    Client:
     Cloud integration: 1.0.17
     Version:           20.10.8
     API version:       1.41
     Go version:        go1.16.6
     Git commit:        3967b7d
     Built:             Fri Jul 30 19:55:20 2021
     OS/Arch:           darwin/amd64
     Context:           default
     Experimental:      true
    
    Server: Docker Engine - Community
     Engine:
      Version:          20.10.8
      API version:      1.41 (minimum version 1.12)
      Go version:       go1.16.6
      Git commit:       75249d8
      Built:            Fri Jul 30 19:52:10 2021
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.4.9
      GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
     runc:
      Version:          1.0.1
      GitCommit:        v1.0.1-0-g4144b63
     docker-init:
      Version:          0.19.0
      GitCommit:        de40ad0
    #------------------------------------------------------------------------------------
    

    镜像基础操作

    #查找镜像
    docker search nginx
    #----------------------------------------控制台输出-------------------------------------------------------------
    
    NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    nginx                             Official build of Nginx.                        15480     [OK]       
    jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2066                 [OK]
    richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   818                  [OK]
    jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   242                  
    linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   152                  
    tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   141                  [OK]
    jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        135                  [OK]
    alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   107                  [OK]
    jasonrivers/nginx-rtmp            Docker images to host RTMP streams using NGI…   93                   [OK]
    nginxdemos/hello                  NGINX webserver that serves a simple page co…   72                   [OK]
    privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   57                   [OK]
    nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   55                   
    nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  51                   
    staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   24                   [OK]
    nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   20                   
    nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   19                   
    schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
    centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   15                   
    centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                   
    raulr/nginx-wordpress             Nginx front-end for the official wordpress:f…   13                   [OK]
    mailu/nginx                       Mailu nginx frontend                            9                    [OK]
    sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
    navidonskis/nginx-php5.6          Docker nginx + php5.6 on Ubuntu                 7                    [OK]
    ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          2                    [OK]
    wodby/nginx                       Generic nginx                                   1                    [OK]
    #-------------------------------------------------------------------------------------------------------------
    #选择一个nginx镜像 使用命令下载镜像
    #拉取最新版本的镜像
    docker pull nginx
    

    查看镜像

    docker images
    #-----------------------------控制台输出-------------------------------------
    REPOSITORY              TAG       IMAGE ID       CREATED      SIZE
    phpdockerio/php73-fpm   latest    918fc516e945   2 days ago   162MB
    nginx                   latest    ad4c705f24d3   7 days ago   133MB
    

    通过镜像启动一个容器

    docker run -i -t -d -p 80:80  nginx /bin/bash
    #-----------------------------命令释义-------------------------------------
    run:运行
    -i:交互式操作
    -t:终端
    -p:端口 [宿主机端口]:[docker内部端口]
    -d:后台运行
    

    查看已经运行的容器

    docker ps -a
    #-----------------------------控制台输出---------------------------------------------------------------------------------
    CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS        PORTS                               NAMES
    78317fad8931   nginx     "/docker-entrypoint.…"   47 hours ago   Up 47 hours   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
    

    进入已经运行的容器

    docker exec -i -t 78317fad8931 /bin/bash
    #-----------------------------命令释义-------------------------------------
    方式一(推荐)-exec:退出容器终端,不会导致容器的停止。
    方式二(不推荐)-attach:退出容器终端,会导致容器的停止。
    

    停止已启动的容器

    docker stop 78317fad8931
    

    启动已经停止的容器

    #重启
    docker restart 78317fad8931
    
    #启动
    docker start 78317fad8931
    

    删除已经停止的容器

    # 注意删除容器前需要先stop容器,否则会出现错误
    docker rm 78317fad8931
    #-----------------------------未停止容器错误提示-------------------------------------
    Error response from daemon: You cannot remove a running container 78317fad893178246db3c52743c2770704319f6f991160bd3b4f0ddc97ec3949. 
    Stop the container before attempting removal or force remove
    

    删除镜像

    # 注意删除镜像前需要先删除容器,解除容器与镜像关联,否则会出现错误
    docker rmi nginx
    #-----------------------------未删除容器错误提示-------------------------------------
    Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container 78317fad8931 is using its referenced image ad4c705f24d3
    

    搭建LNMP环境

    安装nginx

    #查找镜像
    docker search nginx
    #----------------------------------------控制台输出-------------------------------------------------------------
    
    NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    nginx                             Official build of Nginx.                        15480     [OK]       
    jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2066                 [OK]
    richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   818                  [OK]
    jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   242                  
    linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   152                  
    tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   141                  [OK]
    jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        135                  [OK]
    alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   107                  [OK]
    jasonrivers/nginx-rtmp            Docker images to host RTMP streams using NGI…   93                   [OK]
    nginxdemos/hello                  NGINX webserver that serves a simple page co…   72                   [OK]
    privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   57                   [OK]
    nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   55                   
    nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  51                   
    staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   24                   [OK]
    nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   20                   
    nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   19                   
    schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
    centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   15                   
    centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                   
    raulr/nginx-wordpress             Nginx front-end for the official wordpress:f…   13                   [OK]
    mailu/nginx                       Mailu nginx frontend                            9                    [OK]
    sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
    navidonskis/nginx-php5.6          Docker nginx + php5.6 on Ubuntu                 7                    [OK]
    ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          2                    [OK]
    wodby/nginx                       Generic nginx                                   1                    [OK]
    #-------------------------------------------------------------------------------------------------------------
    
    #选择一个nginx镜像 使用命令下载镜像
    #拉取最新版本的镜像
    docker pull nginx
    #------------------------------------控制台输出----------------------------------
    Using default tag: latest
    latest: Pulling from library/nginx
    Digest: sha256:853b221d3341add7aaadf5f81dd088ea943ab9c918766e295321294b035f3f3e
    Status: Image is up to date for nginx:latest
    docker.io/library/nginx:latest
    

    运行容器

    docker run --name nginx -p 80:80 -d nginx
    #------------------------------------释义----------------------------------
      --name 这里是别名,使用docker ps 会最后显示names
      -p  80:80    第一个80是服务器的端口,第二个80是docker容器端口,
      -d 要运行的容器名称 这里是nginx
    

    查看运行结果

    CONTAINER ID   IMAGE     COMMAND                  CREATED      STATUS          PORTS                               NAMES
    78317fad8931   nginx     "/docker-entrypoint.…"   2 days ago   Up 17 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
    

    访问nginx服务


    image.png

    安装PHP

    docker search php
    #--------------------------------------------------控制台输出---------------------------------------
    NAME                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    php                            While designed for web development, the PHP …   6158      [OK]       
    phpmyadmin/phpmyadmin          A web interface for MySQL and MariaDB.          1137                 [OK]
    phpmyadmin                     phpMyAdmin - A web interface for MySQL and M…   323       [OK]       
    webdevops/php-nginx            Nginx with PHP-FPM                              209                  [OK]
    php-zendserver                 Zend Server - the integrated PHP application…   199       [OK]       
    webdevops/php-apache-dev       PHP with Apache for Development (eg. with xd…   145                  [OK]
    webdevops/php-apache           Apache with PHP-FPM (based on webdevops/php)    119                  [OK]
    osixia/phpldapadmin            phpLDAPadmin with easy configuration via env…   116                  
    phpunit/phpunit                PHPUnit is a programmer-oriented testing fra…   82                   [OK]
    nazarpc/phpmyadmin             phpMyAdmin as Docker container, based on off…   61                   [OK]
    phpipam/phpipam-www            phpIPAM is an open-source web IP address man…   53                   
    thecodingmachine/php           General-purpose ultra-configurable PHP images   38                   [OK]
    jakzal/phpqa                   Docker image with quality analysis tools for…   37                   [OK]
    pierrecdn/phpipam              phpIPAM web IP address management applicatio…   36                   [OK]
    erikdubbelboer/phpredisadmin   Simple web interface to manage Redis databas…   32                   [OK]
    chialab/php                    Adding common PHP extensions to some of the …   28                   [OK]
    devilbox/php-fpm               PHP-FPM Docker images based on original PHP …   26                   
    phpdockerio/php7-fpm           PHP 7 FPM base container for PHPDocker.io.      19                   [OK]
    phpstan/phpstan                Docker container to check your application w…   19                   [OK]
    phpdockerio/php73-fpm          PHP 7.3 FPM base container for PHPDocker.io.    18                   
    appsvc/php                     Azure App Service php dockerfiles               16                   [OK]
    arm32v7/php                    While designed for web development, the PHP …   14                   
    phpipam/phpipam-cron           phpIPAM is an open-source web IP address man…   14                   
    graze/php-alpine               Smallish php7 alpine image with some common …   12                   [OK]
    phpdockerio/php7-cli           PHP 7 CLI base container image for PHPDocker…   2                    [OK]
    

    选择一个版本进行安装

    docker pull phpdockerio/php73-fpm
    

    查看nginx 默认运行的路径,因为要通过-v命令将容器内部的文件目录挂载到宿主机的对应目录,便于后期项目的管理
    先进入已经启动的nginx容器

    docker exec -it nginx /bin/bash
    #查看nginx服务配置文件
    vi /etc/nginx/conf.d/default.conf
    #其中 root /usr/share/nginx/html 为容器的网络应用的根地址
    

    创建并运行php容器

    docker run -p 9000:9000 --name phpfpm -v /var/www/html:/usr/share/nginx/html -d phpdockerio/php73-fpm
    #-p 将容器的9000端口与宿主机的9000端口进行映射
    

    进入php 容器,找到路径/usr/share/nginx/html
    创建一个文件 index_test.php 文件

    docker exec -it phpfpm bash
    
    #进入容器
    cd /usr/share/nginx/html/ 
    vi test.php
    #根据自己的喜好echo一句话进行测试
    echo '测试PHP文件访问';
    
    • 回到宿主机进入到 /var/www/html 已经生成了test.php文件
    • 进入nginx容器配置访问php 文件
    • 通过命令获取到phpfpm容器的内网ip地址
    #方式一:通过命令行解析的方式进行ip的解析并获取
    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' phpfpm 
    #方式二:通过检查配置的方式返回所有配置信息
    docker inspect  phpfpm 
    
    方式二
    方式一和方式二都可以获取到IP地址,只不过方式一的命令不太方便记忆,我一般常用方式二进行查询配置
    如上:我们获取到172.17.0.3,这个ip 需要配置到nginx容器的/etc/nginx/conf.d/default.conf文件的配置中(熟悉nginx配置的同学也可以自己在/etc/nginx/conf.d/目录下新建属于自己的.conf文件进行配置)
    配置nginx 支持php 文件访问
    docker exec -it nginx bash
    #进入容器配置目录
    cd /etc/nginx/conf.d/
    vi default.conf
    
    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           /usr/share/nginx/html;
            #根据实际情况,配置成我们通过inspect命令获取的ip
            fastcgi_pass   172.17.0.3:9000;
            fastcgi_index  index.php;
            fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    

    重启nginx容器,即可通过IP访问test.php

    docker restart nginx
    # http://ip/test.php
    

    安装mysql

    docker search mysql
    docker pull mysql
    
    docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
    -p 3306:3306:将容器的 3306 端口映射到宿主机的 3306 端口。
    -v -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf。
    -v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs。
    -v $PWD/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。
    -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
    

    常见问题

    MYSQL错误

    用navicat for mysql连接mysql发现报错:Client does not support authentication protocol requested by server
    解决方案:

    #进入容器
    docker exec -it [容器id] /bin/bash
    #登陆mysql
    mysql -uroot -p
    #授权mysql的root用户
    GRANT ALL ON *.* TO 'root'@'%';
    #
    flush privileges;
    #更新加密规则:
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
    #更新root用户密码
    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '换成你的密码';
    #刷新权限
    flush privileges;
    

    保存后重新连接mysql,连接成功
    造成这个问题的原因是因为:

    • 我们通过docker pull mysql安装的mysql版本默认是最新的8.x
    • 在MySQL 8.0.11中,caching_sha2_password是默认的身份验证插件,而不是以往的mysql_native_password
    • 所以我们需要手动调整一下加密规则即可

    相关文章

      网友评论

        本文标题:[Docker]容器基础实用教程

        本文链接:https://www.haomeiwen.com/subject/zzvhirtx.html