参考 docker store 里 redis 镜像的使用 帮助:
Alternatively, you can specify something along the same lines with docker run options.
$ docker run -v /myredis/conf/redis.conf:/usr/local/etc/redis/redis.conf --name 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 no need for you to have a 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
注释掉即可运行!但这是什么原因?
#!/bin/shset -e# first arg is `-f` or `--some-option`# or first arg is `something.conf`if [ "${1#-}" != "$1" ] || [ "${1%.conf}" != "$1" ]; then set -- redis-server "$@"fi# allow the container to be started with `--user`if [ "$1" = 'redis-server' -a "$(id -u)" = '0' ]; then chown -R redis . exec gosu redis "$0" "$@"fiexec "$@"
上面是官方 redis 镜像的 entrypoint 脚本,shell 脚本不了解,查了一下第一行 set -e
的意思是:"若指令传回值不等于 0 ,则立即退出 shell"
,和这个有关吗?又或者和 docker run
的 -d
参数有关系?"Run container in background and print container ID"
解决方案
我的是这么启动的
docker run -d -ti \-p 7481:6379 \-v /project/pos/conf/redis.conf:/etc/redis/redis.conf \-v /project/pos/data:/data \--restart always \--name pos_redis \daocloud.io/redis \redis-server /etc/redis/redis.conf