Docker Community Forums

Share and learn in the Docker community.

Docker-compose: create an image before a container


(Edoardo) #1

Hi all,

I am trying to wrap my head round docker-compose and the way he orders his jobs.

My question is: Why does docker-compose try and create the container before the image?

I was following this great tutorial but something bothered me, he creates three images and each image has a container. From my very limited understanding, I would have thought that you should have one image and three containers pointing to it. Now I’m sure he’s got a good reason for it, I just don’t get it.

weba:
build: .
expose:
- 80

webb:
build: .
expose:
- 80

webc:
build: .
expose:
- 80

haproxy:
image: hypriot/rpi-haproxy
volumes:

  • haproxy:/haproxy-override
    links:
  • weba
  • webb
  • webc
    ports:
  • “80:80”
  • “70:70”

expose:

  • “80”
  • “70”

So I tried the following:

weba:
build: .
hostname: weba
expose:
- 80
webb:
image: trial_weba
hostname: webb
expose:
- 80

webc:
image: trial_weba
hostname: webc
expose:
- 80

haproxy:
image: haproxy
volumes:

  • haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
    links:
  • trial_weba_1
  • trial_webb_1
  • trial_webc_1
    ports:
  • “80:80”
  • “70:70”

expose:

  • “80”
  • “70”

My problem now is that now docker-compose will try and build the containers before the iamge and this regardless of where in the file I place the image build!

I finally got it working by first creating the image in a separate yml file

image.yml

weba:
build: .
hostname: weba
expose:
- 80

container.yml

webb:
image: trial_weba
hostname: webb
expose:
- 80

webc:
image: trial_weba
hostname: webc
expose:
- 80

haproxy:
image: haproxy
volumes:

  • haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
    external_links:
  • trial_weba_1
  • trial_webb_1
  • trial_webc_1
    ports:
  • “80:80”
  • “70:70”

expose:

  • “80”
  • “70”

I had to change links with external_links and run two scripts but that did it. Why does docker-compose not first create the image? It’s first in line and it seems logical to create any image first as it might be needed by a future container.

In the end, I am just trying to go from this setup where we have an image per container

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
trial_webc latest ca1a44e8a63b 14 seconds ago 641 MB
trial_webb latest ca1a44e8a63b 14 seconds ago 641 MB
trial_weba latest ca1a44e8a63b 14 seconds ago 641 MB
haproxy latest 46ae28c75fd0 3 days ago 97.88 MB
node latest 31eee15d25f8 3 days ago 638 MB

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c83f15463186 haproxy "haproxy -f /usr/loc 12 seconds ago Up 11 seconds 0.0.0.0:70->70/tcp, 0.0.0.0:80->80/tcp trial_haproxy_1
0a91b1a2ffe1 trial_weba “node index.js” 12 seconds ago Up 11 seconds 80/tcp trial_weba_1
8ec68bc15cd6 trial_webc “node index.js” 12 seconds ago Up 12 seconds 80/tcp trial_webc_1
cd30b025ae48 trial_webb “node index.js” 13 seconds ago Up 12 seconds 80/tcp trial_webb_1

to this setup, where we have an image for all the web containers

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
trial_weba latest 9c1d88958da8 19 minutes ago 641 MB
haproxy latest 46ae28c75fd0 3 days ago 97.88 MB
node latest 31eee15d25f8 3 days ago 638 MB

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b94712684730 haproxy "haproxy -f /usr/loc 19 minutes ago Up 19 minutes 0.0.0.0:70->70/tcp, 0.0.0.0:80->80/tcp trial_haproxy_1
bcc240337f26 trial_weba “node index.js” 19 minutes ago Up 19 minutes 80/tcp trial_webc_1
137700ff2df0 trial_weba “node index.js” 19 minutes ago Up 19 minutes 80/tcp trial_webb_1
5ed6f0b763ee trial_weba “node index.js” 19 minutes ago Up 19 minutes 80/tcp trial_weba_1

If it’s in the manual, feel free to slap me with it and if what I am trying to do is stupid, here is my other cheek.

Thanks.
-Ed