Last week I’ve been thrown into docker land head first and this post is just a few notes from my notes getting into it and figuring out the different concepts and tools.
First of all it needs to be installed. I’m running on OS X and favor homebrew for that:
> brew install docker > brew install docker-machine > brew install docker-compose
Now to the tools and what they do.
Creates and manages the docker host(s).
Earlier knows as boot2docker and the image it boots still is named boot2docker.iso. There are different drivers, below I’ll use virtual box which is very common on Mac OS X.
So on OS X creating a virtualbox docker host can look like this:
$ docker-machine create --driver virtualbox docker-test Running pre-create checks... Creating machine... (docker-test) Copying /Users/nippe/.docker/machine/cache/boot2docker.iso to /Users/nippe/.docker/machine/machines/docker-test/boot2docker.iso... (docker-test) Creating VirtualBox VM... (docker-test) Creating SSH key... (docker-test) Starting VM... Waiting for machine to be running, this may take a few minutes... Machine is running, waiting for SSH to be available... Detecting operating system of created instance... Detecting the provisioner... Provisioning with boot2docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect Docker to this machine, run: docker-machine env docker-test
Now we can see the machine exists:
> $ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS default - virtual box Running tcp://192.168.99.100:2376 v1.9.1 docker-test - virtual box Running tcp://192.168.99.101:2376 v1.9.1
And we can check it’s status
> $ docker-machine status docker-test Running
A common problem is not finding the docker-machine from your host (Mac OS X for example). You then need to expose the docker-machine config. First get it:
> $ docker-machine env docker-test export DOCKER_TLS_VERIFY=“1” export DOCKER_HOST=“tcp://192.168.99.101:2376” export DOCKER_CERT_PATH=“/Users/nippe/.docker/machine/machines/docker-test” export DOCKER_MACHINE_NAME=“docker-test” # Run this command to configure your shell: # eval $(docker-machine env docker-test)
Personally I’ve put an alias in my rc-file for running:
> eval $(docker-machine env docker-test)
“A lightweight runtime and robust tooling that builds and runs your Docker containers.”
So what that means, as I understand it, is that on a docker host (aka docker-machine) you can manage the separate docker images using the docker command.
docker command can be used to start, stop and manage individual docker containers. It can also run, pull and push images from/to the docker-registry.
If you get the error: “Cannot connect to the Docker daemon. Is the docker daemon running on this host?”
You can run (se more info below):
> eval $(docker-machine env docker-test)
To run an image:
> docker run hello-world
Remove docker images (don’t do this if you’re not sure):
> docker rmi $(docker images -q)
Is a repository for docker image files. The most common one is Docker Hub.
> docker run -i -p 3000:3000 grafana/grafana
Do docker or the docker engine enables us to run images in containers, which is great. Only thing is, usually our applications consist of more than one box. Here’s where docker compose comes in, it lets us specify entire environments.
Lets take an example: We want to spin up a solution with 3 boxes (web server with node.js, redis server and a mongo database). We might create a
docker-compose.yml-file looking something like this:
node: build: . container_name: web_app ports: - “3000” links: - redis - mongo redis: image: redis ports: - “6379:6379” mongo: image: mongo:3.0.4
In this case I have a Dockerfile in the same folder as docker-compose.yml that defines my node environment.
Then we build it and start it:
> docker-compose build > docker-compose up
And it spins up an entire environment with 3 containers. To check we can open another terminal (don’t forget the eval-trick mentioned earlier to set up docker environment variables, they are per terminal session) and run
> $ docker ps CONTAINER ID IMAGE COMMAND 7847a9012f4c dockercomposelab_node “npm start” a9182a623fc6 redis “/entrypoint.sh redis” 783370a485c1 mongo:3.0.4 “/entrypoint.sh mongo”
Docker Swarm is still a bit above my head but if I understand it somewhat correctly it’s a tool for managing multiple docker hosts as a single unit / docker container.
Hope this helps!