Using python instead of docker compose to compose containers
docker compose is docker's container orchestration tool. It is based on YAML configuration. YAML is a configuration file format that supports the transfer of environment variables, but it is not enough for complex container orchestration.
so I developed this program, which can arrange docker like writing a program, and give full play to the imagination of the program ape.
To get started quickly, first we refer to the docker-compose.yaml script and convert it into a python script.
version: '3.9' services: nginx: container_name: nginx environment: - TZ=Asia/Shanghai extra_hosts: - db.netkiller.cn:127.0.0.1 - cache.netkiller.cn:127.0.0.1 - api.netkiller.cn:127.0.0.1 hostname: www.netkiller.cn image: nginx:latest ports: - 80:80 - 443:443 restart: always volumes: - /tmp:/tmp
After converting to python
from netkiller.docker import * service = Services('nginx') service.image('nginx:latest') service.container_name('nginx') service.restart('always') service.hostname('www.netkiller.cn') service.extra_hosts(['db.netkiller.cn:127.0.0.1','cache.netkiller.cn:127.0.0.1','api.netkiller.cn:127.0.0.1']) service.environment(['TZ=Asia/Shanghai']) service.ports(['80:80','443:443']) service.volumes(['/tmp:/tmp']) # service.debug() # print(service.dump()) compose = Composes('development') compose.version('3.9') compose.services(service) # print (compose.debug()) print(compose.dump()) compose.save()
how about it? It's just another way of writing. It's not difficult. Next, let's learn how to use python to choreograph the docker container
in fact, the program will eventually be converted into a docker compose script for execution. This way of writing is a little more flexible. You can use if, while, linked database, etc. in the program, and you can make more complex container arrangement.
Install dependent Libraries
neo@MacBook-Pro-Neo ~ % pip install netkiller-devops
Confirm whether the installation is successful
neo@MacBook-Pro-Neo ~ % pip show netkiller-devops Name: netkiller-devops Version: 0.2.4 Summary: DevOps of useful deployment and automation Home-page: https://github.com/oscm/devops Author: Neo Chen Author-email: netkiller@msn.com License: BSD Location: /usr/local/lib/python3.9/site-packages Requires: pyttsx3, requests, redis, pyyaml Required-by:
Create a service
from netkiller.docker import * service = Services('nginx') service.image('nginx:latest') service.container_name('nginx') service.restart('always') service.hostname('www.netkiller.cn') service.extra_hosts(['db.netkiller.cn:127.0.0.1','cache.netkiller.cn:127.0.0.1','api.netkiller.cn:127.0.0.1']) service.environment(['TZ=Asia/Shanghai']) service.ports(['80:80','443:443']) service.volumes(['/tmp:/tmp']) # service.debug() print(service.dump())
Operation results
nginx: container_name: nginx environment: - TZ=Asia/Shanghai extra_hosts: - db.netkiller.cn:127.0.0.1 - cache.netkiller.cn:127.0.0.1 - api.netkiller.cn:127.0.0.1 hostname: www.netkiller.cn image: nginx:latest ports: - 80:80 - 443:443 restart: always volumes: - /tmp:/tmp
For a complicated demonstration
for i in range(10) : cluster = Services('nginx-'+str(i)) cluster.image('nginx:latest').container_name('nginx-'+str(i)).restart('always').hostname('www'+str(i)+'.netkiller.cn') cluster.ports(['8{port}:80'.format(port=i)]) print(cluster.dump())
Operation results
nginx-0: container_name: nginx-0 hostname: www0.netkiller.cn image: nginx:latest ports: - 80:80 restart: always nginx-1: container_name: nginx-1 hostname: www1.netkiller.cn image: nginx:latest ports: - 81:80 restart: always nginx-2: container_name: nginx-2 hostname: www2.netkiller.cn image: nginx:latest ports: - 82:80 restart: always nginx-3: container_name: nginx-3 hostname: www3.netkiller.cn image: nginx:latest ports: - 83:80 restart: always nginx-4: container_name: nginx-4 hostname: www4.netkiller.cn image: nginx:latest ports: - 84:80 restart: always nginx-5: container_name: nginx-5 hostname: www5.netkiller.cn image: nginx:latest ports: - 85:80 restart: always nginx-6: container_name: nginx-6 hostname: www6.netkiller.cn image: nginx:latest ports: - 86:80 restart: always nginx-7: container_name: nginx-7 hostname: www7.netkiller.cn image: nginx:latest ports: - 87:80 restart: always nginx-8: container_name: nginx-8 hostname: www8.netkiller.cn image: nginx:latest ports: - 88:80 restart: always nginx-9: container_name: nginx-9 hostname: www9.netkiller.cn image: nginx:latest ports: - 89:80 restart: always
Create compositions
The Services object creates a service, and the compositions object is also required for the service to work.
from netkiller.docker import * service = Services('nginx') service.image('nginx:latest') service.container_name('nginx') service.restart('always') service.hostname('www.netkiller.cn') service.extra_hosts(['db.netkiller.cn:127.0.0.1','cache.netkiller.cn:127.0.0.1','api.netkiller.cn:127.0.0.1']) service.environment(['TZ=Asia/Shanghai']) service.ports(['80:80','443:443']) service.volumes(['/tmp:/tmp']) compose = Composes('development') compose.version('3.9') compose.services(service) # print (compose.debug()) print(compose.dump()) compose.save() # compose.save('/tmp/docker-compose.yaml')
Operation results
services: nginx: container_name: nginx environment: - TZ=Asia/Shanghai extra_hosts: - db.netkiller.cn:127.0.0.1 - cache.netkiller.cn:127.0.0.1 - api.netkiller.cn:127.0.0.1 hostname: www.netkiller.cn image: nginx:latest ports: - 80:80 - 443:443 restart: always volumes: - /tmp:/tmp version: '3.9'
this is already a perfect docker compose script. Use save to save as a yaml file. You can start the container by using docker-compose -f development.yaml up.
The compositions object also carries the perfect docker compose command and parameters for self-management containers.
compose.up() creates a container
compose = Composes('development') compose.version('3.9') compose.services(service) compose.up()
compose.start() starts an existing container
compose = Composes('development') compose.version('3.9') compose.services(service) compose.start()
compose.stop() stops an existing container
compose = Composes('development') compose.version('3.9') compose.services(service) compose.stop()
compose.restart() restarts an existing container
compose = Composes('development') compose.version('3.9') compose.services(service) compose.restart()
compose.rm() destroys an existing container
compose = Composes('development') compose.version('3.9') compose.services(service) compose.rm()
compose.logs() view container logs
compose = Composes('development') compose.version('3.9') compose.services(service) compose.logs()
compose.ps() to view the running status of the container
compose = Composes('development') compose.version('3.9') compose.services(service) compose.ps()
Container management
the Docker object is to get rid of the Docker compose command. It will take over the Docker compose command for self-management.
#!/usr/bin/python3 #-*- coding: utf-8 -*- ############################################## # Home : http://netkiller.github.io # Author: Neo <netkiller@msn.com> # Upgrade: 2021-09-05 ############################################## try: import os, sys module = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0,module) from netkiller.docker import * except ImportError as err: print("%s" %(err)) nginx = Services('nginx') nginx.image('nginx:latest') nginx.container_name('nginx') nginx.restart('always') nginx.hostname('www.netkiller.cn') nginx.environment(['TA=Asia/Shanghai']) nginx.ports(['80:80']) compose = Composes('development') compose.version('3.9') compose.services(nginx) compose.workdir('/tmp/compose') if __name__ == '__main__': try: docker = Docker() docker.environment(compose) docker.main() except KeyboardInterrupt: print ("Crtl+C Pressed. Shutting down.")
Operation results
neo@MacBook-Pro-Neo ~ % python3 docker.py Usage: docker.py [options] up|rm|start|stop|restart|logs|top|images|exec <service> Options: -h, --help show this help message and exit --debug debug mode -d, --daemon run as daemon --logfile=LOGFILE logs file. -l, --list following logging -f, --follow following logging -c, --compose show docker compose -e, --export export docker compose Homepage: http://www.netkiller.cn Author: Neo <netkiller@msn.com>
The Docker object provides parameters equivalent to Docker compose, and its usage is basically the same. for example
python3 docker.py up = docker-compose up python3 docker.py up -d nginx = docker-compose up -d nginx python3 docker.py restart nginx = docker-compose restart nginx python3 docker.py ps = docker-compose ps python3 docker.py logs nginx = docker-compose logs nginx
Use - c to view the compose yaml script, and - e to export docker compose yaml
experience in software testing, interface testing, automation testing, continuous integration and interview. If you are interested, you can go to 806549072. There will be irregular sharing of test data in the group. There will also be technology giants to exchange technology with peers in the industry