Dockerfile构建Redis镜像

本文介绍了使用Docker构建Redis镜像的详细步骤。首先建立工作目录,接着编写Dockerfile文件,然后开始构建镜像,之后进行查看和测试容器启动操作,最后通过redis-cli登录测试,成功实现了Redis在Docker中的部署与使用。

建立工作目录

[root@localhost ~]# mkdir redis

[root@localhost ~]# cd redis/

编写Dockerfile文件

[root@localhost redis]# vim Dockerfile

FROM centos:7

MAINTAINER dddd <dddd@163.com>

RUN  yum -y install epel-release && yum -y install redis

RUN sed -i -e 's@bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf

RUN sed -i -e 's@protected-mode yes@protected-mode no@g' /etc/redis.conf

RUN echo "requirepass 123456" >> /etc/redis.conf

#RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime         如果构建镜像报错可以试试把设置时区注释

EXPOSE 6379

CMD [ "/usr/bin/redis-server","/etc/redis.conf"]

  1. FROM centos:7:基于 CentOS 7 镜像构建容器。
  2. MAINTAINER dddd <dddd@163.com>:指定作者和联系方式,但这在 Dockerfile 中已经不再推荐使用。
  3. RUN yum -y install epel-release && yum -y install redis:安装 epel-release 仓库并通过 yum 安装 Redis。
  4. RUN sed -i -e 's@bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf:将 Redis 配置文件中的绑定地址从 127.0.0.1 修改为 0.0.0.0,以允许外部访问。
  5. RUN sed -i -e 's@protected-mode yes@protected-mode no@g' /etc/redis.conf:将 Redis 配置文件中的保护模式设置从 "yes" 修改为 "no",以允许外部访问。
  6. RUN echo "requirepass 123456" >> /etc/redis.conf:在 Redis 配置文件末尾添加一行,设置密码为 "123456",以增加安全性。
  7. #RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime:注释掉的一行代码,原本用于设置时区为上海。
  8. EXPOSE 6379:声明容器将监听的端口号为 6379,以便外部访问。
  9. CMD [ "/usr/bin/redis-server","/etc/redis.conf"]:指定容器启动时要执行的命令,即运行 Redis 服务器并指定配置文件为 /etc/redis.conf。

开始构建镜像

[root@localhost redis]# docker build -t redis:new .

Sending build context to Docker daemon 2.048 kB

Step 1/9 : FROM centos:7

 ---> 74d9a3a7d55d

Step 2/9 : MAINTAINER dddd <dddd@163.com>

 ---> Using cache

 ---> 59011c98ef4e

Step 3/9 : RUN yum -y install epel-release && yum -y install redis

 ---> Using cache

 ---> 63ef1aba6db8

Step 4/9 : RUN sed -i -e 's@bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf

 ---> Using cache

 ---> 96b999c6b66a

Step 5/9 : RUN sed -i -e 's@protected-mode yes@protexted-mode no@g' /etc/redis.conf

 ---> Using cache

 ---> 3f90be33dd18

Step 6/9 : RUN echo "requirepass 123456" >> /etc/redis.conf

 ---> Using cache

 ---> 3da1345a7dea

Step 7/9 : RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

 ---> Running in 0ec226d6d98b

 ---> 76a9a29fdf78

Removing intermediate container 0ec226d6d98b

Step 8/9 : EXPOSE 6379

 ---> Running in 894f0be2bcb3

 ---> 70b6fb7cc6c9

Removing intermediate container 894f0be2bcb3

Step 9/9 : CMD /usr/bin/redis-server /etc/localtime

 ---> Running in f02aff397be4

 ---> aad25b2bc7e3

Removing intermediate container f02aff397be4

Successfully built aad25b2bc7e3

查看

[root@localhost redis]# docker images redis

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

redis               new                 aad25b2bc7e3        42 seconds ago      851 MB

测试容器开始

[root@localhost redis]# docker run -d -p 6379:6379 --name=redis redis:new

02a84355cb28753f481df93341ac9758f78d41e35dc3f8145871573435e82916

查看

[root@localhost redis]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

redis               new                 aad25b2bc7e3        6 minutes ago       851 MB

 

如果源是新的就不用

[root@localhost ~]# ll

-rw-r--r--. 1 root root     15080 7月  17 2020 epel-release-latest-7.noarch.rpm

[root@localhost ~]# rpm -ivh /root/epel-release-latest-7.noarch.rpm

准备中...                          ################################# [100%]

软件包 epel-release-7-14.noarch (比 epel-release-7-11.noarch 还要新) 已经安装

file /etc/yum.repos.d/epel-testing.repo from install of epel-release-7-11.noarch conflicts with file from package epel-release-7-14.noarch

file /etc/yum.repos.d/epel.repo from install of epel-release-7-11.noarch conflicts with file from package epel-release-7-14.noarch

file /usr/lib/systemd/system-preset/90-epel.preset from install of epel-release-7-11.noarch conflicts with file from package epel-release-7-14.noarch

本地没有  下载测试

[root@localhost redis]#  yum -y install redis

登陆成功

[root@localhost redis]#  redis-cli -h localhost -a 123456
localhost:6379> ping
PONG

localhost:6379>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值