Marvin
Marvin

Reputation: 133

Accessing the shell of a docker container and running multiple commands, always keeping the current shell state

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!

Upvotes: 0

Views: 46

Answers (0)

Related Questions