|
| 1 | +# Service 编排演示 |
| 2 | + |
| 3 | +```python |
| 4 | +#!/usr/bin/python3 |
| 5 | +#-*- coding: utf-8 -*- |
| 6 | +############################################## |
| 7 | +# Home : https://www.netkiller.cn |
| 8 | + |
| 9 | +# Upgrade: 2025-07-24 |
| 10 | +############################################## |
| 11 | +import os, sys |
| 12 | + |
| 13 | +module = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 14 | +sys.path.insert(0, module) |
| 15 | +from netkiller.docker import * |
| 16 | + |
| 17 | +dockerfile = Dockerfile("neo") |
| 18 | +dockerfile.image('nginx:latest').volume(['/etc/nginx','/var/log/nginx']).run('apt update -y && apt install -y procps').expose(['80','443']).workdir('/opt') |
| 19 | +dockerfile.show() |
| 20 | +print('-'*60) |
| 21 | + |
| 22 | +neo = Services('netkiller') |
| 23 | +neo.build(dockerfile) |
| 24 | +neo.image("netkiller:1.3.0") |
| 25 | +neo.show() |
| 26 | +print('-'*60) |
| 27 | + |
| 28 | +development = Composes('development') |
| 29 | +development.version('3.9') |
| 30 | +development.services(neo) |
| 31 | +development.show() |
| 32 | + |
| 33 | +print('-'*60) |
| 34 | +development.debug() |
| 35 | +development.save('/tmp/neo.yaml') |
| 36 | +``` |
| 37 | + |
| 38 | +输出演示 |
| 39 | + |
| 40 | +```text |
| 41 | +FROM nginx:latest |
| 42 | +VOLUME ["/etc/nginx","/var/log/nginx"] |
| 43 | +RUN apt update -y && apt install -y procps |
| 44 | +EXPOSE 80 443 |
| 45 | +WORKDIR /opt |
| 46 | +------------------------------------------------------------ |
| 47 | +container_name: netkiller |
| 48 | +build: |
| 49 | + context: . |
| 50 | + dockerfile: Dockerfile |
| 51 | +image: netkiller:1.3.0 |
| 52 | +
|
| 53 | +------------------------------------------------------------ |
| 54 | +services: |
| 55 | + netkiller: |
| 56 | + container_name: netkiller |
| 57 | + build: |
| 58 | + context: /Users/neo/GitHub/devops/doc/docker |
| 59 | + dockerfile: ./development/netkiller/Dockerfile |
| 60 | + image: netkiller:1.3.0 |
| 61 | +version: '3.9' |
| 62 | +
|
| 63 | +------------------------------------------------------------ |
| 64 | +{'services': {'netkiller': {'container_name': 'netkiller', 'build': {'context': '/Users/neo/GitHub/devops/doc/docker', 'dockerfile': './development/netkiller/Dockerfile'}, 'image': 'netkiller:1.3.0'}}, 'version': '3.9'} |
| 65 | +``` |
| 66 | + |
| 67 | +## target 演示 |
| 68 | + |
| 69 | +设置 target 名称 init |
| 70 | + |
| 71 | +```text |
| 72 | +FROM nginx:latest AS init |
| 73 | +COPY --from=init index.html /var/www |
| 74 | +``` |
| 75 | + |
| 76 | +代码 |
| 77 | + |
| 78 | +```python |
| 79 | +#!/usr/bin/python3 |
| 80 | +#-*- coding: utf-8 -*- |
| 81 | +############################################## |
| 82 | +# Home : https://www.netkiller.cn |
| 83 | + |
| 84 | +# Upgrade: 2025-07-24 |
| 85 | +############################################## |
| 86 | +import os, sys |
| 87 | + |
| 88 | +module = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 89 | +sys.path.insert(0, module) |
| 90 | +from netkiller.docker import * |
| 91 | + |
| 92 | +target = 'init' |
| 93 | + |
| 94 | +dockerfile = Dockerfile("neo",None,target) |
| 95 | +dockerfile.image('nginx:latest',target).volume(['/etc/nginx','/var/log/nginx']).run('apt update -y && apt install -y procps').expose(['80','443']).workdir('/opt') |
| 96 | +dockerfile.copy('index.html','/var/www',target) |
| 97 | +dockerfile.show() |
| 98 | +print('-'*60) |
| 99 | + |
| 100 | +neo = Services('netkiller') |
| 101 | +neo.build(dockerfile) |
| 102 | +neo.image("netkiller:1.3.0") |
| 103 | +neo.show() |
| 104 | +print('-'*60) |
| 105 | + |
| 106 | +development = Composes('development') |
| 107 | +development.version('3.9') |
| 108 | +development.services(neo) |
| 109 | +development.show() |
| 110 | + |
| 111 | +print('-'*60) |
| 112 | +development.debug() |
| 113 | +# development.save('/tmp/neo.yaml') |
| 114 | +``` |
| 115 | + |
| 116 | +输出演示 |
| 117 | + |
| 118 | +```text |
| 119 | +FROM nginx:latest AS init |
| 120 | +VOLUME ["/etc/nginx","/var/log/nginx"] |
| 121 | +RUN apt update -y && apt install -y procps |
| 122 | +EXPOSE 80 443 |
| 123 | +WORKDIR /opt |
| 124 | +COPY --from=init index.html /var/www |
| 125 | +------------------------------------------------------------ |
| 126 | +container_name: netkiller |
| 127 | +build: |
| 128 | + context: . |
| 129 | + dockerfile: Dockerfile |
| 130 | + target: init |
| 131 | +image: netkiller:1.3.0 |
| 132 | +
|
| 133 | +------------------------------------------------------------ |
| 134 | +services: |
| 135 | + netkiller: |
| 136 | + container_name: netkiller |
| 137 | + build: |
| 138 | + context: /Users/neo/GitHub/devops/doc/docker |
| 139 | + dockerfile: ./development/netkiller/Dockerfile |
| 140 | + image: netkiller:1.3.0 |
| 141 | +version: '3.9' |
| 142 | +
|
| 143 | +------------------------------------------------------------ |
| 144 | +{'services': {'netkiller': {'container_name': 'netkiller', 'build': {'context': '/Users/neo/GitHub/devops/doc/docker', 'dockerfile': './development/netkiller/Dockerfile'}, 'image': 'netkiller:1.3.0'}}, 'version': '3.9'} |
| 145 | +
|
| 146 | +``` |
0 commit comments