周作业九

1、通过 RPM 安装 docker 17.03.0 版本并且配置 docker 阿里加速

[root@centos7 ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
[root@centos7 ~]# cat <<END >/etc/yum.repos.d/docker.repo
> [docker]
> name=docker
> baseurl = https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/
> enabled=1
> gpgcheck=0
> END
[root@centos7 ~]# yum clean all;yum repolist 

#查看docker版本
[root@centos7 ~]# yum list docker-ce --showduplicates | sort -r
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: mirrors.huaweicloud.com
 * epel: mirrors.huaweicloud.com
docker-ce.x86_64                 3:19.03.9-3.el7                          docker
docker-ce.x86_64                 3:19.03.8-3.el7                          docker
docker-ce.x86_64                 3:19.03.7-3.el7                          docker
docker-ce.x86_64                 3:19.03.6-3.el7                          docker
docker-ce.x86_64                 3:19.03.5-3.el7                          docker
docker-ce.x86_64                 3:19.03.4-3.el7                          docker
docker-ce.x86_64                 3:19.03.3-3.el7                          docker
docker-ce.x86_64                 3:19.03.2-3.el7                          docker
docker-ce.x86_64                 3:19.03.1-3.el7                          docker
docker-ce.x86_64                 3:19.03.13-3.el7                         docker
docker-ce.x86_64                 3:19.03.12-3.el7                         docker
docker-ce.x86_64                 3:19.03.11-3.el7                         docker
docker-ce.x86_64                 3:19.03.10-3.el7                         docker
docker-ce.x86_64                 3:19.03.0-3.el7                          docker
docker-ce.x86_64                 3:18.09.9-3.el7                          docker
docker-ce.x86_64                 3:18.09.8-3.el7                          docker
docker-ce.x86_64                 3:18.09.7-3.el7                          docker
docker-ce.x86_64                 3:18.09.6-3.el7                          docker
docker-ce.x86_64                 3:18.09.5-3.el7                          docker
docker-ce.x86_64                 3:18.09.4-3.el7                          docker
docker-ce.x86_64                 3:18.09.3-3.el7                          docker
docker-ce.x86_64                 3:18.09.2-3.el7                          docker
docker-ce.x86_64                 3:18.09.1-3.el7                          docker
docker-ce.x86_64                 3:18.09.0-3.el7                          docker
docker-ce.x86_64                 18.06.3.ce-3.el7                         docker
docker-ce.x86_64                 18.06.2.ce-3.el7                         docker
docker-ce.x86_64                 18.06.1.ce-3.el7                         docker
docker-ce.x86_64                 18.06.0.ce-3.el7                         docker
docker-ce.x86_64                 18.03.1.ce-1.el7.centos                  docker
docker-ce.x86_64                 18.03.0.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.12.1.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.12.0.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.09.1.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.09.0.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.06.2.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.06.1.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.06.0.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.03.3.ce-1.el7                         docker
docker-ce.x86_64                 17.03.2.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.03.1.ce-1.el7.centos                  docker
docker-ce.x86_64                 17.03.0.ce-1.el7.centos                  docker
 * base: mirrors.huaweicloud.com
Available Packages

#安装docker
[root@centos7 ~]# yum -y install docker-ce-17.03.0.ce-1.el7.centos
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:17.03.0.ce-1.el7.centos will be installed
--> Processing Dependency: docker-ce-selinux >= 17.03.0.ce-1.el7.centos for package: docker-ce-17.03.0.ce-1.el7.centos.x86_64
Package docker-ce-selinux is obsoleted by docker-ce, but obsoleting package does not provide for requirements
--> Processing Dependency: libcgroup for package: docker-ce-17.03.0.ce-1.el7.centos.x86_64
--> Processing Dependency: libltdl.so.7()(64bit) for package: docker-ce-17.03.0.ce-1.el7.centos.x86_64
--> Running transaction check
---> Package docker-ce.x86_64 0:17.03.0.ce-1.el7.centos will be installed
--> Processing Dependency: docker-ce-selinux >= 17.03.0.ce-1.el7.centos for package: docker-ce-17.03.0.ce-1.el7.centos.x86_64
Package docker-ce-selinux is obsoleted by docker-ce, but obsoleting package does not provide for requirements
---> Package libcgroup.x86_64 0:0.41-21.el7 will be installed
---> Package libtool-ltdl.x86_64 0:2.4.2-22.el7_3 will be installed
--> Finished Dependency Resolution
Error: Package: docker-ce-17.03.0.ce-1.el7.centos.x86_64 (docker)
           Requires: docker-ce-selinux >= 17.03.0.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch (docker)
               docker-ce-selinux = 17.03.0.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch (docker)
               docker-ce-selinux = 17.03.1.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch (docker)
               docker-ce-selinux = 17.03.2.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.3.ce-1.el7.noarch (docker)
               docker-ce-selinux = 17.03.3.ce-1.el7
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
#查看到上面报错提示,重新安装
[root@centos7 ~]# yum -y install docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch

#启用阿里镜像加速
[root@centos7 ~]# mkdir -p /etc/docker
[root@centos7 ~]# tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://44kfix0d.mirror.aliyuncs.com"]
}
EOF
{
  "registry-mirrors": ["https://8ikfix0d.mirror.aliyuncs.com"]
}
[root@centos7 ~]# systemctl daemon-reload 
[root@centos7 ~]# systemctl enable --now docker

