1

I have an EC2 linux2 instance containing dokcer compose. I want the instance to start the docker service, navigate to the correct folder, and then run docker-compose up -d, three simple lines, every time the instance start:

sudo service docker start
cd APP
docker-compose up -d

As shown here, entering this to the user data when the instance is stopped should work:

#!/bin/bash
sudo service docker start
cd APP
docker-compose up -d

But it does nothing. The aws doc has also this script:

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo "Hello World" >> /tmp/testfile.txt  <<<<<<<<------- ACTUAL USER SCRIPT
--//

When I paste my three lines instead of the hello world line, only the first one, start the docker service, works.

I am new to Linux, so it is probably something small that I'm missing.

Thanks

EDIT Thanks guys for your help, the issue is really the path to dir APP.

Ehud Grand
  • 3,501
  • 4
  • 33
  • 52
  • 2
    From your host go to `/var/log/cloud-init-output. log`. This will contain an errors outputted in your user-data – Chris Williams Jul 29 '20 at 12:31
  • 3
    The user-data script runs as `root`, not as `ec2-user` so the shell paths, and home directories will be different. Because of that you should really use absolute paths in user-data scripts. For example `cd APP` may need to be `cd /home/ec2-user/APP`? – Mark B Jul 29 '20 at 12:53

1 Answers1

2

entering this to the user data when the instance is stopped should work

The first attempt will sadly not work. This is because UserData executes only when a new instance is launched.

When I paste my three lines instead of the hello world line, only the first one

The second attempt fails because this script runs as root in / folder (root of the filesystem). So unless your APP is in /APP, this will not work.

Marcin
  • 215,873
  • 14
  • 235
  • 294