前言
為了實現“一鍵部署”的目的,我采用Dockerfile 和 docker-compose來實現自己的目的。這個過程中,我怎么也無法啟動自己的redis-server服務。
目錄結構
👍 ~/Workspace/docker/images/redis tree
.
├── Dockerfile
├── conf
│ └── redis.conf
└── docker-compose.yml
文件內容
Dockerfile
FROM redis:latest
WORKDIR /data/
# 默認的源太慢,原因就是被我大天朝給牆了,所以換成國內,阿里的
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN apt-get clean
# 不能直接 apt-get install curl ,因為容器里面默認apt的包是空的,所以需要更新到本地
RUN apt-get update
# docker 是基於Ubuntu的,所以里面基本默認都帶有apt-get這個工具
RUN apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/* \
&& mkdir -p /usr/local/etc/redis/ \
&& curl http://download.redis.io/redis-stable/redis.conf > /usr/local/etc/redis/redis.conf
CMD [ "redis-server","/usr/local/etc/redis/redis.conf"]
docker-compose.yml
version: "2.2"
services:
redis:
# 使用當前目錄下的Dockerfile構建鏡像
build: .
image: my_redis
container_name: redis
ports:
- "6379:6379"
volumes:
- ./data:/data
# 此處就是引發血案的地方
# - ./conf:/usr/local/etc/redis
問題分析:
-
Dockerfile 在構建的過程中,通過curl獲取到了redis.conf的配置
-
docker-compose 在啟動容器時,由於
volumes
這個地方將本地的目錄掛在到了redis容器內部的/usr/local/etc/redis
下。那么/usr/local/etc/redis
里面的文件就會被全部被本地覆蓋。如果本地./conf
這個目錄下是空的,則/usr/local/etc/redis
里面也會是空的。 -
解決辦法
- 本地的
./conf
文件夾中存在redis.conf
,這樣的文件 - 像上面的案例一樣,不要將redis.conf暴露處理。
- 本地的
調試問題的經過
本次調試,着實讓我頭疼了老一陣,一看死,總是報 can't open file
這種錯誤。我查看了docker 日志,依然無法找到問題。想進入到docker 容器里面去看,結果發現redis容器根本就沒有起來。將Dockerfile 改成如下的形式,才啟動了redis容器,並順利進入到容器里面。才找到原來是redis.conf
文件被覆蓋掉了。
FROM redis:latest
WORKDIR /data/
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN apt-get clean
RUN apt-get update
RUN apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/* \
&& mkdir -p /usr/local/etc/redis/ \
&& curl http://download.redis.io/redis-stable/redis.conf > /usr/local/etc/redis/redis.conf
# CMD [ "redis-server","/usr/local/etc/redis/redis.conf"]
# 啟動容器,直接讓其運行 shell腳本,這樣容器就不會推出了。
CMD ["sh","-c","while true;do sleep 1000 ;done"]