2、通过 docker 安装一个 LAMP 架构

#查找点赞超过10的lamp镜像
[root@centos7 ~]# docker search -s 10 lamp
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mattrayner/lamp     A simple LAMP docker image running the prere…   240                                     [OK]
linode/lamp         LAMP on Ubuntu 14.04.1 LTS Container            178                                     
tutum/lamp          Out-of-the-box LAMP image (PHP+MySQL)           141                                     
greyltc/lamp        a super secure, up-to-date and lightweight L…   101                                     [OK]
fauria/lamp         Modern, developer friendly LAMP stack. Inclu…   93                                      [OK]
lioshi/lamp         Docker image for LAMP under debian              15                                      [OK]
dgraziotin/lamp                                                     11                                      [OK]

#安装lamp镜像
[root@centos7 ~]# docker pull mattrayner/lamp 
Using default tag: latest
latest: Pulling from mattrayner/lamp
c64513b74145: Pull complete 
01b8b12bad90: Pull complete 
c5d85cf7a05f: Pull complete 
b6b268720157: Pull complete 
e12192999ff1: Pull complete 
d39ece66b667: Pull complete 
65599be66378: Pull complete 
1a01f9d0f080: Pull complete 
605c5eef320d: Pull complete 
ec4238960f27: Pull complete 
03fc76524e1f: Pull complete 
02fc5c988d11: Pull complete 
55b6990c2a09: Pull complete 
a49d8ecce316: Pull complete 
4b4d6f40e4e1: Pull complete 
e136dd3c2805: Pull complete 
f049c0f28764: Pull complete 
54419a1f90d4: Pull complete 
d17e99b44c52: Pull complete 
0582fa9cdd6f: Pull complete 
a68382c2bda4: Pull complete 
40f885ad48f1: Pull complete 
3c11e2c69ecd: Pull complete 
eefb377eac87: Pull complete 
1373ef1311e1: Pull complete 
e4fbff56a269: Pull complete 
b98ae4334229: Pull complete 
2db533ff1486: Pull complete 
859e47f43854: Pull complete 
Digest: sha256:bf1ca644324b0fcfc589e6696ce673397853afc6ba665a8d3e4c4c89888c3fb6
Status: Downloaded newer image for mattrayner/lamp:latest
docker.io/mattrayner/lamp:latest

[root@centos7 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mattrayner/lamp     latest              05750cfa54d5        3 months ago        915MB

创建目录以及容器,并挂载到宿主机的目录容器内
[root@centos7 ~]# docker run -d --name lamp -p 8080:80 -p 3306:3306 -v /data/mysql:/var/lib/mysql docker.io/mattrayner/lamp
c650be761018d4376219d5d37ea3473de2b6cf56618d216c466c534b3680ed71

#进入容器,初始化数据库
[root@centos7 ~]# docker exec -it lamp bash
root@c650be761018:/# mysql_secure_installation 

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
New password: 

Re-enter new password: 

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

#此lamp镜像是基于Ubuntu1804系统,查看apache,MySQL,php的版本
root@c650be761018:/# dpkg -l
...省略...
可知:
apache2             2.4.29-1ubuntu
mysql-server-5.7    5.7.30-0ubuntu
php7.4

#下载包查看端口信息
root@c650be761018:/# apt -y install net-tools
root@c650be761018:/# netstat -aptn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      506/apache2         
tcp        0      0 172.17.0.2:58772        91.189.88.142:80        TIME_WAIT   -                   
tcp        0      0 172.17.0.2:37264        91.189.88.152:80        FIN_WAIT2   -  

root@c650be761018:/# vim /var/www/html/index.php 
root@c650be761018:/# cat /var/www/html/index.php #测试页面
<?php
  echo phpinfo();
?>
#访问

在这里插入图片描述

3、写出 docker run 命令的延申指令,如怎么在停止一个 docker 容器的时候自动删除该容器

[root@centos7 ~]# docker run -it --rm centos bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
3c72a8ed6814: Pull complete 
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
[root@f2dcbe30ff28 /]# exit
exit

[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                          NAMES

4、写出 docker run 命令在自动启动 docker 服务时通过什么参数能够启动 docker 中的容器,从而实现容器随着 docker 服务的启动而自动启动

[root@centos7 ~]# docker run -d --name centos-1 --restart=always centos
fa082ca882d65bb09a22f2618af1bc1c295f5e31b8b3e98dcb647faeb81492b1

#测试
[root@centos7 ~]# systemctl stop docker
[root@centos7 ~]# systemctl start docker
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
fa082ca882d6        centos              "/bin/bash"         55 seconds ago      Up Less than a second                           centos-1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值