參考 docker store 裡 redis 映像的使用 幫助:
Alternatively, you can specify something along the same lines with docker run options.
$ docker run -v /myredis/conf/redis。 myredis redis redis-server /usr/local/etc/redis/redis.conf
Where /myredis/conf/ is a local directory containing your redis.conf file. Using this method means that there is noave for you to have Dockerfile for your redis container.
首先我建立了一個目錄 /docker/redis/
並在其中放置設定檔: redis.conf
。
接下來,我使用下面的程式碼啟動鏡像:
> docker run -v /docker/redis:/data --name my-redis -d redis redis-server /data/redis.conf
再用 docker inspect [containerId]
查看剛剛建立的容器發現容器啟動後立即就退出了:
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-02-07T03:15:46.558191922Z",
"FinishedAt": "2017-02-07T03:15:46.973984747Z"
},
...
另外,我嘗試不指定配置文件,只掛著 /data
目錄,是沒有問題,可以順利啟動:
> docker run -v /docker/redis:/data -d redis
好像找到原因了。我將 redis.conf
中的 daemonize yes
註解掉即可運行!但這是什麼原因?
docker-entrypoint.sh
#!/bin/sh
set -e
# first arg is `-f` or `--some-option`
# or first arg is `something.conf`
if [ "${1#-}" != "" ] || [ "${1%.conf}" != "" ]; then
set -- redis-server "$@"
fi
# allow the container to be started with `--user`
if [ "" = 'redis-server' -a "$(id -u)" = '0' ]; then
chown -R redis .
exec gosu redis "rrreee" "$@"
fi
exec "$@"
上面是官方redis 鏡像的entrypoint 腳本,shell 腳本不了解,查了一下第一行set -e
的意思是:"若指令傳回值不等於0 ,則立即退出shell"
,和這個有關嗎?又或者和 docker run
的 -d
參數有關係? "Run container in background and print container ID"
我的是這樣啟動的