Thursday, May 28, 2015

Docker Machine and the Docker Registry

This article is going to be focused on moving away from Boot2Docker and using Docker Machine (still in beta) in its place. After going through the prerequisites, you should be familiar with some basic commands and a workflow that accommodates individual Dockerfiles and leverages Boot2Docker to provision a VM pre-baked with Docker on your local machine.

Prerequisites: Read up on Docker basics and get familiar with this Docker workflow. This guide was written for Mac OS X users.

Moving forward, we're going to be using Docker Machine in its place. We're also going to deploy containers to live servers for the first time. Since Docker Machine is still under active development, keep in mind that some of these commands will only apply to the versions of the software I've indicated. Please refer to the official docs or reach out in the comments below if you have issues.

Local Machine Installation Using Docker Machine

# make sure docker is installed
brew install docker

# make sure docker-compose is installed
brew install docker-compose

# make sure docker-machine is installed
brew install docker-machine

# create a virtualbox vm with docker running
docker-machine create --driver virtualbox dev

# follow the instructions to connect
# it's usually this command
eval "$(docker-machine env dev)"

# if you want the ip address
docker-machine ip dev

# if you want to stop or start the machine
docker-machine stop dev
docker-machine start dev

Building a Container From a Dockerfile

In this section, we're revisiting the process of building containers from Dockerfiles. If you'd like to go revisit some past workflows and commands, head over to this article. You can either create your own application and Dockerfile or use the following examples:

# after your files are written...
# ...make sure you tag your repo in git
git push origin master
git tag 1.0.0 -m "First release"
git push --tags

# make sure the tag
git describe
1.0.0

# build the image 
docker build -t imagename .

# run the container and automatically map the ports
docker run --name containername -P -d imagename

# attach to the running instance
docker attach containername

# or open up a new tty within the instance
docker exec -it containername /bin/bash

# or just run the container's current process in interactive mode
docker run -it containername /bin/bash

Pushing to the Registry

Now when it comes to docker registries, you have the option to leverage the public Docker Hub or to host your own private registry on your servers. Since I haven't yet written an article about how to set up the latter, we're going to set up a private repository within the public registry, which is free but limited to 1. I recommend that you explore Docker's pricing plans before you sign up for an account. Once you're all set, carry out the following steps to sign up and being pushing containers.

# login
docker login
Username: yourusername
Password:
Email: youraddr@email.com
Login Succeeded

# build your container for the 1.0.0 release...
# ...we tagged earlier
docker build -t youraccount/yourrepo:$(git describe) .
.
.
Successfully built 5960307bb452

docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
youraccount/yourrepo            1.0.0               5960307aa452        4 minutes ago       132.9 MB

# push to repo
docker push youraccount/yourrepo

Automated Builds

Follow the instructions under the "Automated Builds" section of https://docs.docker.com/userguide/dockerrepos/.

Creating Your Docker Compose File

To make this easier, we're going to create a docker-compose.yml file that references the container we just pushed to the registry.

Preparing a Docker Host on Digital Ocean

We're now ready to deploy it to a production server. Sign up and generate a Digital Ocean access token. If you're using a different hosting solution, refer to this page and take note of the appropriate flags.

# create the docker machine
docker-machine create \
  --driver digitalocean \
  --digitalocean-access-token 0cb6684... \
  staging

# point to the new host
docker-machine env staging | source

# navigate to the docker-compose.yml file and run docker-compose
docker-compose up

Now run docker-machine ip in your terminal and navigate to [docker.machine.ip.address]:8080.

No comments:

Post a Comment