ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Docker 学习笔记

2021-07-01 10:33:00  阅读:168  来源: 互联网

标签:harbor 笔记 学习 nginx mysql Docker php root docker


Centos7 安装Docker CE

前言

Docker 提供了两个版本:社区版 (CE) 和企业版 (EE)。

Docker 社区版 (CE) 是开发人员和小型团队开始使用 Docker 并尝试使用基于容器的应用的理想之选。

本文介绍下Docker 社区版 (CE) 在CentOS 7系统中的里两种安装方式

安装前提

· 64 位版本的 CentOS 7

· CentOS 系统的内核版本高于 3.10(uname -r命令可查看系统内核版本)

安装 Docker CE

  1. 设置 Docker 的镜像仓库并从中进行安装
  2. 下载 RPM 软件包并手动进行安装(例如,在不能访问互联网的隔离系统中安装 Docker)

从镜像仓库安装

确保 yum 包更新到最新:

sudo yum update

安装一些必要的系统工具:

· yum-utils 提供了 yum-config-manager 实用程序,用于设置yum源

· devicemapper 存储驱动需要 device-mapper-persistent-data 和 lvm2

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

设置镜像仓库:

阿里云仓库地址(推荐)

sudo yum-config-manager \

 --add-repo \

 http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

官方仓库地址

sudo yum-config-manager \

 --add-repo \

 https://download.docker.com/linux/centos/docker-ce.repo

更新 yum 软件包索引。

sudo yum makecache fast

查看仓库中所有docker版本

sudo yum list docker-ce --showduplicates | sort -r

安装 Docker CE 最新版本

sudo yum install docker-ce 

安装 Docker-ce 指定版本

sudo yum install docker-ce-版本号

sudo yum -y install docker-ce-18.03.1.ce-1.el7.centos

启动 Docker

sudo systemctl start docker    #启动

sudo systemctl restart docker  #重启

sudo systemctl enable docker   #加入开机启动

Docker 版本信息查看

sudo docker version

网易镜像源

编辑/etc/docker/daemon.json(如果不存在则创建):

vi /etc/**docker**/daemon.json   #内容如下

{
"registry-mirrors":[
"http://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com",
"http://registry.docker-cn.com"
] ,
"insecure-registries":[
"docker.mirrors.ustc.edu.cn",
"registry.docker-cn.com"
]
}

重启Docker服务

sudo systemctl restart docker

配置加速器

访问该地址获取配置加速脚本https://www.daocloud.io/mirror

直接执行该脚本,重启docker服务 systemctl restart docker,之后下载容器得到加速

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

Docker 部署lnmp

关闭防火墙、关闭selinux

