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.
docker
) to be executed before starting the task, and execute sshd in the Docker container.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