Docker创建redis容器,且可以通过宿主机访问

刚开始搞开发的时候,朋友就建议有时间学习下docker,通过docker构建环境,快准狠。
可惜过了这么多年,才开始学习。哈哈。
今天主要聊下自己从安装docker,到创建第一个镜像文件,到第一个容器,以及在容器中安装redis,最终通过宿主机访问成功的过程,以及躺过的坑。希望对大家有借鉴价值。

首先在本地电脑安装docker

我是直接手动下载安装19.03.8stable版本 https://www.runoob.com/docker/macos-docker-install.html
Windows连接里也有下载安装方法,请自取。
建议大家去https://hub.docker.com/注册一个账户,方便将自己制作好的image发布到dockerhub中方便大家下载。(当然,不注册也是可以的,从dockerhub上pull镜像是不需要登陆的,另外自己的镜像文件想要分享给其他小伙伴们的话也可以使用export 与 import命令,以后有机会的话会写)
下一步就是镜像加速
19版本界面改了,添加加速地址通过点击小鲸鱼应用-> Perferences->到下图

E2F956BD-BBB6-4E98-9A77-F5CF81FE2097.png

可以使用阿里、163等提供的加速器
重启一下就可以开始愉快的玩耍了。

准备好镜像文件

因为有强迫症,东西都是想自己安装(主要是属性安装目录)我在此是只先弄centos7的镜像,然后自己编译安装的redis。如果嫌弃的话。可以直接在dockerhub上下载官方提供安装好的镜像文件。

docker pull centos:centos7

centos:centos7 冒号前是软件名,后面是版本。建议大家都指定版本,若不选则默认下载最后的latest版本,因为最新出,可能不如前面版本稳定,会有未知bug。

下载需要点时间,完成后

docker images

就可以看到列表下有刚下载的镜像文件了


F16FED34D79D5CD863B037008B66865F.jpg

REPOSITORY:镜像文件名
TAG:版本
IMAGE ID:加密的id
CREATED:创建时间
SIZE:镜像文件大小

ok,现在可以构建容器了,很简单的一条命令

  docker run -itd --name centos centos:centos7

-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
--name="nginx-lb": 为容器指定一个名称;

执行成功后,会返回一个加密id,这就是你的容器id。

docker exec -it centos bash

然后就进入了你熟悉的centos环境了(命令最后的bash,算是docker的一个小bug,原因大概是你进入容器中什么都不做,你会退出,但你执行bash、top之类的命令就可以正常操作了)

安装redis 直接撸code

groupadd -r redis && useradd -r -g redis redis
yum update -y;
yum -y install gcc automake autoconf libtool make wget epel-release gcc-c++;
mkdir -p /usr/src/redis;
wget https://github.com/antirez/redis/archive/5.0.7.tar.gz; 
tar -zxvf 5.0.7.tar.gz -C /usr/src/redis; 
#为了保证image尽量的小,解压完,将安装包删除
rm -rf 5.0.7.tar.gz
cd /usr/src/redis/redis-5.0.7 && make && make PREFIX=/usr/local/redis install;

(redis6最新版本,在编译安装的时候gcc有版本要求,且在编译的时候要指定版本,以后有机会单独聊)

redis-server

就可以看见熟悉的redis开启命令
在开一个客户端,打开客户端 redis-cli 说明可以正常操作了。

至此,一个正常可用的redis容器就可以了。

最后实现从宿主机可以通过客户端访问到redis容器

想要实现联通,需要在生成容器时,指定端口。

docker exec -it -p 8379:6379 centos bash

但是我们现在已经生成好了容器,怎么办呢。
简单,将容器逆向生成镜像文件,然后重新生成新的容器。

docker commit centos  centos/redis

现在你的docker images 里就多了一条镜像 centos/redis

重新run新的容器

docker run -itd -p 8379:6379 --name redis centos/redis bash

现在 docker ps 里就有了两个容器

进入到新的容器 redis里

docker exec -it redis bash

开启redis-server,redis-cli 可以正常使用

然后在宿主机,通过命令行远程访问容器

redis-cli -h 127.0.0.1 -p 8379

#发现会报如下错误
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

解决方法:
更改redis.conf
将配置文件里的protected mode改为了no,原本是yes。

然后重启服务
在容器的cli里设置密码

>config set requirepass 123456
>OK

在通过宿主机登陆

9DEAC33157C6413E9D7EE957AD7E8270.jpg

完美

你可能感兴趣的:(Docker创建redis容器,且可以通过宿主机访问)