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?
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: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.
Update: See my other answer for a better method that doesn't require hard-coding your creds into your
User Data
scriptTo get an instance to pull a private Dockerhub repo upon launching you can authenticate simply by running
sudo docker login
in theUser Data
start-up script before yoursudo docker run
command, altogether like so:This requires hard-coding your Dockerhub creds into your
User Data
script, which is less than ideal, but it does work.I figured out a better way if you care to use ECS (which creates the EC2 instance/s for you) and don't want to utilize file storage in your solution. I mixed the solutions suggested by @AJB ('User Data' property and 'docker login' output), I'll describe the process:
docker login
on your machine (no sudo needed as far as I can tell), upon successful login runcat .docker/config.json
and you'll get something like:KEY
andEMAIL
asidecluster
,service
and atask definition
(with the image property set toyourusername/hello
), this will automatically generate the configuration for the EC2launch configuration
generated by ECScopy launch configuration
button and edit to taste (you can change the AMI although I'd recommend stay withAmazon Linux AMI
unless you have to, set a new descriptive name)A new Instance will shortly be launched by the Auto Scaling Group which now uses the new configuration which allows access to the private repository on your DockerHub account.