[root@docker-server ~]# setenforce 0
[root@docker-server ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g'  /etc/selinux/config   //永久关闭selinux
[root@docker-server ~]# systemctl stop firewalld
[root@docker-server ~]# systemctl disable firewalld    //开机不启动防火墙

下载所需要的镜像 (nginx 1.12.2 ,mysql 5.7,php 7.2) 版本可自选

获取 Mysql 5.7 镜像

[root@docker-server ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
5e6ec7f28fb7: Pull complete 
4140e62498e1: Pull complete 
e7bc612618a0: Pull complete 
1af808cf1124: Pull complete 
.....

启动容器 cs_mysql

[root@docker-server ~]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=xiaoqiu --name cs_mysql mysql:5.7

参数说明
-d 让容器在后台运行
-p 添加主机到容器的端口映射
-e 设置环境变量,这里设置mysql的root用户的初始密码
-name 容器的名称、只要求唯一性

获取php 7.2镜像

[root@docker-server ~]# docker pull php:7.2-fpm
7.2-fpm: Pulling from library/php
5e6ec7f28fb7: Already exists 
cf165947b5b7: Pull complete 
7bd37682846d: Pull complete 
.....

启动php 容器

[root@docker-server ~]# docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link cs_mysql:mysql --name cs_phpfpm php:7.2-fpm

参数说明
-d 让容器在后台运行
-p 添加主机到容器的端口映射
-v 添加目录映射,主机上的/var/nginx/www/html映射到容器里面 的/var/www/html,如果主机没有这个目录就创建这个目录,或者映射别的目录,但是后面路径要统一
-name 容器的名字
-link 容器与另外一个容器建立联系,这样就可以在当前的容器去使用另一个容器里的服务

测试目录映射
在主机/var/nginx/www/html 目录下创建php测试页面,会直接映射到容器里面

[root@docker-server ~]# cat /var/nginx/www/html/index.php 
<?
	phpinfo();
?>

//看直接映射到了容器内部了

[root@docker-server ~]# docker exec -it 92fc30be9a85 /bin/bash   //进入到php的容器里面了
root@92fc30be9a85:/var/www/html# ls  
index.php

php的扩展安装

安装php-redis

root@92fc30be9a85:~# pecl install redis && docker-php-ext-enable redis
downloading redis-4.2.0.tgz ...
Starting to download redis-4.2.0.tgz (235,569 bytes)
.................................................done: 235,569 bytes
25 source files, building
running: phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
enable igbinary serializer support? [no] :          //no  或者空格都可以
enable lzf compression support? [no] : 
building in /tmp/pear/temp/pear-build-defaultuserJI3qgU/redis-4.2.0
running: /tmp/pear/temp/redis/configure --with-php-config=/usr/local/bin/php-config --enable-redis-igbinary=no --enable-redis-lzf=no
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking whether the C compiler works... yes
......此处省略

安装后执行 php -m //查看安装了那些

root@92fc30be9a85:~# php -m
[PHP Modules]
.。...此处省略
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
redis                         //这就是安装的redis
Reflection

获取Nginx 1.12.2镜像

[root@docker-server ~]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete 
e3eaf3d87fe0: Pull complete 
38cb13c1e4c9: Pull complete 
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2

启动Nginx容器

[root@docker-server ~]# docker run -d -p 80:80 --name cs_nginx -v /var/nginx/www/html:/var/www/html --link cs_phpfpm:phpfpm --name cs_nginx nginx:1.12.2

参数说明
-d 让容器运行在后台
-p 添加主机到容器的端口映射
-v 添加目录映射,这里最好nginx容器的根目录最好写成和php容器中根目录一样。但是不一定非要一样,如果不一样在配置nginx的时候需要注意
-name 容器的名称
-link 容器之间建立起来联系

修改Nginx 的配置文件

使他支持php ,方式有多种,这里我直接拷贝出配置文件修改在导进去即可

[root@docker-server opt]# docker cp 2a8e910b40db:/etc/nginx/conf.d/default.conf /opt/
[root@docker-server opt]# cat default.conf 
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /var/www/html;                       // 修改这里默认的路径
        index  index.html index.htmi index.php;     //这里加入添加php的方式
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        root           html;
        fastcgi_pass   92fc30be9a85:9000;        //这里可以用容器ID,也可以用容器IP,都具备唯一性
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;      //修改这里的路径
        include        fastcgi_params;
    }
}

修改完毕导入配置文件到Nginx 容器里面

[root@docker-server opt]# docker cp default.conf  2a8e910b40db:/etc/nginx/conf.d/default.conf

进入到容器里面重新加载配置文件

[root@docker-server ~]# docker exec -it 2a8e910b40db /bin/bash
root@2a8e910b40db:/# nginx -t             Nginx检测配置文件是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@2a8e910b40db:/# nginx -s reload
2019/02/02 15:56:49 [notice] 24#24: signal process started

参数说明
-t 在容器里面生产一个伪终端
-i 对容器内的标准输入(STDIN)进行交互

查看所有容器映射的端口

[root@docker-server opt]# ss -anlt           注:容器映射的端口可以修改的
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                                                :::*   

访问浏览器测试
看到这个页面说明搭建成功了!

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2a4TM0vY-1625105671225)(F:\1.文档 学习教程 笔记\0.笔记\MD笔记\src\php-web.png)]

docker-compose的安装

官方链接

https://docs.docker.com/compose/install/

下载源

非官方源(推荐):

sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" > /usr/local/bin/docker-compose

几乎秒下载完

官方源(不推荐,因为国内速度会特别慢)

sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

赋权:

sudo chmod +x /usr/local/bin/docker-compose

创建软连接

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

查看成功没有:

docker-compose --version

或者

docker-compose version

如果出现版本号,即安装成功了

docker-compose version 1.27.4, build 40524192
但如果出现 docker-compose 未识别命令

3.其它
如果还是提示不识别,那么安装以下扩展源试试

安装扩展源

sudo yum -y install epel-release

安装python-pip模块

sudo yum install python-pip

卸载:

sudo rm /usr/local/bin/docker-compose

docker-harbor2.2.1安装

下载harbor离线安装包

wget https://github.com/goharbor/harbor/releases/download/v2.2.1/harbor-offline-installer-v2.2.1.tgz

