How to connect with redis sever in python while having a Key Error?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to set up my first Docker Container. For this I wrote an app where you can choose between 3 options and vote for your prefered. However I get a Key Error telling me, that I haven't defined my variable 'REDIS' to connect to the Redis server. How do I connect with it?
I don't know where or how to link it in my code. And whatever I try I get back that error. Here the important code:
from flask import Flask, request, render_template
import os
import random
import redis
import socket
import sys
app = Flask(__name__)
app.config.from_pyfile('config_file.cfg')
if("VOTE1VALUE" in os.environ and os.environ['VOTE1VALUE']):
button1 = os.environ['VOTE1VALUE']
else:
button1 = app.config['VOTE1VALUE']
if("VOTE2VALUE" in os.environ and os.environ['VOTE2VALUE']):
button2 = os.environ['VOTE2VALUE']
else:
button2 = app.config['VOTE2VALUE']
if("VOTE3VALUE" in os.environ and os.environ['VOTE3VALUE']):
button2 = os.environ['VOTE3VALUE']
else:
button2 = app.config['VOTE3VALUE']
if("TITLE" in os.environ and os.environ['TITLE']):
title = os.environ['TITLE']
else:
title = app.config['TITLE']
redis_server = os.environ['REDIS']
try:
if "REDIS_PWD" in os.environ:
r = redis.StrictRedis(host=redis_server,
port = 6379,
password = os.environ['REDIS_PWD'])
else:
r = redis.Redis(redis_server)
r.ping()
except redis.ConnectionError:
exit('Failed to connect to Redis, terminating.')
I thought I'd get it to work like this but when I try to run the docker image I get following Error:
File "programm.py", line 32, in <module>
redis_server = os.environ['REDIS']
File "/usr/local/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'REDIS'
My question is how to get it to work? And how to connect my programm to the Redis Server.
Thanks in advance!
python
add a comment |
I'm trying to set up my first Docker Container. For this I wrote an app where you can choose between 3 options and vote for your prefered. However I get a Key Error telling me, that I haven't defined my variable 'REDIS' to connect to the Redis server. How do I connect with it?
I don't know where or how to link it in my code. And whatever I try I get back that error. Here the important code:
from flask import Flask, request, render_template
import os
import random
import redis
import socket
import sys
app = Flask(__name__)
app.config.from_pyfile('config_file.cfg')
if("VOTE1VALUE" in os.environ and os.environ['VOTE1VALUE']):
button1 = os.environ['VOTE1VALUE']
else:
button1 = app.config['VOTE1VALUE']
if("VOTE2VALUE" in os.environ and os.environ['VOTE2VALUE']):
button2 = os.environ['VOTE2VALUE']
else:
button2 = app.config['VOTE2VALUE']
if("VOTE3VALUE" in os.environ and os.environ['VOTE3VALUE']):
button2 = os.environ['VOTE3VALUE']
else:
button2 = app.config['VOTE3VALUE']
if("TITLE" in os.environ and os.environ['TITLE']):
title = os.environ['TITLE']
else:
title = app.config['TITLE']
redis_server = os.environ['REDIS']
try:
if "REDIS_PWD" in os.environ:
r = redis.StrictRedis(host=redis_server,
port = 6379,
password = os.environ['REDIS_PWD'])
else:
r = redis.Redis(redis_server)
r.ping()
except redis.ConnectionError:
exit('Failed to connect to Redis, terminating.')
I thought I'd get it to work like this but when I try to run the docker image I get following Error:
File "programm.py", line 32, in <module>
redis_server = os.environ['REDIS']
File "/usr/local/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'REDIS'
My question is how to get it to work? And how to connect my programm to the Redis Server.
Thanks in advance!
python
add a comment |
I'm trying to set up my first Docker Container. For this I wrote an app where you can choose between 3 options and vote for your prefered. However I get a Key Error telling me, that I haven't defined my variable 'REDIS' to connect to the Redis server. How do I connect with it?
I don't know where or how to link it in my code. And whatever I try I get back that error. Here the important code:
from flask import Flask, request, render_template
import os
import random
import redis
import socket
import sys
app = Flask(__name__)
app.config.from_pyfile('config_file.cfg')
if("VOTE1VALUE" in os.environ and os.environ['VOTE1VALUE']):
button1 = os.environ['VOTE1VALUE']
else:
button1 = app.config['VOTE1VALUE']
if("VOTE2VALUE" in os.environ and os.environ['VOTE2VALUE']):
button2 = os.environ['VOTE2VALUE']
else:
button2 = app.config['VOTE2VALUE']
if("VOTE3VALUE" in os.environ and os.environ['VOTE3VALUE']):
button2 = os.environ['VOTE3VALUE']
else:
button2 = app.config['VOTE3VALUE']
if("TITLE" in os.environ and os.environ['TITLE']):
title = os.environ['TITLE']
else:
title = app.config['TITLE']
redis_server = os.environ['REDIS']
try:
if "REDIS_PWD" in os.environ:
r = redis.StrictRedis(host=redis_server,
port = 6379,
password = os.environ['REDIS_PWD'])
else:
r = redis.Redis(redis_server)
r.ping()
except redis.ConnectionError:
exit('Failed to connect to Redis, terminating.')
I thought I'd get it to work like this but when I try to run the docker image I get following Error:
File "programm.py", line 32, in <module>
redis_server = os.environ['REDIS']
File "/usr/local/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'REDIS'
My question is how to get it to work? And how to connect my programm to the Redis Server.
Thanks in advance!
python
I'm trying to set up my first Docker Container. For this I wrote an app where you can choose between 3 options and vote for your prefered. However I get a Key Error telling me, that I haven't defined my variable 'REDIS' to connect to the Redis server. How do I connect with it?
I don't know where or how to link it in my code. And whatever I try I get back that error. Here the important code:
from flask import Flask, request, render_template
import os
import random
import redis
import socket
import sys
app = Flask(__name__)
app.config.from_pyfile('config_file.cfg')
if("VOTE1VALUE" in os.environ and os.environ['VOTE1VALUE']):
button1 = os.environ['VOTE1VALUE']
else:
button1 = app.config['VOTE1VALUE']
if("VOTE2VALUE" in os.environ and os.environ['VOTE2VALUE']):
button2 = os.environ['VOTE2VALUE']
else:
button2 = app.config['VOTE2VALUE']
if("VOTE3VALUE" in os.environ and os.environ['VOTE3VALUE']):
button2 = os.environ['VOTE3VALUE']
else:
button2 = app.config['VOTE3VALUE']
if("TITLE" in os.environ and os.environ['TITLE']):
title = os.environ['TITLE']
else:
title = app.config['TITLE']
redis_server = os.environ['REDIS']
try:
if "REDIS_PWD" in os.environ:
r = redis.StrictRedis(host=redis_server,
port = 6379,
password = os.environ['REDIS_PWD'])
else:
r = redis.Redis(redis_server)
r.ping()
except redis.ConnectionError:
exit('Failed to connect to Redis, terminating.')
I thought I'd get it to work like this but when I try to run the docker image I get following Error:
File "programm.py", line 32, in <module>
redis_server = os.environ['REDIS']
File "/usr/local/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'REDIS'
My question is how to get it to work? And how to connect my programm to the Redis Server.
Thanks in advance!
python
python
edited Jan 3 at 8:09
bruno desthuilliers
51.8k54465
51.8k54465
asked Jan 3 at 7:58
nataliefoersternataliefoerster
35
35
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your code fails as there is no environment variable 'REDIS'.
Depending on what you can edit and how you are running your container, you might:
- Declare the REDIS ENV variable in the Dockerfile
- Extend the Docker image just to add your REDIS ENV variable. Then use the new image
- Specify the environment variable value with the
docker run
option "-e
" - In case of Docker compose, you can specify the environment variable in the Docker compose file. In this case you might also include Redis as a Docker image, thus it would all be self-contained. Beware of modules though
Hope this helps.
Thank you! How do I declare the REDIS ENV in my Dockerfile?
– nataliefoerster
Jan 3 at 8:31
From Docker manual:ENV <key> <value>
orENV <key>=<value> ...
Remember to rebuild the image though.
– fcracker79
Jan 3 at 8:39
Ok thank you, but I mean I set ENV REDIS <value> and what is the value?
– nataliefoerster
Jan 3 at 9:16
Based on the code you attached, it should contain the Redis hostname.
– fcracker79
Jan 3 at 9:41
I'm very sorry for being so stupid but can you give the explicit example? I don't get how to do it ...
– nataliefoerster
Jan 3 at 10:00
|
show 11 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018378%2fhow-to-connect-with-redis-sever-in-python-while-having-a-key-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code fails as there is no environment variable 'REDIS'.
Depending on what you can edit and how you are running your container, you might:
- Declare the REDIS ENV variable in the Dockerfile
- Extend the Docker image just to add your REDIS ENV variable. Then use the new image
- Specify the environment variable value with the
docker run
option "-e
" - In case of Docker compose, you can specify the environment variable in the Docker compose file. In this case you might also include Redis as a Docker image, thus it would all be self-contained. Beware of modules though
Hope this helps.
Thank you! How do I declare the REDIS ENV in my Dockerfile?
– nataliefoerster
Jan 3 at 8:31
From Docker manual:ENV <key> <value>
orENV <key>=<value> ...
Remember to rebuild the image though.
– fcracker79
Jan 3 at 8:39
Ok thank you, but I mean I set ENV REDIS <value> and what is the value?
– nataliefoerster
Jan 3 at 9:16
Based on the code you attached, it should contain the Redis hostname.
– fcracker79
Jan 3 at 9:41
I'm very sorry for being so stupid but can you give the explicit example? I don't get how to do it ...
– nataliefoerster
Jan 3 at 10:00
|
show 11 more comments
Your code fails as there is no environment variable 'REDIS'.
Depending on what you can edit and how you are running your container, you might:
- Declare the REDIS ENV variable in the Dockerfile
- Extend the Docker image just to add your REDIS ENV variable. Then use the new image
- Specify the environment variable value with the
docker run
option "-e
" - In case of Docker compose, you can specify the environment variable in the Docker compose file. In this case you might also include Redis as a Docker image, thus it would all be self-contained. Beware of modules though
Hope this helps.
Thank you! How do I declare the REDIS ENV in my Dockerfile?
– nataliefoerster
Jan 3 at 8:31
From Docker manual:ENV <key> <value>
orENV <key>=<value> ...
Remember to rebuild the image though.
– fcracker79
Jan 3 at 8:39
Ok thank you, but I mean I set ENV REDIS <value> and what is the value?
– nataliefoerster
Jan 3 at 9:16
Based on the code you attached, it should contain the Redis hostname.
– fcracker79
Jan 3 at 9:41
I'm very sorry for being so stupid but can you give the explicit example? I don't get how to do it ...
– nataliefoerster
Jan 3 at 10:00
|
show 11 more comments
Your code fails as there is no environment variable 'REDIS'.
Depending on what you can edit and how you are running your container, you might:
- Declare the REDIS ENV variable in the Dockerfile
- Extend the Docker image just to add your REDIS ENV variable. Then use the new image
- Specify the environment variable value with the
docker run
option "-e
" - In case of Docker compose, you can specify the environment variable in the Docker compose file. In this case you might also include Redis as a Docker image, thus it would all be self-contained. Beware of modules though
Hope this helps.
Your code fails as there is no environment variable 'REDIS'.
Depending on what you can edit and how you are running your container, you might:
- Declare the REDIS ENV variable in the Dockerfile
- Extend the Docker image just to add your REDIS ENV variable. Then use the new image
- Specify the environment variable value with the
docker run
option "-e
" - In case of Docker compose, you can specify the environment variable in the Docker compose file. In this case you might also include Redis as a Docker image, thus it would all be self-contained. Beware of modules though
Hope this helps.
answered Jan 3 at 8:07
fcracker79fcracker79
420314
420314
Thank you! How do I declare the REDIS ENV in my Dockerfile?
– nataliefoerster
Jan 3 at 8:31
From Docker manual:ENV <key> <value>
orENV <key>=<value> ...
Remember to rebuild the image though.
– fcracker79
Jan 3 at 8:39
Ok thank you, but I mean I set ENV REDIS <value> and what is the value?
– nataliefoerster
Jan 3 at 9:16
Based on the code you attached, it should contain the Redis hostname.
– fcracker79
Jan 3 at 9:41
I'm very sorry for being so stupid but can you give the explicit example? I don't get how to do it ...
– nataliefoerster
Jan 3 at 10:00
|
show 11 more comments
Thank you! How do I declare the REDIS ENV in my Dockerfile?
– nataliefoerster
Jan 3 at 8:31
From Docker manual:ENV <key> <value>
orENV <key>=<value> ...
Remember to rebuild the image though.
– fcracker79
Jan 3 at 8:39
Ok thank you, but I mean I set ENV REDIS <value> and what is the value?
– nataliefoerster
Jan 3 at 9:16
Based on the code you attached, it should contain the Redis hostname.
– fcracker79
Jan 3 at 9:41
I'm very sorry for being so stupid but can you give the explicit example? I don't get how to do it ...
– nataliefoerster
Jan 3 at 10:00
Thank you! How do I declare the REDIS ENV in my Dockerfile?
– nataliefoerster
Jan 3 at 8:31
Thank you! How do I declare the REDIS ENV in my Dockerfile?
– nataliefoerster
Jan 3 at 8:31
From Docker manual:
ENV <key> <value>
or ENV <key>=<value> ...
Remember to rebuild the image though.– fcracker79
Jan 3 at 8:39
From Docker manual:
ENV <key> <value>
or ENV <key>=<value> ...
Remember to rebuild the image though.– fcracker79
Jan 3 at 8:39
Ok thank you, but I mean I set ENV REDIS <value> and what is the value?
– nataliefoerster
Jan 3 at 9:16
Ok thank you, but I mean I set ENV REDIS <value> and what is the value?
– nataliefoerster
Jan 3 at 9:16
Based on the code you attached, it should contain the Redis hostname.
– fcracker79
Jan 3 at 9:41
Based on the code you attached, it should contain the Redis hostname.
– fcracker79
Jan 3 at 9:41
I'm very sorry for being so stupid but can you give the explicit example? I don't get how to do it ...
– nataliefoerster
Jan 3 at 10:00
I'm very sorry for being so stupid but can you give the explicit example? I don't get how to do it ...
– nataliefoerster
Jan 3 at 10:00
|
show 11 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018378%2fhow-to-connect-with-redis-sever-in-python-while-having-a-key-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown