且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Docker中运行Redis服务器和另一个应用程序?

更新时间:2022-10-18 23:19:04

RUN 命令只添加新的图像层。它们在运行时没有执行。只有在图像的构建时间。



使用 CMD 。您可以通过将多个命令外部化为由 CMD 调用的shell脚本来组合多个命令:

 code> CMD start.sh 

start.sh 脚本你写下列内容:

 #!/ bin / bash 
nohup redis-server &安培;
uwsgi --http 0.0.0.0:8000 - 模块mymodule.wsgi


I created a Django application which runs inside a Docker container. I needed to create a thread inside the Django application so I used Celery and Redis as the Celery Database. If I install redis in the docker image (Ubuntu 14.04):

RUN apt-get update && apt-get -y install redis-server
RUN pip install redis

The Redis server is not launched: the Django application throws an exception because the connection is refused on the port 6379. If I manually start Redis, it works.

If I start the Redis server with the following command, it hangs :

RUN redis-server

If I try to tweak the previous line, it does not work either :

RUN nohup redis-server &

So my question is: is there a way to start Redis in background and to make it restart when the Docker container is restarted ?

The Docker "last command" is already used with:

CMD uwsgi --http 0.0.0.0:8000 --module mymodule.wsgi

RUN commands are adding new image layers only. They are not executed during runtime. Only during build time of the image.

Use CMD instead. You can combine multiple commands by externalizing them into a shell script which is invoked by CMD:

CMD start.sh

In the start.sh script you write the following:

#!/bin/bash
nohup redis-server &
uwsgi --http 0.0.0.0:8000 --module mymodule.wsgi