1

I'm trying to get my container to run on startup using upstart.

As the tutorial said - I created /etc/init/nginx_server.conf

description "Nginx docker"
author "Me"
start on filesystem and started docker
stop on runlevel [!2345]
respawn
script
  /usr/bin/docker run -d -p 80:80 test_server
end script

Running of latest Amazon linux (amzn-ami-hvm-2015.03.0.x86_64-gp2)

The container isn't running at all (running docker ps -a doesn't show it at all)

Boaz
  • 375
  • 1
  • 9
  • 16
  • Provide an actual image to the run target ? There's no `test_server` container in the docker registry. – Xavier Lucas May 10 '15 at 13:32
  • When I run it in command line (after the machine is up) it works. I'm not following what you are trying to say... – Boaz May 10 '15 at 13:49
  • [New to docker](https://docs.docker.com/reference/run/) ? What's the `test_server` image ? Where does this come from ? Nothing of this kind is [available in the docker registry](https://registry.hub.docker.com/search?q=test_server&searchfield=). Past your commands and their output when you do this without upstart. Past `sudo docker images` output and `sudo docker ps -a` too. – Xavier Lucas May 10 '15 at 14:08
  • It's my own local container of a locally built image – Boaz May 10 '15 at 14:54
  • Ok then... Next time some precision of this kind could bring some context to your question. – Xavier Lucas May 10 '15 at 14:56
  • 1
    Wow...Amazon Linux still uses upstart?!? – Michael Hampton May 10 '15 at 14:57
  • You have also an option of using `docker run --restart=always -d IMAGEID` to have the container autostart during reboot. – Daniel t. May 10 '15 at 16:33
  • @Danielt. - it worked! thanks (I think I can't accept this as an answer because I asked a different question - but YOU did help me!!! – Boaz May 10 '15 at 16:59
  • What name did you use for `IMAGEID`? – Daniel t. May 10 '15 at 17:12
  • Michael Hampton - Amazon Linux is based on EL6, which used upstart. – Jakov Sosic May 10 '15 at 19:38

2 Answers2

2

On Amazon Linux, the Docker daemon is started with an init.d script, not an Upstart script. In your Upstart script you're trying to signal the startup when the docker service starts (..started docker). This will only work if the docker init script is an Upstart script.

You can add initctl emit docker-started to the docker init.d script and then trigger your Upstart service config file with that event (ie. start on docker-started)

Brandon Wagner
  • 141
  • 1
  • 6
1

You cannot do a 'docker run' from the upstart, what you should do is create a container from a docker image, and then do 'docker start' in upstart. If you check /var/log/messages you will probably see something like:

init: test_server main process (6570) terminated with status 1
init: test_server respawning too fast, stopped

To work around it, try running:

docker run -d -p 80:80 test_server

You will get the container id as an output:

a64db8e1cca5

Then, put in your upstart file:

description "Nginx docker"
start on filesystem and started docker
stop on runlevel [!2345]
respawn
script
  /usr/bin/docker start -a a64db8e1cca5
end script
Jakov Sosic
  • 5,157
  • 3
  • 22
  • 33
  • You can give your container a name with `--name ` when using `docker run` and refer to it in other subcommands instead of using the container hash ID. – Xavier Lucas May 10 '15 at 14:21
  • 1
    Yes, you can, although I answered purposely this way, that it's clearly obvious what's container and what is an image. But still thanks for the comment. – Jakov Sosic May 10 '15 at 14:24
  • Why would you not be able to issue `docker run` from upstart since it's basically a create + start action ? I believe you simply need to remove the `-d` option. – Xavier Lucas May 10 '15 at 14:43
  • wait. When I do a docker run, it stays persistent in the FS of the host? – Boaz May 10 '15 at 14:53
  • 1
    @Boaz Yes unless you pass the `--rm` option. – Xavier Lucas May 10 '15 at 14:54
  • well. It didn't help. I had it run with --name as @XavierLucas suggested, and now I have it Exited(0) when running `docker ps -a`. Nothing seems docker related other than `IPv6: ADDRCONF(NETDEV_UP): docker0: link is not ready` (which I don't use) (The exited 0 is after a machine restart) – Boaz May 10 '15 at 15:06