#!/usr/bin/env python # coding: utf-8 # In[11]: import socket s = socket.socket() # In[12]: s.bind(('127.0.0.1', 0)) # In[14]: s.getsockname() # In[16]: port = s.getsockname()[1] s.close() port # In[17]: import docker # In[33]: client = docker.from_env(version='auto') get_ipython().run_line_magic('pinfo', 'client.containers.run') # In[34]: port # Create the dictionary for docker that forwards our port on the host to the same port in the container # In[35]: port_dict = {'%i/tcp' % port : port} port_dict # Start the container. `detach=True` allows this to return immediately while the container is still running. # In[39]: container = client.containers.run( "jupyter/base-notebook", command=["jupyter", "notebook", "--ip=0.0.0.0", "--port=%i" % port], ports=port_dict, detach=True, remove=True, ) container.attrs # In[47]: print(container.logs().decode('utf8')) # In[53]: import requests r = requests.get("http://localhost:%i/api" % port) r.json() # In[52]: import requests r = requests.get("http://localhost:%i/api/contents/work?token=72f0ac9a4bf481fce1932b2f71b884fd85f5147845be0001" % port) r.json() # After we are done, remove the container # In[55]: container.remove(force=True) # In[ ]: # In[ ]: