Docker Primer

Getting started

If you see:

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock ...

add yourself to the docker group and fix the socket permissions:

sudo gpasswd -a $(whoami) docker
sudo chgrp docker /var/run/docker.sock
sudo service docker restart

Then sudo docker search nginx should work.

Pull an image from Docker Hub:

sudo docker pull nginx
sudo docker images

Create a host directory to mount:

sudo mkdir /opt/docker

Run the container:

sudo docker run -d -p 80:80 -v /opt/docker:/opt/ubuntu --name webserver nginx

Flags:

Visit http://localhost/ and you should see “Welcome to nginx!”

List running containers:

sudo docker ps

Enter the container:

sudo docker exec -it webserver /bin/bash

Here exec runs a command in the container, -it attaches your terminal, and /bin/bash starts a shell. Because we mounted /opt/docker to /opt/ubuntu, files created inside the container show up on the host.

Stop/Start/Remove:

sudo docker stop webserver
sudo docker start webserver
sudo docker rm webserver

Docker vs. Vagrant

Vagrant (vagrant up / vagrant ssh) provisions full VMs and can take a while to boot, especially the first time, and uses more memory. Docker is lighter and faster, so it is great when you want snappy environments. Vagrant can still be nice for full-stack dev setups managed by VirtualBox.