Is there a way to command Docker to start containers to start on next boot instead of now?

Viewed 268

I am using Packer to build an EC2 AMI containing Docker images. I want a few services (restart policy set to unless-stopped to be downloaded and ready to run on first boot without actually running during build time.

At the moment I docker-compose up -d, wait an arbitrary amount of time, then finish the packer build (which probably ungracefully stops the running containers).

What I am planning is to docker-compose pull && docker-compose build and create some kind of init script that issues the docker compose run command.

Is there a better way to do this?

1 Answers

You can just do docker pull and add and enable a systemd unit file for each docker container. Something like this:

[Unit]
Description=Redis Container    
After=docker.service 
Requires=docker.service
[Service] 
TimeoutStartSec=0
Restart=always 
ExecStartPre=-/usr/bin/docker stop %n 
ExecStartPre=-/usr/bin/docker rm %n 
ExecStartPre=/usr/bin/docker pull redis 
ExecStart=/usr/bin/docker run --rm --name %n redis
[Install] 
WantedBy=multi-user.target

Read more in this blog post.

Related