2020-11-15 docker容器

作业1115

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

# rpm...选择centos7 安装
#————————————————————————————————————————————————————————————————————————————
# 方法一:
# 添加仓库:
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装:
yum install -y --setopt=obsoletes=0 \
docker-ce-17.03.1.ce-1.el7.centos   docker-ce-selinux-17.03.1.ce-1.el7.centos

systemctl enable --now docker.service
docker version
#----------------------------------------------------------------------------
# 方法二:
# 依赖包 docker-ce-selinux
yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
# 安装包 docker-ce
yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm

'也可以直接一步安装:
yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm   https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm

systemctl enable --now docker.service
docker version

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

思路:将AMP分别运行为3个容器

1 = ubuntu安装docker
# step 1: 更新并安装必要的系统工具
apt update
apt -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 将阿里源添加至仓库
add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 安装Docker-CE
apt -y install docker-ce

# Step 5: 检查
[root@ubuntu1804 ~]#docker version 
Client: Docker Engine - Community
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 17:02:36 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:01:06 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.3.7
  GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

2 = 运行一个centos容器。。。在此平台上安装AMP

# 启动
'注意:--privileged、/usr/sbin/init 实现命令systemctl的正常使用
[root@ubuntu1804 ~]#docker run -p 80:80 -p 3306:3306 -p 9000:9000 --restart=always --privileged --name=centos -itd centos /usr/sbin/init
d4dc50dc03305884b3868d70006be4ac39b88451d249fca5460a4e66bb080e8f

# 进入
[root@ubuntu1804 ~]#docker exec -it centos /bin/bash
[root@d4dc50dc0330 /]#

# 查看发行版本
[root@214197b4245d /]# cat /etc/redhat-release 
CentOS Linux release 8.2.2004 (Core) 

# 换成阿里源
[root@214197b4245d /]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@214197b4245d /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2595  100  2595    0     0   7654      0 --:--:-- --:--:-- --:--:--  7654

# 缓存
[root@214197b4245d /]#  yum makecache
Failed to set locale, defaulting to C.UTF-8
Repository AppStream is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
CentOS-8 - AppStream                                                   613 kB/s | 5.8 MB     00:09
CentOS-8 - Base - mirrors.aliyun.com                                   196 kB/s | 2.2 MB     00:11
CentOS-8 - Extras - mirrors.aliyun.com                                 1.6 kB/s | 8.6 kB     00:05
Metadata cache created.

# 相关包
dnf -y install httpd php php-json php-mysqlnd mariadb-server

# 启动
systemctl enable --now httpd mariadb

# 创建 wordpress库、wordpress账号
mysql -e 'create database wordpress;'
mysql -e 'grant all on wordpress.* to wordpress@"localhost" identified by "centos";'   # 单双引错开

# 本机ip
[root@d4dc50dc0330 /]# hostname -I
172.17.0.2 

# 通过scp去宿主机拉取 wordpress安装包
# 宿主机安装。。。
apt -y install lrzsz
# 放入安装包
[root@ubuntu1804 ~]#ls wordpress-5.4.2-zh_CN.tar.gz 
wordpress-5.4.2-zh_CN.tar.gz

# 容器中安装相关工具实现scp。。。
yum -y install openssh-server  ssh
systemctl enable --now sshd

# 拉取安装包!!!!!
'注意:如果从宿主机推送,容器的root密码错误。。。
scp 10.0.0.15:/root/wordpress-5.4.2-zh_CN.tar.gz .

# 解压到 网页目录
tar xvf wordpress-5.4.2-zh_CN.tar.gz -C /var/www/html/
# 查看
[root@d4dc50dc0330 /]# ls /var/www/html/
wordpress

# 设置文件权限
chown -R apache.apache /var/www/html/

# 测试访问
http://10.0.0.15/wordpress

浏览器访问:

1605432650600.png

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

# 当容器退出时自动移除容器
docker run --rm xxx

'注意: 不能与选项 -d 共用

4、 docker run 命令通过什么参数能够实现容器随着 docker 服务的启动而自动启动

docker run --restart=always xxx

# --restart=always:无论容器的退出状态如何,始终重启容器

你可能感兴趣的:(2020-11-15 docker容器)