简体   繁体   English

运行Docker容器时如何运行CMD并打开Bash?

[英]How to run a CMD and open Bash when running a docker container?

I am having a rough time trying to figure this out. 我正在艰难的时期试图解决这个问题。

So I have a dockerfile based on an Ubuntu image. 所以我有一个基于Ubuntu映像的dockerfile。 At the end of my dockerfile I have: 在我的dockerfile末尾,我有:

CMD django-admin startproject $PROJECTNAME

I was told in a previous post that the base Ubuntu image has a CMD to run /bin/bash so my command is actually overwriting this (not sure if this is relevant or not). 在上一篇文章中曾告诉我,基本的Ubuntu映像具有运行/ bin / bash的CMD,因此我的命令实际上覆盖了此命令(不确定是否相关)。

The problem I'm encountering is if I run: 我遇到的问题是如果我运行:

docker run -i -t <containerid> 

Nothing happens.. docker ps shows no containers are running 什么也没发生.. docker ps显示没有容器在运行

But if I run: 但是如果我运行:

docker run -i -t <containerid> /bin/bash

The container starts running, I am in the shell, and docker ps shows that this container is running. 容器开始运行,我在外壳中,并且docker ps显示该容器正在运行。 Everything works as expected, but my django project is not there and my understanding is that running /bin/bash overrides the CMD in the dockerfile, which means django-admin startproject never gets run. 一切都按预期工作,但我的django项目不存在,我的理解是,运行/ bin / bash会覆盖dockerfile中的CMD,这意味着django-admin startproject永远不会运行。

From inside the container, I can run django-admin startproject $projectname and it creates the project with no issues, which tells me django and all its dependencies are installed, and my environment variables are being registered. 从容器内部,我可以运行django-admin startproject $ projectname,它创建的项目没有问题,这告诉我django及其所有依赖项都已安装,并且我的环境变量正在注册。

However, I still suspect that there maybe is an issue with my CMD in my dockerfile and I don't know where to go from here. 但是,我仍然怀疑我的dockerfile中的CMD可能有问题,我不知道从这里去哪里。

With my provided dockerfile, if I do not run the container with /bin/bash, the container will not run. 使用提供的dockerfile,如果我不使用/ bin / bash运行容器,则该容器将不会运行。

Output of docker images: 泊坞窗图像的输出:

REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
<none>              <none>              cbec557d6362        About a minute ago   579 MB
<none>              <none>              6919c19b159a        16 hours ago 

docker run: 泊坞窗运行:

docker run -i -t cbec557d6362

I commented on the original issue but for whatever reason there is a new one now. 我对原始问题发表了评论,但无论出于何种原因,现在都有一个新问题。 When you run /bin/bash that becomes the command run in that container, a bash shell. 当您运行/bin/bash 变成该容器中运行的命令,是bash shell。 You can do whatever you want in the container (like create a Django project) but when you exit that shell the container still stop (as containers stop when the PID 1 process, in this case, /bin/bash ) exits. 您可以在容器中做任何想做的事情(例如创建Django项目),但是当退出该外壳时,容器仍会停止(因为PID 1进程(在本例中为/bin/bash )退出时容器会停止)。

My suspicion (I don't know Django super well) is that the command django-admin startproject $PROJECTNAME is being run, completes successfully then exits. 我怀疑(我不太了解Django)是django-admin startproject $PROJECTNAME命令正在运行, 成功完成然后退出。 It's PID 1, so the container stops. 它是PID 1,因此容器停止了。 This is why docker ps shows nothing. 这就是为什么docker ps什么都不显示的原因。

My suggestion would be to instead use RUN django-admin startproject $PROJECTNAME in your Dockerfile followed by CMD /bin/bash . 我的建议是改为在Dockerfile中使用RUN django-admin startproject $PROJECTNAME ,然后使用CMD /bin/bash

Build the container then run it and you should be in a bash shell, check if your project is created. 构建容器然后运行它,您应该在bash shell中,检查是否已创建项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM