<CoreOS, cloud-config> How to "Wait" until the private Docker registry is ready?

I am trying Docker on CoreOS on EC2.

What I want to do:

  • Launch Docker Private Registry Container
  • Launching other containers after pulling an image from the private registry

Initial configuration

My cloud-config.ymllooks like this:

#cloud-config
coreos:
  units:
    - name: etcd2.service
      command: start

    - name: fleet.service
      command: start

    - name: docker.service
      command: start
      drop-ins:
        - name: 50-insecure-registry.conf
          content: |
            [Service]
            Environment=DOCKER_OPTS='--insecure-registry="localhost:5000"'

    - name: private-docker-registry.service
      command: start
      runtime: true
      content: |
       [Unit]
       Description=Docker Private Registry
       After=docker.service
       Requires=docker.service
       Requires=network-online.target
       After=network-online.target

       [Service]
       ExecStartPre=/usr/bin/docker pull registry:latest
       ExecStart=/usr/bin/docker run --name private-docker-registry --privileged -e SETTINGS_FLAVOR=s3 -e AWS_BUCKET=bucket -e AWS_KEY=awskey -e AWS_SECRET=awssecret -e SEARCH_BACKEND=sqlalchemy -p 5000:5000 registry:latest

    - name: myservice.service
      command: start
      runtime: true
      content: |
       [Unit]
       Description=My Service
       After=private-docker-registry.service
       Requires=private-docker-registry.service
       Requires=network-online.target
       After=network-online.target

       [Service]
       ExecStartPre=/usr/bin/docker pull localhost:5000/myservice:latest
       ExecStart=/usr/bin/docker run --name myservice localhost:5000/myservice:latest

myservice.service is not working

The problem is here:

  • myservice.service fails even though the closed registry container has started successfully

When I log in to the machine, it shows the following message.

Failed Units: 1
  myservice.service

The command journalctl -u private-docker-registry.serviceshows this:

Jul 24 07:30:25 docker[830]: [2015-07-24 07:30:25 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)

The command journalctl -u myservice.serviceshows the following log.

Jul 24 07:30:25 systemd[1]: Starting My Service...
Jul 24 07:30:25 docker[836]: time="2015-07-24T07:30:25Z" level=fatal msg="Error response from daemon: v1 ping attempt failed with error: Get http://localhost:5000/v1/_ping: dial tcp 127.0.0.1:5000: connection refused"
Jul 24 07:30:25 systemd[1]: myservice.service: Control process exited, code=exited status=1
Jul 24 07:30:25 systemd[1]: Failed to start My Service.
Jul 24 07:30:25 systemd[1]: myservice.service: Unit entered failed state.
Jul 24 07:30:25 systemd[1]: myservice.service: Failed with result 'exit-code'.

However, I can start the myservice container manually (after a few minutes).

docker run --name myservice localhost:5000/myservice:latest

My suggestion:

  • myservice , myservice.service myservice , .

, wait-for-registry.service, 2 .

#cloud-config
coreos:
  units:
    - name: etcd2.service
      command: start

    - name: fleet.service
      command: start

    - name: docker.service
      command: start
      drop-ins:
        - name: 50-insecure-registry.conf
          content: |
            [Service]
            Environment=DOCKER_OPTS='--insecure-registry="localhost:5000"'

    - name: private-docker-registry.service
      command: start
      runtime: true
      content: |
       [Unit]
       Description=Docker Private Registry
       After=docker.service
       Requires=docker.service
       Requires=network-online.target
       After=network-online.target

       [Service]
       ExecStartPre=/usr/bin/docker pull registry:latest
       ExecStart=/usr/bin/docker run --name private-docker-registry --privileged -e SETTINGS_FLAVOR=s3 -e AWS_BUCKET=bucket -e AWS_KEY=awskey -e AWS_SECRET=awssecret -e SEARCH_BACKEND=sqlalchemy -p 5000:5000 registry:latest

    - name: wait-for-registry.service
      command: start
      runtime: true
      content: |
       [Unit]
       Description=Wait Until Private Registry is Ready
       After=private-docker-registry.service
       Requires=private-docker-registry.service

       [Service]
       ExecStart=/usr/bin/sleep 120

    - name: myservice.service
      command: start
      runtime: true
      content: |
       [Unit]
       Description=My Service
       After=wait-for-registry.service
       After=private-docker-registry.service
       Requires=private-docker-registry.service
       Requires=network-online.target
       After=network-online.target

       [Service]
       ExecStartPre=/usr/bin/docker pull localhost:5000/myservice:latest
       ExecStart=/usr/bin/docker run --name myservice localhost:5000/myservice:latest

.

journalctl -u private-docker-registry.service :

Jul 24 08:23:38 docker[838]: [2015-07-24 08:23:38 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)

journalctl -u wait-for-registry.service :

Jul 24 08:23:37 systemd[1]: Started Wait Until Private Registry is Ready.
Jul 24 08:23:37 systemd[1]: Starting Wait Until Private Registry is Ready...

journalctl -u myservice.service :

Jul 24 08:23:37 systemd[1]: Starting My Service...
Jul 24 08:23:37 docker[847]: time="2015-07-24T08:23:37Z" level=fatal msg="Error response from daemon: v1 ping attempt failed with error: Get http://localhost:5000/v1/_ping: dial tcp 127.0.0.1
Jul 24 08:23:37 systemd[1]: myservice.service: Control process exited, code=exited status=1
Jul 24 08:23:37 systemd[1]: Failed to start My Service.
Jul 24 08:23:37 systemd[1]: myservice.service: Unit entered failed state.
Jul 24 08:23:37 systemd[1]: myservice.service: Failed with result 'exit-code'.

, sleep .

, ?

!

:)

+4
1

systemd : -)

, . , , , .

-, , :

- name: wait-for-registry.service
  command: start
  runtime: true
  content: |
   [Unit]
   Description=Wait Until Private Registry is Ready
   After=private-docker-registry.service
   Requires=private-docker-registry.service

   [Service]
   ExecStart=/usr/bin/sleep 120
   RemainAfterExit=true
   Type=oneshot

, /usr/bin/sleep 120. , ( myservice.service). , , . , , - .

. , "", . , , 5000 , ? , :

ExecStart=/usr/bin/bash /opt/bin/waiter.sh

, -config:

write_files:
  - path: /opt/bin/waiter.sh
    permissions: 0755
    owner: root
    content: |
      #! /usr/bin/bash
      until curl -s http://127.0.0.1:5000/; do echo waiting waiter.sh; sleep 2; done

- . , - , .

-g

+4

Source: https://habr.com/ru/post/1599466/


All Articles