Skip to content

Instantly share code, notes, and snippets.

@hdemon
Last active January 4, 2016 06:59
Show Gist options
  • Select an option

  • Save hdemon/8585452 to your computer and use it in GitHub Desktop.

Select an option

Save hdemon/8585452 to your computer and use it in GitHub Desktop.
docker setting
from fabric.api import *
from fabric.contrib import *
import datetime
import os
def setup_docker():
update_apt_get()
install_basic_packages()
def create_basement_image():
source_file = './templates/Dockerfile-basement'
destination_file = './tmp/Dockerfile'
sudo("mkdir -p ./tmp")
files.upload_template(source_file, destination_file, mode=0777)
image = Image("basement", destination_file)
image.build()
print image.image_id
def create_mysql_image():
source_file = './templates/Dockerfile-mysql'
destination_file = './tmp/Dockerfile'
sudo("mkdir -p ./tmp")
files.upload_template(source_file, destination_file, mode=0777)
image = Image("mysql", destination_file)
image.build()
print image.image_id
def create_web_server_image():
context = { "parent_image_id": Image.image_id("basement")[0] }
source_file = './templates/Dockerfile-web-server'
destination_file = './Dockerfile'
sudo("mkdir -p ./tmp")
files.upload_template(source_file, destination_file, context=context, mode=0777)
image = Image("mysql", destination_file)
image.build()
print image.image_id
def remove_all_images():
remove_containers_of(repository_name)
sudo('docker rmi $(sudo docker images -q)')
def remove_images_without_latest_one(repository_name):
for image_id in Image.image_id_of(repository_name)[1:]:
sudo("docker rmi %s" % (image_id))
def remove_images(repository_name, tag = None):
for image_id in Image.image_id_of(repository_name, tag):
remove_containers_of(repository_name, tag)
sudo("docker rmi %s" % (image_id))
def remove_all_containers():
sudo('docker rm $(sudo docker ps -a -q)')
class Image:
def __init__(self, repository_name, dockerfile_path):
self.tag = None
self.repository_name = repository_name
self.dockerfile_directory = "/".join(dockerfile_path.split("/")[::-1][1:][::-1])
def build(self):
self.tag = self.timestamp()
sudo("docker build -no-cache -t %s:%s %s" % (self.repository_name, self.tag, self.dockerfile_directory))
def timestamp(self):
return datetime.datetime.today().strftime("%Y%m%d%H%M%S")
def image_id(self):
return sudo("docker images|grep -E \"^%s\s+%s\"|awk '{print $3}'" % (self.repository_name, self.tag))
def container_id(self):
id_array = sudo("docker ps -a|grep %s:%s|awk '{print $1}'" % (self.repository_name, self.tag)).split("\r\n")
if len(id_array) == 0:
return None
elif len(id_array) == 1:
return id_array[0]
else:
return id_array
# @classmethod
# def container_ids_of(self, image_id_or_repository_name):
# id_array = sudo("docker ps -a|grep %s|awk '{print $1}'" % image_id_or_repository_name).split("\r\n")
# if len(id_array) == 0:
# return None
# elif len(id_array) == 1:
# return id_array[0]
# else:
# return id_array
@classmethod
def image_id(self, repository_name, tag = None):
if tag == None:
return sudo("docker images|grep -E \"^%s\"|awk '{print $3}'" % (repository_name)).split('\r\n')
else:
return sudo("docker images|grep -E \"^%s\s+%s\"|awk '{print $3}'" % (repository_name, tag)).split('\r\n')[0]
def update_apt_get():
sudo("apt-get update -y")
sudo("apt-get upgrade -y")
def install_basic_packages():
sudo("""
apt-get install -y \
sudo \
man-db \
wget \
git \
nano \
curl \
dialog \
net-tools \
patch \
gcc \
openssl \
make \
bzip2 \
autoconf \
automake \
libtool \
bison
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment