0

I am currently creating a wrapper kernel for Juptyer Notebook that can interpret Dockerfiles. Do do this I am building a new image after each executed cell and storing the resulting image id. I am using the Low Level API docker provides.

By now I was able to execute single commands by doing the following:

The image only consists of the following:

FROM ubuntu:latest
#sleep infinity to keep the container from exiting
container = api.create_container(image_id, command="sleep infinity") 
api.start(container=container)

def execute_command(command):
    exec_info = api.exec_create(container=container, cmd=command)
    response = api.exec_start(exec_id=exec_info.get('Id')).decode()
    print(response)

If I now call something like execute_command("ls") it works and prints out the directories. If I now want to change the current directory I need to call execute_command("/bin/bash -c cd dev"). If I now repeat the ls command I get the same output as before. How can I manage to send multiple commands (one after another, not knowing all commands beforehand) and always keeping the current state of the shell? Basically "saving" the shell as a variable and sending my commands to it.

I've read something about attaching to the docker container via api.attach() but I'm not sure if this leads to my goal.

I hope I made myself clear on what I want to achieve!

Marvin34565
  • 71
  • 1
  • 6
  • 1
    Have you considered how you might do this without Docker? (`subprocess.call('cd ...')` will similarly be a no-op, for example.) It sounds like you're trying to reimplement `docker build`, would just running that make more sense? – David Maze Jun 05 '23 at 10:56
  • You made a good point there. It might not be exactly what you meant but you got me the right idea: I just spin up a terminal session in jupyter lab and execute the commands to interactively [interact](https://stackoverflow.com/questions/30172605/how-do-i-get-into-a-docker-containers-shell) with the container's shell. This also removes the need to implement the terminal myself in the wrapper kernel – Marvin34565 Jun 05 '23 at 11:09

0 Answers0