简体   繁体   English

Docker-从docker-compose运行映像

[英]Docker - run image from docker-compose

I have set up a docker-compose.yml file that runs a web service along with postgres. 我已经设置了一个docker-compose.yml文件,该文件与postgres一起运行Web服务。

It works nicely when I run it with docker-compose up . 当我使用docker-compose up运行它时,它很好用。

docker-compose.yml: docker-compose.yml:

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

Dockerfile: Dockerfile:

FROM python:3
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

CMD ["python", "manage.py", "runserver"]

Is there any way to construct an image out of the services? 有什么方法可以从服务中构建映像吗?

I tried it with docker-compose build, but running the created image simply freezes the terminal. 我用docker-compose build尝试了一下,但是运行创建的映像只会冻结终端。

Thanks! 谢谢!

docker-compose is a container orchestration tool , albeit a simple one , and not a bundler of multiple images and preferences into one. docker-compose是一个容器编排工具 ,尽管是一个简单的工具 ,而不是将多个图像和首选项捆绑在一起的工具。 In fact, such a thing does not even exists . 实际上, 这样的事情甚至不存在

What happens when you run docker-compose up is that it effectively runs docker-compose build for those images that need to be built, web in your example, and then effectively replaces the build: . 当您运行docker-compose up时发生的事情是,它为需要docker-compose build的映像有效运行docker-compose build ,在您的示例中为web ,然后有效地替换了build: . :。 with image: web and executes the configuration as defined by the compose file. 使用image: web并执行由compose文件定义的配置。

So if you were to run docker-compose build manually and wanted to run the same configuration you have in the compose file manually, you would need to something along the lines of (in order) 因此,如果您要手动运行docker-compose build ,并且想要手动运行compose文件中的相同配置,则需要遵循(按顺序)进行操作

  • run docker-compose build or docker build -t web . 运行docker-compose builddocker build -t web . to build the web image 建立web图像
  • run docker run --name db postgres 运行docker run --name db postgres
  • run docker run --name web -v .:/code -p 8000:8000 web python manage.py runserver 0.0.0.0:8000 运行docker run --name web -v .:/code -p 8000:8000 web python manage.py runserver 0.0.0.0:8000

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

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