Implement 'bocker pull' for Docker Hub integration

This commit is contained in:
Peter Wilmott 2015-07-27 19:04:14 +01:00
parent d5b38393c2
commit b240a47ac8
5 changed files with 66 additions and 39 deletions

View File

@ -12,6 +12,7 @@ Docker implemented in 100 lines of bash.
The following packages are needed to run bocker.
* btrfs-progs
* curl
* iproute2
* iptables
* util-linux >= 2.25.2
@ -34,30 +35,33 @@ Even if you meet the above prerequisites you probably still want to **run bocker
## Example Usage
```
$ bocker init base-image/
Created: img_84632
$ bocker pull centos 7
######################################################################## 100.0%
######################################################################## 100.0%
######################################################################## 100.0%
Created: img_42150
$ bocker images
IMAGE_ID
img_84632
IMAGE_ID SOURCE
img_42150 centos:7
$ bocker run img_84632 uname -sro
Linux 3.10.0-123.20.1.el7.x86_64 GNU/Linux
$ bocker run img_42150 cat /etc/centos-release
CentOS Linux release 7.1.1503 (Core)
$ bocker ps
CONTAINER_ID COMMAND
ps_12277 uname -sro
ps_42045 cat /etc/centos-release
$ bocker logs ps_12277
Linux 3.10.0-123.20.1.el7.x86_64 GNU/Linux
$ bocker logs ps_42045
CentOS Linux release 7.1.1503 (Core)
$ bocker rm ps_12277
Removed: ps_12277
$ bocker rm ps_42045
Removed: ps_42045
$ bocker run img_84632 which wget
$ bocker run img_42150 which wget
which: no wget in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
$ bocker run img_84632 yum install -y wget
$ bocker run img_42150 yum install -y wget
Installing : wget-1.14-10.el7_0.1.x86_64 1/1
Verifying : wget-1.14-10.el7_0.1.x86_64 1/1
Installed : wget.x86_64 0:1.14-10.el7_0.1
@ -65,35 +69,25 @@ Complete!
$ bocker ps
CONTAINER_ID COMMAND
ps_14099 which wget
ps_43377 yum install -y wget
ps_42018 yum install -y wget
ps_42182 which wget
$bocker commit ps_43377 img_84632
Removed: img_84632
Created: img_84632
$ bocker commit ps_42018 img_42150
Removed: img_42150
Created: img_42150
$ bocker run img_84632 which wget
$ bocker run img_42150 which wget
/usr/bin/wget
$ bocker rm ps_14099
Removed: ps_14099
$ bocker rm ps_43377
Removed: ps_43377
$ bocker rm ps_95942
Removed: ps_95942
$ bocker rm img_84632
Removed: img_84632
```
## Functionality: Currently Implemented
* `docker build`
* `docker pull`
* `docker images`
* `docker ps`
* `docker run`
* `docker exec`
* `docker logs`
* `docker commit`
* `docker rm` / `docker rmi`
@ -103,9 +97,10 @@ Removed: img_84632
## Functionality: Not Yet Implemented
* Port Forwarding
* Data Volumes
* Data Volume Containers
* Data Volumes
* Port Forwarding
* Quota Support / CGroups
## License

24
bocker
View File

