I have a EC2 server running Docker and I'd like to add the following to the User Data
so my private Dockerhub images will be pulled/run when the server starts up, like so:
#!/bin/bash
sudo docker run -p 3333:3333 -d --name Hello myusername/hello
But I'm unsure as to how to go about authenticating in order to gain access to the private repo myusername/hello
.
With Github you create and upload a deploy key, does Dockerhub offer a similar deploy key option?
UPDATE: Figured out an even better way that doesn't involve baking your creds into an image at all. See the following question for information that would be applicable to solving this problem as well: Is it secure to store EC2 User-Data shell scripts in a private S3 bucket?
This helps keep your secrets in the least number of places necessary at any given time.
Figured out a better way:
sudo docker login
on that machine.dockercfg
file in your home directory (e.g. /home/yourusername/.dockercfg
). Docker will use this file for all authentication from now on..dockercfg
file baked-in.User Data
of your machine image:#!/bin/bash sudo docker run -p 3333:3333 -d --name Hello yourusername/hello
Now when you launch an instance based on your machine image your sudo docker run
commands will succeed in pulling private repos provided the user you run the docker command under has a .dockercfg
file in their home directory.
Hope that helps anyone looking to figure this out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With