Skip to content

Instantly share code, notes, and snippets.

@proppy
Last active August 29, 2015 13:57
Show Gist options
  • Save proppy/9823406 to your computer and use it in GitHub Desktop.
Save proppy/9823406 to your computer and use it in GitHub Desktop.
gce2docker: bootstrap a GCE vm with docker
FROM google/cloud-sdk
RUN apt-get update && apt-get install -y --no-install-recommends curl sshpass netcat-traditional
RUN curl https://get.docker.io/builds/Linux/x86_64/docker-latest -o /bin/docker && chmod +x /bin/docker
RUN curl http://stedolan.github.io/jq/download/linux64/jq -o /bin/jq && chmod +x /bin/jq
ADD gce2docker-fork.sh /
EXPOSE 44243
ENV DOCKER_HOST :44243
ENTRYPOINT ["/gce2docker-fork.sh"]
#!/bin/bash
set -ex
GS_ROOT=gs://proppy-docker/debian2docker-1395924258.tar.gz
ROOT_IMAGE=$(echo ${GS_ROOT} | sed -e 's/gs:\/\/.*\/\(.*\)\.tar\.gz/\1/')
DATE=$(date +%s)
INSTANCE=d2d-vm-${DATE}
DISK=d2d-data-${DATE}
DISK_SNAPSHOT=${DISK_SNAPSHOT:?'snapshot envvar not defined: `gcutil addsnapshot <d2d-data-snapshot-id> --source_disk=<d2d-data-disk-id> --wait`'}
PORT=${PORT:-4243}
ZONE=us-central1-a
MACHINE=machineTypes/f1-micro
gcutil adddisk ${DISK} --size=200 --zone=${ZONE} --source_snapshot=${DISK_SNAPSHOT}
gcutil addinstance ${INSTANCE} --zone=${ZONE} --image=${ROOT_IMAGE} --machine_type=${MACHINE} --disk=${DISK} --service_account_scopes=https://www.googleapis.com/auth/devstorage.full_control,https://www.googleapis.com/auth/compute
IP=$(gcutil getinstance --format=json ${INSTANCE} | jq -r .networkInterfaces[].accessConfigs[].natIP)
while !(echo | nc -w 1 ${IP} 22 | grep SSH); do
echo "connection refused"
done
echo "connection ok"
sshpass -p live ssh -oStrictHostKeyChecking=no -f -N -L ${PORT}:127.0.0.1:4243 docker@${IP}
docker -H :${PORT} version
#!/bin/bash
set -ex
GS_ROOT=gs://proppy-docker/debian2docker-1395924258.tar.gz
ROOT_IMAGE=$(echo ${GS_ROOT} | sed -e 's/gs:\/\/.*\/\(.*\)\.tar\.gz/\1/')
DATE=$(date +%s)
INSTANCE=d2d-vm-${DATE}
DISK=d2d-data-${DATE}
DISK_SNAPSHOT=d2d-data-snapshot
ZONE=us-central1-a
MACHINE=machineTypes/f1-micro
SRC_DISK=${SRC_DISK:-$(curl -H 'X-Google-Metadata-Request: true' http://metadata/computeMetadata/v1/instance/disks/1/device-name)}
gcutil addsnapshot ${DISK_SNAPSHOT} --zone=${ZONE} --source_disk=${SRC_DISK} --wait_until_complete || true
gcutil adddisk ${DISK} --size=200 --zone=${ZONE} --source_snapshot=${DISK_SNAPSHOT}
gcutil addinstance ${INSTANCE} --zone=${ZONE} --image=${ROOT_IMAGE} --machine_type=${MACHINE} --disk=${DISK} --permit_root_ssh --noadd_compute_key_to_project --service_account_scopes=https://www.googleapis.com/auth/devstorage.full_control,https://www.googleapis.com/auth/compute
IP=$(gcutil getinstance --format=json ${INSTANCE} | jq -r .networkInterfaces[].accessConfigs[].natIP)
while !(echo | nc -w 1 ${IP} 22 | grep SSH); do
echo "connection refused"
done
echo "connection ok"
ssh -oStrictHostKeyChecking=no -f -N -L 0.0.0.0:44243:127.0.0.1:4243 docker@${IP}
$@
sleep infinity
#!/bin/bash
set -ex
GS_ROOT=gs://proppy-docker/debian2docker-1395924258.tar.gz
GS_DISK=gs://proppy-docker/debian2docker-disk.tar.gz
ROOT_IMAGE=$(echo ${GS_ROOT} | sed -e 's/gs:\/\/.*\/\(.*\)\.tar\.gz/\1/')
DISK_IMAGE=$(echo ${GS_DISK} | sed -e 's/gs:\/\/.*\/\(.*\)\.tar\.gz/\1/')
DATE=$(date +%s)
INSTANCE=d2d-vm-${DATE}
DISK=d2d-data-${DATE}
ZONE=${ZONE:-us-central1-a}
MACHINE=${MACHINE:-machineTypes/f1-micro}
GCUTIL=$(which gcutil || true)
GCUTIL=${GCUTIL:?'gcloud not found `curl https://dl.google.com/dl/cloudsdk/release/install_google_cloud_sdk.bash | bash`'}
JQ=$(which jq || true)
JQ=${JQ:?'jq not found: `sudo apt-get/brew install jq`'}
SSHPASS=$(which sshpass || true)
SSHPASS=${SSHPASS:?'sshpass not found: `sudo apt-get/brew install sshpass`'}
NC=$(which nc || true)
NC=${NC:?'nc not found: `sudo apt-get/brew install netcat-traditional`'}
PORT=${PORT:-4243}
${GCUTIL} addimage ${ROOT_IMAGE} ${GS_ROOT} || true
${GCUTIL} addimage ${DISK_IMAGE} ${GS_DISK} || true
${GCUTIL} adddisk ${DISK} --size=200 --zone=${ZONE} --source_image=${DISK_IMAGE}
${GCUTIL} addinstance ${INSTANCE} --zone=${ZONE} --image=${ROOT_IMAGE} --machine_type=${MACHINE} --disk=${DISK} --service_account_scopes=https://www.googleapis.com/auth/devstorage.full_control,https://www.googleapis.com/auth/compute
IP=$(${GCUTIL} getinstance --format=json ${INSTANCE} | ${JQ} -r .networkInterfaces[].accessConfigs[].natIP)
while !(echo | ${NC} -w 1 ${IP} 22 | grep SSH); do
echo "connection refused"
done
echo "connection ok"
${SSHPASS} -p live ssh -oStrictHostKeyChecking=no -f -N -L ${PORT}:127.0.0.1:4243 docker@${IP}
echo "docker remote api forwarded on port localhost:${PORT}"
echo "usage: docker -H :4243 version"
echo "usage: sshpass -p live ssh docker@${IP}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment