Reuse image built by one service in another service

余生颓废 提交于 2020-01-22 14:08:27

问题


Let's say I have this project structure:

proj/
├── docker
│   └── myservice
│       └── Dockerfile
└── docker-compose.yml

And this is my docker-compose.yml:

version: '3'

services:

  master:
      build: docker/myservice

  slave:
      image: proj_master
      depends_on: master

I want the master service to create an image, which will be used by the master and the slave services (with different parameters, not shown here). By trial and error I have found out that the slave must refer to the image proj_master.

  • Where is this documented?
  • Why do I need to make a reference to the proj? Usually a docker composer file is agnostic related to where it is located ...

回答1:


Docker Compose builds your image with the name proj_master, beacuse you did not specify an image name under your master service in the Compose file.

You have a build section, so Docker Compose will build the image and give it a name based on your <directory_name_where_you_run_compose>_<service_name>:latest. I did not find this in the documentation, but tried with one of my projects, and it's in line with what you experienced.

You can fix your project by specifying the image name in your Compose file and using the same image for both services:

version: '3'


services:

  master:
      build: docker/myservice
      image: username/masterimage:version


  slave:
      image: username/masterimage:version
      depends_on: master



回答2:


If you'd use the following syntax in docker-compose.yaml the image will be built once by master, then used by slave:

version: '3'

services:
  master:
    build: docker/myservice
    image: master-image:3

  slave:
    image: master-image:3



回答3:


This feature is not that broadly discussed but it is awesome. You can just link your slaves to your master.

dg: &dg
  image: python:3.7.6-alpine
   command: ....  

dg_socketshark:
  <<: *dg
  restart: on-failure
  ports:
    - 9011:9011
  command: /start-dg_socketshark.sh 

dg_run_telegram:
  <<: *dg
  restart: on-failure
  command: /start-dg_run_telegram.sh

Now the all share the same source but use different command o start.



来源:https://stackoverflow.com/questions/50019948/reuse-image-built-by-one-service-in-another-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!