4

I am running an EC2 instance with a startup script that is called on reboot. This startup script checks that the docker daemon is running before then starting the container, but fails with the error: Post http:///var/run/docker.sock/v1.13/containers/create: dial unix /var/run/docker.sock: no such file or directory

Startup Script

# Make sure the docker daemon has started
sudo /usr/sbin/service docker start

# start the container
sudo /usr/bin/docker run -d 91b5261e2dc0

Please note that his is on an ec2 instance where "sudo" does not require password entry.

Crontab entry:

@reboot /bin/bash /home/ubuntu/start-container.sh 2> /home/ubuntu/cron_errors.log

Errors:

start: Job is already running: docker
2014/08/01 09:45:48 Post http:///var/run/docker.sock/v1.13/containers/create: dial unix /var/run/docker.sock: no such file or directory

Whenever I manually run the startup script, it works perfectly which makes it seem like an Environment variable/PATH issue to me. Googling around found information about not setting the DOCKER_HOST, but the startup script works fine even when DOCKER_HOST is still not set.

What do I need to change or define to ensure the container starts correctly on startup?

Versioning Info

OS: Ubuntu 14.04 Hardware Virtualized.

Docker version:

Client version: 1.1.2
Client API version: 1.13
Go version (client): go1.2.1
Git commit (client): d84a070
Server version: 1.1.2
Server API version: 1.13
Go version (server): go1.2.1
Git commit (server): d84a070

uname -a output

Linux ip-10-76-167-92 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Programster
  • 12,242
  • 9
  • 49
  • 55
  • Try adding a `wait` command (or a &&) after your first command in `start-container.sh`, failing that - are you sure your cron job is running as root? Try `@reboot root /bin/bash start-container.sh ...` – Chris McKinnel Aug 01 '14 at 10:37
  • Adding a sleep 10 has "fixed" the issue temporarily, but I would rather not have to rely on a race condition. I am not running as root, but the default "ubuntu" user, hence the calls to sudo – Programster Aug 01 '14 at 11:46
  • That's fine, at least you know now that the docker daemon isn't starting in time for you to start your container. What about using `&&` at the end of the start command? You're effectively saying "When you're done starting the docker daemon, start the container". Basically the same as wait, though. – Chris McKinnel Aug 01 '14 at 12:15
  • @Chris McKinnel are you saying that my call to `sudo /usr/sbin/service docker start` is non-blocking and it will immediately move onto the next line: `sudo /usr/bin/docker run -d 91b5261e2dc0` before the daemon has finished starting? – Programster Aug 01 '14 at 12:49
  • 1
    Yeah so if you take a look inside `/etc/init.d/docker`, you'll see that calling `start` uses the --background option to `start-stop-daemon` which means it's still doing stuff when it returns. – Chris McKinnel Aug 01 '14 at 14:41
  • I'm afraid commands like `sudo /usr/sbin/service "docker" "start" && /usr/bin/docker run -d [IMAGE ID]` appear to never run the second half. I am guessing this is because && only works if the previous command succeeds and if the docker service is already running is not considered successful? – Programster Aug 06 '14 at 09:30

3 Answers3

4

The "solution" ended up being to put in a sleep or a wait after the call to starting the docker daemon. E.g

# Make sure the docker daemon has started
sudo /usr/sbin/service docker start

# Wait for the docker daemon to finish starting
sleep 10

# start the container
sudo /usr/bin/docker run -d 91b5261e2dc0

This appears to be because as Chris McKinnel stated:

...if you take a look inside /etc/init.d/docker, you'll see that calling start uses the --background option to start-stop-daemon which means it's still doing stuff when it returns.

Why not just use && ?

I tried using && to queue the starting of the container after the call to starting the docker daemon, but && will only run the next command if the former was successful which isn't always the case (e.g. if the docker daemon is already running). [ ref ]

Community
  • 1
  • 1
Programster
  • 12,242
  • 9
  • 49
  • 55
3

Another solution could be to run this container manually with --restart=always option:

docker run --restart=always -d 91b5261e2dc0    

docker will run start all such containers after docker start and if container will stop without docker stop command.

ISanych
  • 21,590
  • 4
  • 32
  • 52
1

You can wait for the docker socket to become available before running your container:

# Start the docker daemon
sudo /usr/sbin/service docker start

# Wait for docker socket to become available
while [ ! -e /var/run/docker.sock ]; do sleep 1; done

# start the container
sudo /usr/bin/docker run -d 91b5261e2dc0
Lee Netherton
  • 21,347
  • 12
  • 68
  • 102