<jupyter_start><jupyter_text>Dockerize NotebookThe purpose of this notebook is to aid my tutorial found [here](https://medium.com/@patrickhanbury1/how-to-launch-docker-containers-with-the-official-python-library-31dbe1bff8ca). This notebook is intended to function as a standlone piece that both clones the rHuggler's Django repository for you and launches the Docker containers.<jupyter_code>import docker
import os
from git import Repo
import shutil
client = docker.from_env()
cwd = os.getcwd()
# dock_pth = os.path.join(cwd,'todo')<jupyter_output><empty_output><jupyter_text>Clone Repo<jupyter_code>git_url = 'https://github.com/rHuggler/django-todo.git'
repo_dir = os.path.join(cwd, 'todo')
if os.path.isdir(repo_dir):
    print(f"Removing {repo_dir}")
    shutil.rmtree(repo_dir, ignore_errors=True)
Repo.clone_from(git_url, repo_dir);<jupyter_output><empty_output><jupyter_text>Clean Up DockerThis was not included in the tutorial, but might be helpful if you want to automate the pruning and removal of your Docker objects<jupyter_code># remove dangling containers
db_name = 'db'
web_name = 'web'
api_name = 'api'
names = [db_name, web_name, api_name]
for cont in client.containers.list(all=True):
    if cont.name in names:
        cont.stop() # if running
        try:
            cont.remove()
        except:
            pass
# remove any dangling nets
net_name = 'djang-net'
nets = client.networks.list()
for net in nets:
    if net.name == net_name:
        net.remove()
## Create Shell Script and Move to Todo
with open('todo/api/django_todo/api.sh', 'w') as fb:
    cmds = ['python manage.py makemigrations', 'python manage.py migrate',
           'python manage.py runserver 0.0.0.0:8000']
    for cmd in cmds:
        fb.write(cmd + '\n')
        
with open('todo/web/vue-todo/web.sh', 'w') as fb:
    cmds = ['npm install', 'npm run dev']
    for cmd in cmds:
        fb.write(cmd + '\n')<jupyter_output><empty_output><jupyter_text>Build Images<jupyter_code>web_pth = os.path.join(repo_dir, 'web')
web_img, logs = client.images.build(path=web_pth, tag='web')
api_pth = os.path.join(repo_dir, 'api')
api_img, logs = client.images.build(path=api_pth, tag='api')<jupyter_output><empty_output><jupyter_text>Build Bridge Network<jupyter_code>net = client.networks.create(name=net_name, driver='bridge')<jupyter_output><empty_output><jupyter_text>Run Containers  Define customizable parameters<jupyter_code># ports
api_ports = {'8000/tcp' : 8000}
web_ports = {'8080/tcp' : 8080}
web_env = ["HOST=0.0.0.0"]

# volumes
api_vol = { 
    os.path.join(repo_dir, 'api/django_todo') : {'bind': '/usr/src/app', 'mode': 'rw'}
}

web_vol = {
    os.path.join(repo_dir, 'web/vue-todo') : { 'bind' : '/usr/src/app', 'mode' : 'rw'}
}

db_vol = {
    os.path.join(repo_dir, 'dbdata') : {'bind' : '/var/lib/postgresql/data'}
}<jupyter_output><empty_output><jupyter_text>Launch Containers<jupyter_code>db = client.containers.run('postgres:10', detach=True, name='db', 
                           network=net_name, volumes=db_vol)

api = client.containers.run(api_img, ports=api_ports, detach=True, 
                            name='api', network=net_name, volumes=api_vol,
                            command="sh api.sh")

# api.restart()

web = client.containers.run(web_img, ports=web_ports, detach=True,
                           name='web', network=net_name, volumes=web_vol,
                           environment=web_env, command="sh web.sh")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13780207134637515, 'fraction_numerical': 0.010069044879171462, 'mean_word_length': 3.988522238163558, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '16234', 'license': 'MIT', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1237, 'n_words': 258, 'path': 'dockerize.ipynb', 'repo_name': 'pat-hanbury/Python-Docker-Tutorial', 'size': '3476'}