安装 docker 配置 docker 阿里加速和LAMP

51 阅读3分钟

安装 docker 配置 docker 阿里加速和LAMP

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

方式一:yum源安装

vim /etc/yum.repos.d/docker.repo

[docker]
name=docker
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/
gpgcheck=0

yum install -y --setopt=obsoletes=0   docker-ce-17.03.0.ce-1.el7.centos   docker-ce-selinux-17.03.0.ce-1.el7.centos (若之前安装过较高版本,先将所有包卸载即可)

方式二:脚本安装

 Centos 7
[root@centos7 ~]#cat C7_docker.sh
#!/bin/bash
#
#********************************************************************
#Author:          wei
#QQ:   
#Date:         2020-11-14
#FileName:    f2.sh
#URL:   
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
COLOR="echo -e \033[1;31m"
GREEN="echo -e \E[1;32m"
END="\033[m"
VERSION="17.03.0.ce-1.el7.centos"

yum -y install wget &> /dev/null
wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo || { ${COLOR}"互联网连接失败,请检查网络配置!"${END};exit; }

yum clean all 
yum -y install  --setopt=obsoletes=0 docker-ce-$VERSION  docker-ce-selinux-$VERSION || {
${COLOR}"Base,Extras的yum源失败,请检查yum源配置"${END};exit; }

#使用阿里做镜像加速
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF

{
        "registry-mirrors": ["https://si7y7hh.mirror.aliyuncs.com"]
 }

EOF

systemctl enable --now docker

docker version && ${GREEN}"Docker安装成功"${END} || ${COLOR}"Docker安装失败"${END};exit ;

通过 docker 安装一个 LAPM 架构

 

安装docker

#拉取镜像
docker pull wordpress
docker pull mariadb

#查看镜像
docker images

 

#docker运行mariadb数据库容器,指定数据库名称和密码,放到后台

docker run --name db --env  MYSQL_ROOT_PASSWORD=123456 -p 3306:3306   -d mariadb

#docker exec -it db bash  
root@273202c1af40:/# mysql -uroot -p123456  
Welcome to the MariaDB monitor. Commands end with ; or \g.  
Your MariaDB connection id is 3  
Server version: 10.6.5-MariaDB-1:10.6.5+maria~focal mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database wordpress;  
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> create user 'wordpress'@'%' identified by '123456';  
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';  
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges;  
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> \q  
Bye

#docker运行WordPress容器,指定数据库名称,暴露端口允许外网访问,后台执行

docker run --name MyWordPress --link db:mysql -p 8080:80 -d wordpress

#查看运行容器

docker ps

#登录网站访问

http://10.0.0.17:8080

安装包登录WordPress

如下图所示,打开浏览器安装WordPress

 

 

 或直接使用变量不暴露端口

docker run --name db --env MYSQL_ROOT_PASSWORD=123456 -d mariadb

docker exec -it db bash
mysql -uroot -p123456
create database wordpress;
create user 'wordpress'@'%' identified by '123456';
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
flush privileges;

docker run --name MyWordPress --link db:mysql -p 8080:80 -d wordpress 

访问: IP:8080
数据库暴露端口安装数据库需指定配置:

数据库配置如下

 

 

 

报错连接数据库,需对wpuser授权  
[root@centos127 ~]#docker exec -it af47082fbb8e bash  
root@af47082fbb8e:/etc/mysql/conf.d# mysql -uroot -p123456  
mysql> grant all on *.* to wpuser@'%' identified by 'wppass';  
mysql> grant all on *.* to wpuser@'localhost' identified by 'wppass';  
mysql> flush privileges;

 访问http://10.0.0.127/wp-admin/install.php安装wordpres

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


Docker run  命令格式

-i, --interactive         通常和-t一起使用 , 即使没有连接,也要保持连接
-t, --tty                 分配pseudo-TTY,通常和-i一起使用,注意对应的容器必须运行shell才支持进入
-d, --detach              后台运行,默认前台
--name string             字符串为容器指定名称
--h, --hostname           字符串容器主机名
--rm                      在容器退出时自动删除它
-p, --publish list        将容器的端口发布到主机
-P, --publish-all         将所有公开的端口发布到随机端口
--dns list                设置自定义dns服务器
--entrypoint              字符串覆盖图像的默认入口点
--restart=always          启动策略
--privileged              赋予容器扩展特权
-e, --env=[]              设置环境变量
--env-file=[]             读取环境变量的行分隔文件
--addhost                 注入 hostname <> ip解析

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

 **#设置容器总是运行**  
  
[root@ubuntu1804 ~]#docker run -d --name nginx --restart=always -p 80:80 nginx
[root@ubuntu1804 ~]#reboot
[root@ubuntu1804 ~]#docker ps