Python/Docker: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
=== Test a docker image ===
== Test a docker image ==
 
=== Pull images ===


<source lang="bash">
<source lang="bash">
sudo docker pull python:3.8.6
sudo docker pull python:3.8.6
sudo docker pull python:3.8.6-slim
sudo docker pull python:3.8.6-alpine
sudo docker images
python              3.8.6-alpine        8871758c6a8f        29 hours ago        42.4MB
python              3.8.6-slim          2be36bcc692b        29 hours ago        113MB
python              3.8.6              b4b9bf31ec03        29 hours ago        882MB
</source>
</source>
{| class="wikitable"
! || size || shell || lxml
|-
| python:{version} || 882MB || bash || O
|-
| python:{version}-slim || 113MB || bash || O
|-
| python:{version}-alpine || 42.4MB || busybox (/bin/sh) || X
|}
=== Read platform infomations ===


<source lang="bash">
<source lang="bash">
sudo docker run --name py01 -it python:3.8.6
sudo docker run --name py01 -it python:3.8.6
sudo docker run --name py01 -it python:3.8.6-slim
sudo docker run --name py01 -it python:3.8.6-alpine
</source>
<source lang="bash">
>>> import platform
>>> import platform
>>> platform.uname()
>>> platform.uname()
uname_result(system='Linux', node='812f9e38e887', release='5.4.0-58-generic', version='#64-Ubuntu SMP Wed Dec 9 08:16:25 UTC 2020', machine='x86_64', processor='')
>>> platform.machine()
>>> platform.machine()
'x86_64'
>>> exit()
>>> exit()
</source>
</source>
=== List packages ===


<source lang="bash">
<source lang="bash">
sudo docker run --name py02 -it python:3.8.6 /bin/bash
sudo docker run --name py02 -it python:3.8.6 /bin/bash
sudo docker run --name py02 -it python:3.8.6-slim /bin/bash
sudo docker run --name py02 -it python:3.8.6-alpine /bin/sh
</source>
<source lang="bash">
pip list
pip list


Line 24: Line 55:
setuptools 51.0.0
setuptools 51.0.0
wheel      0.36.2
wheel      0.36.2
</source>
=== Do a scheduled job ===
<source lang="bash">
sudo docker run --name py01 -v `pwd`:/app -w /app -it python:3.8.6-slim /bin/bash
pip install schedule
python start.py
</source>
'''start.py'''
<source lang="python">
import schedule
import time
def job():
    print("I'm working")
schedule.every(5).seconds.do(job)
while True:
    schedule.run_pending()
    time.sleep(1)
</source>
=== Do a scheduled job II (docker-compose) ===
<source lang="bash">
sudo docker-compose up
</source>
'''docker-compose.yaml'''
<source lang="yaml">
version: "3.7"
services:
  app:
    image: python:3.8.6-slim
    command: sh start.sh
    volumes:
      - ${APP_HOME}:/app
    working_dir: /app
</source>
'''requirements.txt'''
<source lang="text">
schedule == 0.6.0
</source>
'''start.sh'''
<source lang="sh">
pip install -r requirements.txt
python -u /app/start.py
</source>
'''start.py'''
<source lang="python">
import schedule
import time
def job():
    print("I'm working")
schedule.every(5).seconds.do(job)
while True:
    schedule.run_pending()
    time.sleep(1)
</source>
=== Do a scheduled job III (capture system signals) ===
'''docker-compose.yaml'''
<source lang="yaml">
version: "3.7"
services:
  app:
    image: python:3.8.6-slim
    command: python -u start.py
    volumes:
      - ${APP_HOME}:/app
    working_dir: /app
</source>
'''requirements.txt'''
<source lang="text">
schedule == 0.6.0
</source>
'''start.py'''
<source lang="python">
import os
import signal
import subprocess
import time
close_requested = False
def on_close():
    close_requested = True
def job():
    print("I'm working.")
def main():
    # Validate process id.
    if os.getpid() != 1:
        print('This script must be run as root process.')
        print('  pid: ', os.getpid())
    signal.signal(signal.SIGTERM, on_close)
    try:
        mtime = os.path.getmtime('requirements.inst')
    except:
        mtime = 0
    if mtime < os.path.getmtime('requirements.txt'):
        # Install requirements.
        subprocess.run(['pip', 'install', '-r', 'requirements.txt'])
        subprocess.run(['touch', 'requirements.inst'])
    import schedule
    schedule.every(3).seconds.do(job)
    while not close_requested:
        schedule.run_pending()
        time.sleep(1)
if __name__ == '__main__':
    main()
</source>
</source>

Latest revision as of 09:45, 17 December 2020

Test a docker image

Pull images

sudo docker pull python:3.8.6
sudo docker pull python:3.8.6-slim
sudo docker pull python:3.8.6-alpine
sudo docker images

python              3.8.6-alpine        8871758c6a8f        29 hours ago        42.4MB
python              3.8.6-slim          2be36bcc692b        29 hours ago        113MB
python              3.8.6               b4b9bf31ec03        29 hours ago        882MB
size shell lxml
python:{version} 882MB bash O
python:{version}-slim 113MB bash O
python:{version}-alpine 42.4MB busybox (/bin/sh) X

Read platform infomations

sudo docker run --name py01 -it python:3.8.6
sudo docker run --name py01 -it python:3.8.6-slim
sudo docker run --name py01 -it python:3.8.6-alpine
>>> import platform
>>> platform.uname()
>>> platform.machine()
>>> exit()

List packages

sudo docker run --name py02 -it python:3.8.6 /bin/bash
sudo docker run --name py02 -it python:3.8.6-slim /bin/bash
sudo docker run --name py02 -it python:3.8.6-alpine /bin/sh
pip list

Package    Version
---------- -------
pip        20.3.3
setuptools 51.0.0
wheel      0.36.2

Do a scheduled job

sudo docker run --name py01 -v `pwd`:/app -w /app -it python:3.8.6-slim /bin/bash
pip install schedule
python start.py

start.py

import schedule
import time

def job():
    print("I'm working")

schedule.every(5).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

Do a scheduled job II (docker-compose)

sudo docker-compose up

docker-compose.yaml

version: "3.7"

services:
  app:
    image: python:3.8.6-slim
    command: sh start.sh
    volumes:
      - ${APP_HOME}:/app
    working_dir: /app

requirements.txt

schedule == 0.6.0

start.sh

pip install -r requirements.txt
python -u /app/start.py

start.py

import schedule
import time

def job():
    print("I'm working")

schedule.every(5).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

Do a scheduled job III (capture system signals)

docker-compose.yaml

version: "3.7"

services:
  app:
    image: python:3.8.6-slim
    command: python -u start.py
    volumes:
      - ${APP_HOME}:/app
    working_dir: /app

requirements.txt

schedule == 0.6.0

start.py

import os
import signal
import subprocess
import time

close_requested = False

def on_close():
    close_requested = True

def job():
    print("I'm working.")

def main():
    # Validate process id.
    if os.getpid() != 1:
        print('This script must be run as root process.')
        print('  pid: ', os.getpid())
    signal.signal(signal.SIGTERM, on_close)

    try:
        mtime = os.path.getmtime('requirements.inst')
    except:
        mtime = 0

    if mtime < os.path.getmtime('requirements.txt'):
        # Install requirements.
        subprocess.run(['pip', 'install', '-r', 'requirements.txt'])
        subprocess.run(['touch', 'requirements.inst'])

    import schedule
    schedule.every(3).seconds.do(job)
    while not close_requested:
        schedule.run_pending()
        time.sleep(1)

if __name__ == '__main__':
    main()