Upstart script to run container won't manage lifecycle

I have an upstart script (say, /etc/init/dtest.conf)

start on runlevel [2345]
stop on runlevel [!2345]
respawn
script
  DID=$(docker.io run -d ubuntu /bin/bash -c "echo Starting;sleep 20;echo Stopping")
  docker.io attach $DID
end script

When issuing start dtest, the upstart logs show the proper cycle of “Starting … Stopping” forever.

  • How can a container enumerate hosts available on the network?
  • Is it possible to run multiple docker/lxc/lxd container based on one base container?
  • ERROR: Error response from daemon: datastore for scope “global” is not initialized
  • Django shell mode in docker
  • Running docker command from docker container
  • Beginners guide for Mesos, Marathon, Docker Integration [closed]
  • However, if I issue a stop dtest, then it appears to exit properly, but the container will run for the remainder of the sleep time (as evidenced by running docker.io ps every second).

    Shouldn’t there be an easy way to run a docker image in a container with upstart and have its lifecycle be managed there?

    My ideal script would be something like this:

    start on runlevel [2345]
    stop on runlevel [!2345]
    respawn
    exec docker.io run -d ubuntu /bin/bash -c "echo Starting;sleep 20;echo Stopping"
    

    Environment: This is on AWS, using Ubuntu 14.04 in a T2.micro, with apt-get install -y docker.io being the only thing installed

  • Cross communication between docker containers in AWS Beanstalk
  • Docker: Jenkins container can't access internet on QNAP device
  • Running Kubernetes Example on CoreOS, Part 1 not work
  • Setting up a development environment with docker
  • Browserify cannot find module 'react-router-dom'
  • Docker engine startup with parameters on OS X Sierra
  • One Solution collect form web for “Upstart script to run container won't manage lifecycle”

    You should create a named container by running the following command:

    docker run --name dtest ubuntu /bin/bash -c "echo Starting;sleep 20;echo Stopping"
    

    Then create the following upstart script (pay attention to the -a flag) which will manage the lifecycle of this container as you expect

    start on runlevel [2345]
    stop on runlevel [!2345]
    respawn
    script
      /usr/bin/docker start -a dtest
    end script
    

    I would also suggest to add the -r flag to the main docker daemon execution script, so that docker will not automatically restart your containers when the host is restarted (instead this will be done by the upstart script)

    sudo sh -c "echo 'DOCKER_OPTS=\"-r=false\"' > /etc/default/docker"
    

    The process of configuring a Docker containers to work with process managers like upstart is described in a great detail here

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.