Ehud Grand
Ehud Grand

Reputation: 3693

run docker-compose in EC2 user-data

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.

Upvotes: 1

Views: 4570

Answers (1)

Marcin
Marcin

Reputation: 238189

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.

Upvotes: 2

Related Questions