GitHub 下载文件加速

https://shrill-pond-3e81.hunsh.workers.dev/

修改相关配置

tar -zxvf harbor-offline-installer-v2.2.1.tgz
cd harbor
cp harbor.yml.tmpl  harbor.yml
vim harbor.yml
#我这里只修改了两个参数,注释了https相关配置,内部用http和https无所谓了,其他的使用默认参数
hostname: 10.1.31.155    #harbor访问地址
harbor_admin_password: xiaoqiu     #harbor访问密码

拉取镜像、启动容器

[root@localhost]#./prepare   #离线安装不执行
[root@localhost]# ./install.sh 
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating registryctl   ... done
Creating registry      ... done
Creating harbor-portal ... done
Creating harbor-db     ... done
Creating redis         ... done
Creating harbor-core   ... done
Creating harbor-jobservice ... done
Creating nginx             ... done
✔ ----Harbor has been installed and started successfully.----
[root@localhost]# docker container ls    #可以看到各种组件都启动了

web端打开harbor界面

直接输入harbor.yml文件hostname对应的地址就行

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lib6QnVO-1625105671241)(F:\1.文档 学习教程 笔记\0.笔记\MD笔记\src\harbor-web.png)]

创建项目

-----------------------------------------------------------以上部署完成------------------------------------------------------------
创建一个项目目录mysql,nginx,php,测试下推送和拉取镜像

打tag、推送拉取镜像

#将本地harbor镜像库地址添加到docker中,重新启动docker

1、cat >> /etc/docker/daemon.json << EOF
{
"insecure-registries" : ["10.1.31.155"]
}
EOF
[root@localhost]# systemctl restart docker
[root@localhost]# docker-compose restart     #重新启动所有harbor容器

#登录harbor库,登录后账号密码默认存储在/root/.docker/config.json

2、[root@localhost]# docker login 10.1.31.155
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

#打标签、推送
#打tag、类似于修改image名字,然后推送会根据你修改的名字去推送到指定的库
#把我们本地的镜像mysql:5.7打个标签推送到私有库上

[root@localhost]# docker tag mysql:5.7 10.1.31.155/mysql/mysql:5.7.34
[root@localhost]# docker push 10.1.31.155/mysql/mysql:5.7.34
The push refers to repository [10.1.31.155/test/harbor]
5233fbaf23ed: Pushed 
aad9bed872f0: Pushed 
b35ba3bc6760: Pushed 
d4b2501bd60f: Pushed 
23e1126e5547: Pushed 
v0.0.1: digest: sha256:2a575d26bc2636af447373d890f4f45f722f4f98e8084072e426a5fd5de20350 size: 1369

#先删除本地mysql:5.7.34,拉取

[root@localhost]# docker image rm 10.1.31.155/mysql/mysql:5.7.34
[root@localhost]# docker pull 10.1.31.155/mysql/mysql:5.7.34
v0.0.1: Pulling from test/harbor
Digest: sha256:2a575d26bc2636af447373d890f4f45f722f4f98e8084072e426a5fd5de20350
Status: Downloaded newer image for 10.1.31.155/mysql/mysql:5.7.34
10.1.31.155/mysql/mysql:5.7.34

#查看下载情况

[root@localhost]# docker image ls
REPOSITORY                      TAG       IMAGE ID       CREATED       	SIZE
10.1.31.155/mysql/mysql   		5.7.34    09361feeb475   7 days ago     447MB
mysql                     		5.7       09361feeb475   7 days ago     447MB
10.1.31.155/php/php       		7.2-fpm   28f52b60203d   6 months ago   398MB
php                       		7.2-fpm   28f52b60203d   6 months ago   398MB
10.1.31.155/nginx/nginx   		1.12.2    4037a5562b03   3 years ago    108MB
nginx                     		1.12.2    4037a5562b03   3 years ago    108MB

#Harbor所有容器组相关命令

[root@harbor harbor]# docker-compose stop   //停止Harbor

[root@harbor harbor]# docker-compose start  //启动Harbor

[root@harbor harbor]# docker-compose restart  //重启Harbor

[root@harbor harbor]# docker-compose ps  //Harbor容器组状态

参考文档:

https://www.jianshu.com/p/a8bf7efe8db0

标签:harbor,笔记,学习,nginx,mysql,Docker,php,root,docker
来源: https://blog.csdn.net/qq_16224495/article/details/118380463

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有