[PYTHON] Generate a Docker image using Fabric

Overview

Let's create a Docker image using Fabric. Since Fabric can only be operated via SSH, it is necessary to operate Docker via SSH.

First, define a function docker that performs the following processing.

import atexit
import time
from fabric.api import *
from fabric.contrib.console import confirm


@task
def docker():
    env.hosts = ['127.0.0.1:2222']
    env.user = 'root'
    env.password = 'PASSWORD'

    import docker
    cl = docker.Client()
    container = cl.create_container('BASE_IMAGE_NAME',
                                    command='/usr/sbin/sshd -D',
                                    ports=[22])

    cl.start(container, port_bindings={22:2222})
    time.sleep(3)

    @atexit.register
    def commit():
        cl.stop(container)
        if confirm('Commit container changes?', default=True):
            cl.commit(container['Id'], repository='IMAGE_NAME', tag='TAG')

By writing the following in fabfile.py,

$ fab docker TASK_NAME

It will be possible to execute like.

Also, if you automate and execute docker push etc. in the above commit function, you can automate everything from task execution to Docker Index upload.

In addition, docker-py is required for execution.

Recommended Posts

Generate a Docker image using Fabric
Try using Jupyter's Docker image
Build a go environment using Docker
Create a GCE instance from a GCR Docker image using terraform
How to code a drone using image recognition
Generate a hash value using the HMAC method.
Pack Japanese processing software into a Docker image
[AWS Lambda] Create a deployment package using the Docker image of Amazon Linux
Summary when using Fabric
Expose your Docker image
Generate a vertical image of a novel from text data
How to hold a hands-on seminar using Jupyter using docker
Image segmentation using U-net
[Python] Mask the image into a circle using Pillow
Carry a Docker container
Generate image text together
Jupyter Docker image summary
Build a lightweight Fast API development environment using Docker
I tried using PI Fu to generate a 3D model of a person from one image
Try a similar search for Image Search using the Python SDK [Search]
How to generate a query using the IN operator in Django
Build a Flask development environment at low cost using Docker
Create a gadget-like transparent background image type window using wxpython
Time measurement using a clock
Randomly generate a complete permutation
Cloud image prediction using convLSTM
Pepper Tutorial (5): Using a Tablet
Using a printer with Debian 10
Generate OCI architecture diagrams using Diagrams
The image is a slug
A memorandum of using eigen3
Do a search by image from the camera roll using Pythonista3
Try to edit a new image using the trained StyleGAN2 model
Create a simple scheduled batch using Docker's Python Image and parse-crontab
Create a Docker container image with JRE8 / JDK8 on Amazon Linux