[Docker]容器基礎實用教程

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內(nèi)部端口]
-d:后臺運行

查看已經(jīng)運行的容器

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

進入已經(jīng)運行的容器

docker exec -i -t 78317fad8931 /bin/bash
#-----------------------------命令釋義-------------------------------------
方式一(推薦)-exec:退出容器終端,不會導致容器的停止。
方式二(不推薦)-attach:退出容器終端,會導致容器的停止。

停止已啟動的容器

docker stop 78317fad8931

啟動已經(jīng)停止的容器

#重啟
docker restart 78317fad8931

#啟動
docker start 78317fad8931

刪除已經(jīng)停止的容器

# 注意刪除容器前需要先stop容器,否則會出現(xiàn)錯誤
docker rm 78317fad8931
#-----------------------------未停止容器錯誤提示-------------------------------------
Error response from daemon: You cannot remove a running container 78317fad893178246db3c52743c2770704319f6f991160bd3b4f0ddc97ec3949. 
Stop the container before attempting removal or force remove

刪除鏡像

# 注意刪除鏡像前需要先刪除容器,解除容器與鏡像關聯(lián),否則會出現(xiàn)錯誤
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環(huán)境

安裝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

查看運行結(jié)果

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命令將容器內(nèi)部的文件目錄掛載到宿主機的對應目錄,便于后期項目的管理
先進入已經(jīng)啟動的nginx容器

docker exec -it nginx /bin/bash
#查看nginx服務配置文件
vi /etc/nginx/conf.d/default.conf
#其中 root /usr/share/nginx/html 為容器的網(wǎng)絡應用的根地址

創(chuàng)建并運行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
創(chuàng)建一個文件 index_test.php 文件

docker exec -it phpfpm bash
#進入容器
cd /usr/share/nginx/html/ 
vi test.php
#根據(jù)自己的喜好echo一句話進行測試
echo '測試PHP文件訪問';
  • 回到宿主機進入到 /var/www/html 已經(jīng)生成了test.php文件
  • 進入nginx容器配置訪問php 文件
  • 通過命令獲取到phpfpm容器的內(nèi)網(wǎng)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;
        #根據(jù)實際情況,配置成我們通過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發(fā)現(xiàn)報錯:Client does not support authentication protocol requested by server
解決方案:

#進入容器
docker exec -it [容器id] /bin/bash
#登陸mysql
mysql -uroot -p
#授權(quán)mysql的root用戶
GRANT ALL ON *.* TO 'root'@'%';
#
flush privileges;
#更新加密規(guī)則:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
#更新root用戶密碼
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '換成你的密碼';
#刷新權(quán)限
flush privileges;

保存后重新連接mysql,連接成功
造成這個問題的原因是因為:

  • 我們通過docker pull mysql安裝的mysql版本默認是最新的8.x
  • 在MySQL 8.0.11中,caching_sha2_password是默認的身份驗證插件,而不是以往的mysql_native_password
  • 所以我們需要手動調(diào)整一下加密規(guī)則即可
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容