This is sample project for Test Driven Development (TDD) of Dockerfile by RSpec. This means developing dockerfile by below cycle,
- Write Rspec test
- Build Docker image and run test ->
RED
- Edit Dockerfile
- Build Docker image and run test ->
GREEN
- Write Rs...
When developing Dockerfile, docker image, we should test pakage installation and dockerfile specific command like CMD
or EXPOSE
. To do this I use belows,
- To test package installation, serverspec
- To test docker specific command, docker-api which is Docker Remote API wrapper.
- OSX
- Docker on Vagrant VM
$ ./build_and_test.sh
This script executes belows,
- Build image,
docker -H :5422 build -t tcnksm/sample /vagrant/.
- Run container,
docker -H :5422 run -p 7654:22 -d serverspec /usr/sbin/sshd -D
- Run rspec test,
bundle exec rspec
- Delete container,
docker stop
anddocker rm
In advance, run Vagrant VM
$ vagrant up
Assgin private network IP to Vagrant VM. And out ssh configuration,
$ vagrant ssh-config --host docker-vm >> ~/.ssh/config
To use serverspec, we need to prepare docker image which is prepared ssh and sudo user, see Dockerfile
. In Dockerfile
, to login the docker container by ssh without password, use public key on your localhost, prepare id_rsa.pub
in project directory.
To install,
$ gem install serverspec
Initialize,
$ bundle serverspec-init
Select OS type:
1) UN*X
2) Windows
Select number: 1
Select a backend type:
1) SSH
2) Exec (local)
Select number: 1
Vagrant instance y/n: n
Input target host name: 192.168.50.4
In Vagrantfile
, private network IP "192.168.50.4" is assined to Vagrant VM. You should use it for target host name.
Edit ~/.ssh/config
, using vagrant private network IP.
Host 192.168.50.4
HostName 192.168.50.4
User taichi
Port 7654
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/taichi/.ssh/id_rsa
IdentitiesOnly yes
LogLevel FATAL
Set environmental variable for sudo,
export SUDO_PASSWORD="pass"
or
export ASK_SUDO_PASSWORD=1
See sample spec/192.168.50.4/dockerfile_git_spec.rb
. For more details, see documents
By default, docker deamon listen on unix:///var/run/docker.sock. (see here), To use it from external host, you should bind specific IP and port. To do this edit /etc/init/docker.conf
. See docker.conf
Install,
$ gem install docker-api
Add below to spec_helper.rb
require "docker"
Docker.url = "http://192.168.50.4:5422"
IP is private IP assined Vagrant VM, port is bind port of docker deamon set by docker.conf
See sample spec/192.168.50.4/dockerfile_spec.rb
. For more details, see this blog post.