Moving From Docker-osx-dev to the Docker Toolbox
Relatively recently the Docker project releaesd a new tool set which includes an easier way to install and configure the Docker utilities.
I’ve been using this new tooling for a couple of months now and it is a lot easier to set up and use than my old environment described in the article above. I removes the need for tools like docker-osx-dev and the need to separately manage tools like the boot2docker utility.
Short description
- Install VirtualBox v 5.0.10
- Install the Docker Toolbox v1.11.1
Long description
This describes the installation and usage for OSX.
Docker Quickstart Terminal
or Kitematic.Download and install VirtualBox using the provided installer. Download and install Docker Toolbox using the provided installer.
Then open a terminal and running the following:
$ docker-machine create --driver virtualbox dockerhost
Since you need Linux to run Docker this creates a small Linux image with a Docker environment configured inside it.
In some cases it’s useful to create the base dockerhost image with more memory than the default (for instance running a MongoDB images seems to require it in some cases). Do do this you can run the following:
$ docker-machine create --driver virtualbox --virtualbox-memory 4096 dockerhost
Now to make this useful on OXS run the following:
$ docker-machine env dockerhost
Which will print out something like:
For fish shell:
set -gx DOCKER_TLS_VERIFY "1";
set -gx DOCKER_HOST "tcp://192.168.99.100:2376";
set -gx DOCKER_CERT_PATH "/Users/bbabb/.docker/machine/machines/dockerhost";
set -gx DOCKER_MACHINE_NAME "dockerhost";
# Run this command to configure your shell:
# eval (docker-machine env dockerhost)
For bash shell:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/bbabb/.docker/machine/machines/dockerhost"
export DOCKER_MACHINE_NAME="dockerhost"
# Run this command to configure your shell:
# eval $(docker-machine env dockerhost)
Notice the eval
line. You can add this to your shell environments start configuration to make interacting with Docker easy.
In fish
add this line to the ~/.config/fish/config.fish
file. In bash
add this line to the ~/.bash_profile
file. Now
when you start a new terminal session the docker tools should be able to connect to the running docker environment.
Now run:
$ docker ps
And if everything is working you should see an email list of docker containers.
Now run:
docker run --rm -it centos:7 bash
After the command completes you’ll be droped into a bash prompt in a CentOS 7 environment. Play around, and then to exit out of the container just run:
exit
The last step that I normally perform is to create a /etc/hosts
entry for the docker host machine, so that you can connect
to the docker environment with a user friendly name.
For instance using the example output from the docker-machien env
invocation above add and entry like the following
to your /etc/hosts
file:
192.168.99.100 dockerhost
To test this setup we’ll create a small NodeJS hello world application. This will use the Docker NodeJS provided language stack.
Create the following Dockerfile:
FROM node:0.12.9
ENV SRC_DIR=/src
RUN mkdir -p $SRC_DIR
COPY . $SRC_DIR
WORKDIR $SRC_DIR
RUN npm install
EXPOSE 8080
CMD [ "npm", "start" ]
And a server.js
file:
'use strict';
var express = require('express');
var PORT = 8080;
var app = express();
app.get('/', function (req, res) {
res.send('Hello world\n');
});
app.listen(PORT);
console.log('Running on http://dockerhost:' + PORT);
and the package.json
file:
{
"name": "myimage",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "First Last <first.last@example.com>",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.13.3"
}
}
all in the same directory. Now the application can be built by running:
docker build --tag myimage .
Now the application is ready to execute. Run:
$ docker run -p 8080:8080 -d myimage
You should now be able to browse to http://dockerhost:8080
.
To stop the server you can run:
$ docker ps
Grab the CONTAINER_ID
for your application, then run:
docker stop <container ID>
A good reference for the NodeJS parts is: Dockerizing a Node.js web app.