0

I'm trying to convert the following docker run command to python docker run:

docker run -v ${HOME}/mypath/somepath:/root/mypath/somepath:ro -v /tmp/report/:/root/report -e MY_VAR=fooname DOCKER_IMAGE

and this is what I have so far:

client = docker.from_env()
client.containers.run(DOCKER_IMAGE, 'MY_VAR=fooname', volumes={
    f'{home}/mypath/somepath': {'bind': '/root/mypath/somepath', 'mode': 'ro'},
    '/tmp/report': {'bind': '/root/report', 'mode': 'rw'},
},)

But it seems like I'm running into issues when passing the env variables

docker.errors.APIError: 500 Server Error: Internal Server Error ("OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"MY_VAR=fooname\": executable file not found in $PATH": unknown")

What's the right way to pass the env variables?

EDIT

After changing it to

client.containers.run(DOCKER_IMAGE, None, environment=['MY_VAR=fooname'], volumes={
    f'{home}/mypath/somepath': {'bind': '/root/mypath/somepath', 'mode': 'ro'},
    '/tmp/report': {'bind': '/root/report', 'mode': 'rw'},
},)

I'm getting this error instead: docker.errors.ContainerError: Command 'None' in image

The docker build file has the command declared to just run a python script.

1 Answer 1

0

The second parameter of the run() method is the command, not the environment. If you don't have a command then pass None.

According to the documentation the environment must be either a dict or a list, so in your case:

client.containers.run(DOCKER_IMAGE, None, environment=['MY_VAR=fooname'], ...

Docs: https://docker-py.readthedocs.io/en/stable/containers.html#docker.models.containers.ContainerCollection.run

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm I'm running into docker.errors.ContainerError: Command 'None' in image '{DOCKER_IMAGE}' returned non-zero exit status 1
@Stupid.Fat.Cat weird, maybe are you passing None as a string? Looking at the function signature it shows that it will default to None if no command is passed so it shouldn't require one.
There must've been a typo somewhere, I rewrote the run command and it started working :) Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.