[英]How do I run my docker container after loading it without running docker-compose up?
I have created a Dockerfile
and docker-compose.yml
file.我创建了一个
Dockerfile
和docker-compose.yml
文件。 I can run docker-compose up
on Windows 10 which builds and then runs my Django web application.我可以在 Windows 10 上运行
docker-compose up
它构建然后运行我的 Django Z2567A5EC9705EB7AC2C984033 应用程序。 I can then successfully browse to 127.0.0.1:7000
and exercise my web application.然后我可以成功浏览到
127.0.0.1:7000
并运行我的 web 应用程序。
My question is, once I exit, how do fire up my image w/o running docker-compose up
again?我的问题是,一旦我退出,如何在不运行
docker-compose up
启动我的图像?
When I type docker images
I see two images: python
and my_web_app
.当我输入
docker images
时,我看到两个图像: python
和my_web_app
。 I've tried我试过了
docker run --publish 7000:8000 -- detach my_web_app
but it exits immediately.但它立即退出。
My Dockerfile我的 Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN useradd user
USER user
My docker-compose.yml我的 docker-compose.yml
version: "3"
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "7000:8000"
In docker-compose.yml
file, you have written build: .
在
docker-compose.yml
文件中,您编写了build: .
, which is using the Dockerfile at path .
,它在 path 使用 Dockerfile
.
and creates an image named my_web_app.并创建一个名为 my_web_app 的图像。
You have 2 images because Python was used as a base image in the Dockerfile, so docker pulls that image first.您有 2 个图像,因为 Python 被用作 Dockerfile 中的基础图像,因此 docker 首先拉取该图像。 And the other one is created as the resultant image (which contains python inside it).
另一个是作为结果图像创建的(其中包含 python )。
Now when you run this command:现在,当您运行此命令时:
docker run --publish 7000:8000 -- detach my_web_app
It creates a container but will exit immediately because it has no command/process to run.它创建一个容器,但会立即退出,因为它没有要运行的命令/进程。
In docker-compose.yml
, there exists command: python manage.py runserver 0.0.0.0:8000
which runs this command inside the container.在
docker-compose.yml
中,存在command: python manage.py runserver 0.0.0.0:8000
在容器内运行此命令。
Two simple ways to run a new container are:运行新容器的两种简单方法是:
docker run --publish 7000:8000 -- detach my_web_app python manage.py runserver 0.0.0.0:8000
which runs the specified command inside the container.
它在容器内运行指定的命令。
- Or you can use
docker exec -ti my_web_app
and add the command to run after >this.或者您可以使用
docker exec -ti my_web_app
并在 >this 之后添加要运行的命令。
If you docker run my_web_app
, it exits immediately because the Dockerfile doesn't have a CMD
in it.如果您
docker run my_web_app
,它会立即退出,因为 Dockerfile 中没有CMD
。
As others have answered, you can run docker-compose up
to re-run the container.正如其他人回答的那样,您可以运行
docker-compose up
重新运行容器。 The docker-compose.yml
file contains a number of settings that are needed to start the container. docker-compose.yml
文件包含启动容器所需的许多设置。 Only the docker-compose
command reads this file;只有
docker-compose
命令读取这个文件; a plain docker run
doesn't.普通的
docker run
不会。
If you want to run this with a plain docker run
command, you should do a couple of things:如果你想用一个普通的
docker run
命令来运行它,你应该做几件事:
Remove the command:
line from the docker-compose.yml
;从
docker-compose.yml
中删除command:
行; add the same CMD
line to your Dockerfile
.将相同的
CMD
行添加到您的Dockerfile
。 This makes the image have a default command that gets run, instead of needing to specify it every time you run the command.这使得图像具有运行的默认命令,而不是每次运行命令时都需要指定它。
Remove the volumes:
from the docker-compose.yml
;删除
volumes:
从docker-compose.yml
; you already ADD
the application in your Dockerfile
, so you don't want to overwrite it with random content from your host.您已经在
Dockerfile
中ADD
了应用程序,因此您不想用主机中的随机内容覆盖它。
This would leave you with a simpler docker-compose.yml
file这将为您留下一个更简单的
docker-compose.yml
文件
version: "3"
services:
web:
build: .
image: my_web_app # (override the name Compose picks)
ports:
- "7000:8000"
Once you have this, you can translate this to a docker run
command一旦你有了这个,你可以把它翻译成
docker run
命令
docker run \
-d \
-p 7000:8000 \ # ports:
my_web_app # image:
If you had other options ( environment:
, volumes:
, ...) you would need to translate this into docker run
options.如果您有其他选项(
environment:
, volumes:
,...),您需要将其转换为docker run
选项。 If you need to communicate between containers, Compose creates a default
network for you and you would also need to recreate this.如果您需要在容器之间进行通信, Compose 会为您创建一个
default
网络,您还需要重新创建它。
That's how you run it.这就是你运行它的方式。
docker-compose up
Run docker-compose up -d
if you want to leave it running in the background.如果要让它在后台运行,请运行
docker-compose up -d
。
The Dockerfile
generates the image, which is then used by docker-compose
to build and run the service. Dockerfile
生成映像,然后docker-compose
使用该映像来构建和运行服务。
If you intend to run the service without running docker-compose
, you should define how to execute the container in Dockerfile
itself.如果您打算在不运行
docker-compose
的情况下运行服务,则应在Dockerfile
本身中定义如何执行容器。 Checkout cmd结帐cmd
CMD ["python", "manage.py", "runserver"]
You should probably move the command
from the docker-compose file to the Dockerfile and build the image again using either docker-compose
or docker build
.您可能应该将
command
从 docker-compose 文件移动到 Dockerfile 并使用docker-compose
或docker build
再次构建映像。
Once the image is available, just run using:图像可用后,只需使用以下命令运行:
> docker run <image_name> --detach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.