Run commands through docker.py

I am trying to run commands through a Docker container. I am able to successfully create and print the containers logs to console however, I want to to be able to run commands once connected to the container. I am using Flask as I am creating a web based console for managing the container. I have it connect to to the container but want to send commands once connected and not send a command as the container is being created.

client.containers.run("ubuntu:latest", "echo hello world") will create the container and echo hello world but I am wanting it so I am able to send commands once the container is already created. I am using the official docker.py lib from Docker.

@DockerDeploy.route('/dashboard')
def dashboard():
    container = client.containers.run('ubuntu:latest', detach=True)
    for line in container.logs(stream=True):
        print(line.strip())
    return "<h1>Container Created!</h1>"

1 answer

  • answered 2019-02-10 20:21 Martin do santos

    docker.py Documentation has the answer: Docker.py Execute

    Since execute is deprecated, you have to use exec_create and exec_start.

    So, you have to use docker.py to get the Id of the container, but it should be enough.

    Hope it helps.