Docker – How to execute some commands before the user scripts in cloud-init

amazon ec2cloud-initdocker

I'm trying to run some scripts and config using a multipart config.

Content-Type: multipart/mixed; boundary="MIMEBOUNDARY"
MIME-Version: 1.0
--MIMEBOUNDARY
Content-Disposition: attachment; filename="cloud-config.cfg"
Content-Transfer-Encoding: 7bit
Content-Type: text/cloud-config
Mime-Version: 1.0

package_upgrade: true
packages:
  - jq
  - docker

runcmd:
  - service docker start

--MIMEBOUNDARY
Content-Disposition: attachment; filename="00-rancher_server_install"
Content-Transfer-Encoding: 7bit
Content-Type: text/x-shellscript
Mime-Version: 1.0

#!/bin/bash

# Pull required images
docker pull rancher/server:v1.0.2

--MIMEBOUNDARY--

The problem is the order in which the scripts are executed…
– First yum install scripts
– Then the bash scripts
– Then the runcmd scripts

then is the docker service hasn't been started when the user scripts commands are executed

I'm using an Amazon Linux AMI on EC2, which is CentOS…

This is a the cloud init log

Cloud-init v. 0.7.6 running 'modules:config' at Tue, 21 Jun 2016 01:30:44 +0000. Up 25.32 seconds.
Loaded plugins: priorities, update-motd, upgrade-helper
Metadata Cache Created
Loaded plugins: priorities, update-motd, upgrade-helper
No packages needed for security; 26 packages available
Resolving Dependencies
Loaded plugins: priorities, update-motd, upgrade-helper
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 0:1.11.1-1.2.amzn1 will be installed
--> Processing Dependency: xfsprogs for package: docker-1.11.1-1.2.amzn1.x86_64
---> Package jq.x86_64 0:1.5-1.2.amzn1 will be installed
--> Processing Dependency: jq-libs(x86-64) = 1.5-1.2.amzn1 for package: jq-1.5-1.2.amzn1.x86_64
--> Processing Dependency: oniguruma for package: jq-1.5-1.2.amzn1.x86_64
--> Processing Dependency: libjq.so.1()(64bit) for package: jq-1.5-1.2.amzn1.x86_64
--> Processing Dependency: libonig.so.2()(64bit) for package: jq-1.5-1.2.amzn1.x86_64
--> Running transaction check
---> Package jq-libs.x86_64 0:1.5-1.2.amzn1 will be installed
---> Package oniguruma.x86_64 0:5.9.1-3.1.2.amzn1 will be installed
---> Package xfsprogs.x86_64 0:3.2.2-2.20.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch         Version                  Repository          Size
================================================================================
Installing:
 docker          x86_64       1.11.1-1.2.amzn1         amzn-updates        17 M
 jq              x86_64       1.5-1.2.amzn1            amzn-updates        44 k
Installing for dependencies:
 jq-libs         x86_64       1.5-1.2.amzn1            amzn-updates       121 k
 oniguruma       x86_64       5.9.1-3.1.2.amzn1        amzn-main          149 k
 xfsprogs        x86_64       3.2.2-2.20.amzn1         amzn-main          1.7 M

Transaction Summary
================================================================================
Install  2 Packages (+3 Dependent packages)

Total download size: 19 M
Installed size: 60 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                               11 MB/s |  19 MB  00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : oniguruma-5.9.1-3.1.2.amzn1.x86_64                           1/5
  Installing : jq-libs-1.5-1.2.amzn1.x86_64                                 2/5
  Installing : xfsprogs-3.2.2-2.20.amzn1.x86_64                             3/5
  Installing : docker-1.11.1-1.2.amzn1.x86_64                               4/5
  Installing : jq-1.5-1.2.amzn1.x86_64                                      5/5
  Verifying  : docker-1.11.1-1.2.amzn1.x86_64                               1/5
  Verifying  : oniguruma-5.9.1-3.1.2.amzn1.x86_64                           2/5
  Verifying  : jq-1.5-1.2.amzn1.x86_64                                      3/5
  Verifying  : jq-libs-1.5-1.2.amzn1.x86_64                                 4/5
  Verifying  : xfsprogs-3.2.2-2.20.amzn1.x86_64                             5/5

Installed:
  docker.x86_64 0:1.11.1-1.2.amzn1           jq.x86_64 0:1.5-1.2.amzn1

Dependency Installed:
  jq-libs.x86_64 0:1.5-1.2.amzn1        oniguruma.x86_64 0:5.9.1-3.1.2.amzn1
  xfsprogs.x86_64 0:3.2.2-2.20.amzn1

Complete!
Cloud-init v. 0.7.6 running 'modules:final' at Tue, 21 Jun 2016 01:31:55 +0000. Up 96.28 seconds.
Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon. Is the docker daemon running on this host?). Using system default: https://index.docker.io/v1/
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
Jun 21 01:31:55 cloud-init[2485]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/00-rancher_server_install [1]
Starting cgconfig service: [  OK  ]
Starting docker:    .[  OK  ]
Jun 21 01:31:56 cloud-init[2485]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Jun 21 01:31:56 cloud-init[2485]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Cloud-init v. 0.7.6 finished at Tue, 21 Jun 2016 01:31:56 +0000. Datasource DataSourceEc2.  Up 97.56 seconds

Best Answer

@smoser from the irc #cloud-init helped me

look in /var/lib/cloud/instance/scripts/ you'll see one file named runcmd and the other files that are in your multipart are named by their filename.

The scripts in that directory are run in C locale sorted order (as if by runparts)

So if you change the name of the script you've added from filename="00-rancher_server_install" to filename="zz-rancher_server_install" then yo'ull be good.

Related Topic