def build_mongodb(): with DockerFile('ubuntu:latest', maintainer='Ted Ostrem, [email protected]') as df: df.prefix('ENV', 'DEBIAN_FRONTEND', 'noninteractive') df.run('apt-get update') df.run('apt-get -y upgrade') df.run(('apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 ' '--recv 7F0CEB10 && ' 'echo "deb http://repo.mongodb.org/apt/ubuntu ' '"$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | ' 'tee /etc/apt/sources.list.d/mongodb-org-3.0.list && ' 'apt-get update && ' 'apt-get install -y mongodb-org')) df.add_volume('/data/db') df.prefix('ENV', 'REPLSET', 'rs0') df.prefix('ENV', 'AUTH', 'no') df.prefix('ENV', 'STORAGE_ENGINE', 'wiredTiger') df.prefix('ENV', 'JOURNALING', 'yes') df.add_file('run.sh', '/run.sh') df.add_file('set_mongodb_password.sh', '/set_mongodb_password.sh') df.prefix('EXPOSE', '27017', '28017') df.prefix('CMD', './run.sh') docker_fabric().build_from_file(df, 'tedostrem/mongodb', add_latest_tag=True, rm=True)
def setup(): """ Pull needed official images, setup db and add nginx settings """ docker_fabric().pull("postgres:latest") docker_fabric().create_container("postgres", name="db", ports=["5432"]) # TODO: Add restart policy docker_fabric().start("db", restart_policy={ 'MaximumRetryCount': 0, 'Name': 'always' }, binds={ '/home/var/lib/postgresql/data': { 'bind': '/var/lib/postgresql/data', 'rw': True } }) # Create nginx path run("mkdir -p /home/vagrant/var/nginx/conf") docker_fabric().pull("nginx:latest") # Upload nginx config config_path = os.path.join(os.getcwd(), "files", "nginx.conf") put(config_path, "/home/vagrant/var/nginx/conf/nginx.conf")
def reload_nginx(): # Remove old running nginx container try: docker_fabric().stop("nginx") docker_fabric().remove_container("nginx") except: print("Nginx has not been started") pass # Setup and run nginx container docker_fabric().create_container("nginx:latest", name="nginx", ports=["80:80"]) docker_fabric().start("nginx", port_bindings={80: 80}, restart_policy={ 'MaximumRetryCount': 0, 'Name': 'always' }, binds={ '/home/vagrant/var/nginx/conf': { 'bind': '/etc/nginx', 'ro': True } }, links={ "web": "web" })
def temp_container(image, no_op_cmd="/bin/true", create_kwargs=None, start_kwargs=None): """ Creates a temporary container, which can be used e.g. for copying resources. The container is removed once it is no longer needed. Note that ``no_op_cmd`` needs to be set appropriately, since the method will wait for the container to finish before copying resources. :param image: Image name or id to create the container from. :type image: unicode :param no_op_cmd: Dummy-command to run, only for being able to access the container. :type no_op_cmd: unicode :param create_kwargs: Additional kwargs for creating the container. The ``entrypoint`` will be set to ``no_op_cmd``. :type create_kwargs: dict :param start_kwargs: Additional kwargs for starting the container. ``restart_policy`` will be set to ``None``. :type start_kwargs: dict :return: Id of the temporary container. :rtype: unicode """ df = docker_fabric() create_kwargs = create_kwargs.copy() if create_kwargs else dict() start_kwargs = start_kwargs.copy() if start_kwargs else dict() create_kwargs.update(entrypoint=no_op_cmd) start_kwargs.update(restart_policy=None) container = df.create_container(image, **create_kwargs)["Id"] df.start(container, **start_kwargs) df.wait(container) yield container df.remove_container(container)
def temp_container(image, no_op_cmd='/bin/true', create_kwargs=None, start_kwargs=None): """ Creates a temporary container, which can be used e.g. for copying resources. The container is removed once it is no longer needed. Note that ``no_op_cmd`` needs to be set appropriately, since the method will wait for the container to finish before copying resources. :param image: Image name or id to create the container from. :type image: unicode :param no_op_cmd: Dummy-command to run, only for being able to access the container. :type no_op_cmd: unicode :param create_kwargs: Additional kwargs for creating the container. The ``entrypoint`` will be set to ``no_op_cmd``. :type create_kwargs: dict :param start_kwargs: Additional kwargs for starting the container. ``restart_policy`` will be set to ``None``. :type start_kwargs: dict :return: Id of the temporary container. :rtype: unicode """ df = docker_fabric() create_kwargs = create_kwargs.copy() if create_kwargs else dict() start_kwargs = start_kwargs.copy() if start_kwargs else dict() create_kwargs.update(entrypoint=no_op_cmd) start_kwargs.update(restart_policy=None) container = df.create_container(image, **create_kwargs)['Id'] df.start(container, **start_kwargs) df.wait(container) yield container df.remove_container(container)
def production(): """Remote production environment""" env.docker = docker_fabric() bootstrap_environment('production') env.project_path = '/home/{user}/{{cookiecutter.app_name}}'.format(**env) env.forward_agent = True env.map_client = container_fabric(env.container_map) env.run = run env.roledefs = { 'web': ['{user}@{site_url}'.format(**env)], }
def development(): """Remote staging/development environment""" env.docker = docker_fabric() bootstrap_environment('development') env.project_path = '/home/{user}/jenkins-test'.format(**env) env.forward_agent = True env.map_client = container_fabric(env.container_map) env.run = run env.roledefs = { 'web': ['{user}@{site_url}'.format(**env)], }
def deploy(): """ Run deployment process """ # Retrive app version from app image dockerfile deploy_version = local('cat %s/Dockerfile | \ grep -e "^LABEL.version" | \ cut -d \\" -f 2' % image_dir, capture=True) # Build docker app image local("docker build -t %s:%s %s" % (image_repository, deploy_version, image_dir)) # Save docker app image local("docker save %s:%s > /tmp/docker_image.tar" % (image_repository, deploy_version)) # Copy docker app image to machine put("/tmp/docker_image.tar", "/tmp/docker_image.tar") # Load docker image run("docker load < /tmp/docker_image.tar") # Stop nginx before removing web try: docker_fabric().stop("nginx") except Exception as e: print("Nginx has not been created yet.") print(e) pass # Remove old running app container try: docker_fabric().stop("web") except Exception as e: print("Container Web has not been started") print(e) pass try: docker_fabric().remove_container("web") except Exception as e: print("Container web cannot be removed") print(e) pass # Remove previous app docker image try: docker_fabric().remove_image("%s:latest" % image_repository) except: print("Image not found") pass # Duplicate deploy app image and tag it latest run("docker tag %s:%s %s:latest" % (image_repository, deploy_version, image_repository)) # Remove older releases of app image with warn_only(): run('docker images | \ grep %s | \ grep -v latest | \ awk \'{print "%s:"$2}\' | \ tail -n +4 | \ xargs -n 1 docker rmi' % (image_repository, image_repository)) # Remove local app image export local("rm /tmp/docker_image.tar") # Remove remote app image export run("rm /tmp/docker_image.tar") # Start app container docker_fabric().create_container("%s:%s" % (image_repository, "latest"), name="web", ports=["8080"]) docker_fabric().start("web", links={ "db": "db" }) reload_nginx()