@ -4,26 +4,42 @@ btrfs_path='/var/bocker';
function bocker_check() {
btrfs subvolume list "$btrfs_path" | grep -qw "$1" && echo 0 || echo 1
}
function bocker_init() { #HELP Create an image:\nBOCKER init <image_directory>
function bocker_init() { #HELP Create an image from a directory:\nBOCKER init <directory>
uuid="img_$(shuf -i 42002-42254 -n 1)"
if [[ -d "$1" ]]; then
[[ "$(bocker_check "$uuid")" == 0 ]] && bocker_run "$@"
btrfs subvolume create "$btrfs_path/$uuid" > /dev/null
cp -rf --reflink=auto "$1"/* "$btrfs_path/$uuid" > /dev/null
[[ ! -f "$btrfs_path/$uuid"/img.source ]] && echo "$1" > "$btrfs_path/$uuid"/img.source
echo "Created: $uuid"
else
echo "No directory named '$1' exists"
fi
}
function bocker_pull() { #HELP Pull an image from Docker Hub:\nBOCKER pull <name> <tag>
token="$(curl -sL -o /dev/null -D- -H 'X-Docker-Token: true' "https://index.docker.io/v1/repositories/$1/images" | tr -d '\r' | awk -F ': *' '$1 == "X-Docker-Token" { print $2 }')"
registry='https://registry-1.docker.io/v1'
id="$(curl -sL -H "Authorization: Token $token" "$registry/repositories/$1/tags/$2" | sed 's/"//g')"
[[ "${#id}" -ne 64 ]] && echo "No image named '$1:$2' exists" && exit 1
ancestry="$(curl -sL -H "Authorization: Token $token" "$registry/images/$id/ancestry")"
IFS=',' && ancestry=(${ancestry//[\[\] \"]/}) && unset IFS; tmp_uuid="$(uuidgen)" && mkdir /tmp/"$tmp_uuid"
for id in "${ancestry[@]}"; do
curl -#L -H "Authorization: Token $token" "$registry/images/$id/layer" -o /tmp/"$tmp_uuid"/layer.tar
tar xf /tmp/"$tmp_uuid"/layer.tar -C /tmp/"$tmp_uuid" && rm /tmp/"$tmp_uuid"/layer.tar
done
echo "$1:$2" > /tmp/"$tmp_uuid"/img.source
bocker_init /tmp/"$tmp_uuid" && rm -rf /tmp/"$tmp_uuid"
}
function bocker_rm() { #HELP Delete an image or container:\nBOCKER rm <image_id or container_id>
[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
btrfs subvolume delete "$btrfs_path/$1" > /dev/null
echo "Removed: $1"
}
function bocker_images() { #HELP List images:\nBOCKER images
echo -e "IMAGE_ID"
echo -e "IMAGE_ID\t\tSOURCE"
for img in "$btrfs_path"/img_*; do
basename "$img"
img=$(basename "$img")
echo -e "$img\t\t$(cat "$btrfs_path/$img/img.source")"
done
}
function bocker_ps() { #HELP List containers:\nBOCKER ps
@ -77,6 +93,6 @@ sed -n "s/^.*#HELP\\s//p;" < "$1" | sed "s/\\\\n/\n\t/g;s/$/\n/;s!BOCKER!${1/!/\
}
[[ -z "${1-}" ]] && bocker_help "$0"
case $1 in
init|rm|images|ps|run|exec|logs|commit) bocker_"$1" "${@:2}" ;;
pull|init|rm|images|ps|run|exec|logs|commit) bocker_"$1" "${@:2}" ;;
*) bocker_help "$0" ;;
esac

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
for img in $(./bocker images | grep 'img'); do
for img in $(./bocker images | grep 'img' | awk '{print $1}'); do
./bocker rm "$img"
done

View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
[[ "$(./bocker images | head -n 1)" == 'IMAGE_ID' ]]
[[ "$(./bocker images | head -n 1)" == 'IMAGE_ID SOURCE' ]]

16
tests/test_pull Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
centos_img="$(./bocker pull centos 7 2> /dev/null | awk '{print $2}')"
./bocker run "$centos_img" cat /etc/redhat-release
ps="$(./bocker ps | grep 'cat /etc/redhat-release' | awk '{print $1}')"
logs="$(./bocker logs "$ps")"
./bocker rm "$ps"
[[ "$logs" == "CentOS Linux release 7"* ]]
ubuntu_img="$(./bocker pull ubuntu 14.04 2> /dev/null | awk '{print $2}')"
./bocker run "$ubuntu_img" tail -n1 /etc/lsb-release
ps="$(./bocker ps | grep 'tail -n1 /etc/lsb-release' | awk '{print $1}')"
logs="$(./bocker logs "$ps")"
./bocker rm "$ps"
[[ "$logs" == *"Ubuntu 14.04"* ]]