我正在尝试链接Python和Redis容器
docker run -d -p 5000:5000 --link=redis dockerapp:v0.3
6fe4b6a7914126d9436f6a517114e98fbd0dc8f5fa9a0c7d70673f89e08cb792
正在运行的容器的输出
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6fe4b6a79141 dockerapp:v0.3 "python app.py" 3 seconds ago Exited (1) 2 seconds ago unruffled_mestorf
fb3f1364c9f9 dockerapp:v0.3 "python app.py" 11 minutes ago Created suspicious_mclean
2749b8453eb6 redis:3.2.0 "docker-entrypoint.s…" 16 minutes ago Up 16 minutes 6379/tcp redis
e5d9713c462a add2d820aa27 "python app.py" 6 hours ago Exited (137) 3 hours ago elastic_leavitt
ffcfb4cd0082 debian:jessie "bash" 28 hours ago Exited (127) 28 hours ago thirsty_elion
4e8205831452 tomcat:8.0 "catalina.sh run" 30 hours ago Exited (143) 27 hours ago objective_lichterman
49aab8cae20c tomcat:8.0 "catalina.sh run" 31 hours ago Exited (130) 30 hours ago objective_khayyam
如果我通过http://192.168.0.12:5000/浏览器
This site can’t be reached
192.168.0.12 refused to connect.
我的app.py
app = Flask(__name__)
default_key = '1'
cache = redis.Redis(host='redis', port=6379, db=0)
cache.set(default_key : 'one')
@app.route('/', methods=['GET', 'POST'])
def mainpage():
key = default_key
if 'key' in request.form:
key = request.form['key']
if request.method == 'POST' and request.form['submit'] == 'save':
cache.set(key,request.form['cache_value'])
cache_value = None;
if cache.get(key):
cache_value = cache.get(key).decode(utf-8)
return render_template('index.html', key=key, cache_value=cache_value)
if __name__ == '__main__':
app.run(host='0.0.0.0')
日志
File "app.py", line 7
cache.set(default_key : 'one')
^
SyntaxError: invalid syntax
为什么?
答案 0 :(得分:1)
键分配中有错字?这有效:
>>> import redis
>>> default_key = '1'
>>> cache = redis.Redis()
>>> cache.set(default_key, 'one')
True
>>> cache.keys()
[b'1']
答案 1 :(得分:0)
似乎python应用程序的主要容器进程无法正常运行,因为它会使容器停止。您的docker run命令非常好,而且似乎没有port colision的情况。我认为您可以检查以下命令:
python app.py
检查app.py的主要进程,并确保即使数据库连接失败,它也将保持活动状态。