Name Image and create Container with Python Docker SDK
I currently have the need to create an image from a Dockerfile with the Python Docker SDK, and then run a container based on that image. I know how to run a container based on an image I manually create, however, when I try to use the Python Docker SDK to create the image, I can't figure out how to pass a name to the image, which I can then reference when I run a container based on the image. Looking at the docs here I don't see a parameter for name.
When I do create the image, based on the Dockerfile, using the SDK, it creates an unnamed image, however has an ID.
import docker
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)
print image
except RuntimeError as e:
print e
if __name__ == '__main__':
main()
This is the image it creates, it doesn't have a name: <Image: ''>
It has an ID, though: 1e3bc3e1eb43
The Dockerfile I have is super-simple, right now:
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
I'd like to be able to create an image, with a name I choose, and then run a container based on that image using the Python Docker SDK.
python docker sdk
add a comment |
I currently have the need to create an image from a Dockerfile with the Python Docker SDK, and then run a container based on that image. I know how to run a container based on an image I manually create, however, when I try to use the Python Docker SDK to create the image, I can't figure out how to pass a name to the image, which I can then reference when I run a container based on the image. Looking at the docs here I don't see a parameter for name.
When I do create the image, based on the Dockerfile, using the SDK, it creates an unnamed image, however has an ID.
import docker
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)
print image
except RuntimeError as e:
print e
if __name__ == '__main__':
main()
This is the image it creates, it doesn't have a name: <Image: ''>
It has an ID, though: 1e3bc3e1eb43
The Dockerfile I have is super-simple, right now:
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
I'd like to be able to create an image, with a name I choose, and then run a container based on that image using the Python Docker SDK.
python docker sdk
add a comment |
I currently have the need to create an image from a Dockerfile with the Python Docker SDK, and then run a container based on that image. I know how to run a container based on an image I manually create, however, when I try to use the Python Docker SDK to create the image, I can't figure out how to pass a name to the image, which I can then reference when I run a container based on the image. Looking at the docs here I don't see a parameter for name.
When I do create the image, based on the Dockerfile, using the SDK, it creates an unnamed image, however has an ID.
import docker
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)
print image
except RuntimeError as e:
print e
if __name__ == '__main__':
main()
This is the image it creates, it doesn't have a name: <Image: ''>
It has an ID, though: 1e3bc3e1eb43
The Dockerfile I have is super-simple, right now:
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
I'd like to be able to create an image, with a name I choose, and then run a container based on that image using the Python Docker SDK.
python docker sdk
I currently have the need to create an image from a Dockerfile with the Python Docker SDK, and then run a container based on that image. I know how to run a container based on an image I manually create, however, when I try to use the Python Docker SDK to create the image, I can't figure out how to pass a name to the image, which I can then reference when I run a container based on the image. Looking at the docs here I don't see a parameter for name.
When I do create the image, based on the Dockerfile, using the SDK, it creates an unnamed image, however has an ID.
import docker
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)
print image
except RuntimeError as e:
print e
if __name__ == '__main__':
main()
This is the image it creates, it doesn't have a name: <Image: ''>
It has an ID, though: 1e3bc3e1eb43
The Dockerfile I have is super-simple, right now:
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
I'd like to be able to create an image, with a name I choose, and then run a container based on that image using the Python Docker SDK.
python docker sdk
python docker sdk
edited Nov 12 '18 at 14:06
miken
asked Nov 12 '18 at 13:50
mikenmiken
307
307
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I was able to pull out the ID from the image, and then pass that in when creating the container.
import docker
import re
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)[0]
image_id = re.sub(r'(sha256:)', '', image.short_id)
client.containers.run(image_id, detach=True, ports='8080/tcp': 8080)
except RuntimeError as e:
print e
if __name__ == '__main__':
main()
add a comment |
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%2f53263597%2fname-image-and-create-container-with-python-docker-sdk%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
I was able to pull out the ID from the image, and then pass that in when creating the container.
import docker
import re
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)[0]
image_id = re.sub(r'(sha256:)', '', image.short_id)
client.containers.run(image_id, detach=True, ports='8080/tcp': 8080)
except RuntimeError as e:
print e
if __name__ == '__main__':
main()
add a comment |
I was able to pull out the ID from the image, and then pass that in when creating the container.
import docker
import re
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)[0]
image_id = re.sub(r'(sha256:)', '', image.short_id)
client.containers.run(image_id, detach=True, ports='8080/tcp': 8080)
except RuntimeError as e:
print e
if __name__ == '__main__':
main()
add a comment |
I was able to pull out the ID from the image, and then pass that in when creating the container.
import docker
import re
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)[0]
image_id = re.sub(r'(sha256:)', '', image.short_id)
client.containers.run(image_id, detach=True, ports='8080/tcp': 8080)
except RuntimeError as e:
print e
if __name__ == '__main__':
main()
I was able to pull out the ID from the image, and then pass that in when creating the container.
import docker
import re
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)[0]
image_id = re.sub(r'(sha256:)', '', image.short_id)
client.containers.run(image_id, detach=True, ports='8080/tcp': 8080)
except RuntimeError as e:
print e
if __name__ == '__main__':
main()
answered Nov 12 '18 at 14:57
mikenmiken
307
307
add a comment |
add a comment |
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%2f53263597%2fname-image-and-create-container-with-python-docker-sdk%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