Connect to docker daemon on an AWS Beanstalk EC2 instance

If you encounter some problem when you're setting up a new Beanstalk Docker app, the first thing to do is to download and take a look at the log files. But what if you still can't figure out what's wrong? Or what if you know what the problem is, and you want to just make some minor change to the container with docker exec? In that case you should SSH to one app's EC2 instances and use the docker command. The problem is that it will not work at first:

$ docker ps
Cannot connect to the Docker daemon. Is the docker daemon running on this host?  

It's actually quite easy to fix:

$ sudo gpasswd -a ec2-user docker

Then you just have to logout by terminating the SSH connection and then connect again.

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES  
13fb72871a59        2feee65ba700        "bash /"    35 minutes ago      Up 35 minutes       80/tcp              amazing_varahamihira  

You can now start a bash session on the container with command:

$ docker exec -it 13fb72871a59 bash
Add group automatically

By adding the following to a .config file in your .ebextensions folder of the source bundle, you will add ec2-user to docker group automatically:

    command: gpasswd -a ec2-user docker
    test: groups ec2-user | grep -qv docker

I hope you find this useful when setting up and troubleshooting your Beanstalk app.