如何在Docker中运行Redis服务器和另一个应用程序? [英] How to run a Redis server AND another application inside Docker?

查看:341
本文介绍了如何在Docker中运行Redis服务器和另一个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个运行在Docker容器内的Django应用程序。我需要在Django应用程序中创建一个线程,所以我将Celery和Redis用作Celery数据库。
如果我在Docker映像(Ubuntu 14.04)中安装redis:

 运行apt-get update&& apt-get -y安装redis-server 
运行点安装redis

Redis服务器是没有启动:Django应用程序抛出异常,因为6379端口上的连接被拒绝。如果我手动启动Redis,它将工作。



如果我启动Redis服务器以下命令挂起:

 运行redis-server 

如果我尝试调整上一行,它不起作用:

  RUN nohup redis-server& 

所以我的问题是:有没有办法在后台启动Redis并重新启动Docker容器重新启动?



Docker最后一个命令已经用于:

 code> CMD uwsgi --http 0.0.0.0:8000  - 模块mymodule.wsgi 


解决方案

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

这篇关于如何在Docker中运行Redis服务器和另一个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