def api_docker_add_service(): r = '0' s_name = request.args.get('sName') c_num = request.args.get('cNum') p_name = request.args.get('pName') if s_name == '' or c_num == '' or p_name == '': return r # 限制最大容器数目 s_port = int(request.args.get('sPort')) if int(c_num) > 40 or int(c_num) < 1: return r if s_name is not None and c_num is not None and int(c_num) > 0: cs = docker.from_env().services mode = types.ServiceMode('replicated', int(c_num)) endpoint = types.EndpointSpec('vip', {s_port: 80}) try: os.mkdir(_WORK_DIR + p_name, 0755) f = open(_WORK_DIR + p_name + '/index.php', 'a+') auto_index = ''' <?php echo 'Project: {}!'; echo '<br/>'; echo 'Docker_iner_IP:' . $_SERVER['SERVER_ADDR']; echo '<br/>'; echo 'Docker_iner_Name:' . $_SERVER['HOSTNAME']; ?> '''.format(p_name) f.write(auto_index) f.close() cs.create(image='richarvey/nginx-php-fpm:latest', name=s_name, mode=mode, mounts=[_WORK_DIR + p_name + ':/var/www/html:rw'], endpoint_spec=endpoint) r = '1' except Exception, e: print e r = '0'
def generate_docker_py_service_description(self, name, docker_networks): mounts = [] for mount_config in self.mounts: mounts.append( types.Mount(target=mount_config['target'], source=mount_config['source'], type=mount_config['type'], read_only=mount_config['readonly'])) configs = [] for config_config in self.configs: configs.append( types.ConfigReference(config_id=config_config['config_id'], config_name=config_config['config_name'], filename=config_config.get('filename'), uid=config_config.get('uid'), gid=config_config.get('gid'), mode=config_config.get('mode'))) secrets = [] for secret_config in self.secrets: secrets.append( types.SecretReference(secret_id=secret_config['secret_id'], secret_name=secret_config['secret_name'], filename=secret_config.get('filename'), uid=secret_config.get('uid'), gid=secret_config.get('gid'), mode=secret_config.get('mode'))) cspec = types.ContainerSpec(image=self.image, user=self.user, dns_config=types.DNSConfig( nameservers=self.dns, search=self.dns_search, options=self.dns_options), args=self.args, env=self.env, tty=self.tty, hostname=self.hostname, labels=self.container_labels, mounts=mounts, secrets=secrets, configs=configs) log_driver = types.DriverConfig(name=self.log_driver, options=self.log_driver_options) placement = types.Placement(constraints=self.constraints) restart_policy = types.RestartPolicy( condition=self.restart_policy, delay=self.restart_policy_delay, max_attempts=self.restart_policy_attempts, window=self.restart_policy_window) resources = types.Resources( cpu_limit=int(self.limit_cpu * 1000000000.0), mem_limit=self.limit_memory, cpu_reservation=int(self.reserve_cpu * 1000000000.0), mem_reservation=self.reserve_memory) update_policy = types.UpdateConfig( parallelism=self.update_parallelism, delay=self.update_delay, failure_action=self.update_failure_action, monitor=self.update_monitor, max_failure_ratio=self.update_max_failure_ratio, order=self.update_order) task_template = types.TaskTemplate(container_spec=cspec, log_driver=log_driver, restart_policy=restart_policy, placement=placement, resources=resources, force_update=self.force_update) if self.mode == 'global': self.replicas = None mode = types.ServiceMode(self.mode, replicas=self.replicas) networks = [] for network_name in self.networks: network_id = None try: network_id = list( filter(lambda n: n['name'] == network_name, docker_networks))[0]['id'] except: pass if network_id: networks.append({'Target': network_id}) else: raise Exception("no docker networks named: %s" % network_name) ports = {} for port in self.publish: ports[int(port['published_port'])] = (int(port['target_port']), port['protocol'], port['mode']) endpoint_spec = types.EndpointSpec(mode=self.endpoint_mode, ports=ports) return update_policy, task_template, networks, endpoint_spec, mode, self.labels
import docker from docker import types client = docker.from_env() if client.swarm.leave(force=True): client.swarm.init() client.services.create("nclcloudcomputing/javabenchmarkapp", name="images", endpoint_spec=types.EndpointSpec(mode="vip", ports={5050: 8080}), mode=types.ServiceMode(mode="replicated", replicas=2)) client.services.create( "dockersamples/visualizer", name="vis", endpoint_spec=types.EndpointSpec(mode="vip", ports={4000: 8080}), mounts=["/var/run/docker.sock:/var/run/docker.sock"]) client.services.create("mongo", name="mongoDB", endpoint_spec=types.EndpointSpec( mode="vip", ports={27017: 27017}), mounts=['db:/data/db'])