diff --git a/README.md b/README.md index 316dce1..ac0c1f4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bocker b/bocker index 0484261..0c6cfac 100755 --- a/bocker +++ b/bocker @@ -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 +function bocker_init() { #HELP Create an image from a directory:\nBOCKER init 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 +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 [[ "$(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 diff --git a/tests/teardown b/tests/teardown index 7667107..02d00ee 100644 --- a/tests/teardown +++ b/tests/teardown @@ -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 diff --git a/tests/test_images b/tests/test_images index af17b22..e617033 100644 --- a/tests/test_images +++ b/tests/test_images @@ -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' ]] diff --git a/tests/test_pull b/tests/test_pull new file mode 100644 index 0000000..4b79179 --- /dev/null +++ b/tests/test_pull @@ -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"* ]]