3

I'm trying to run a shell command with docker-py on an already-running container, but get an error:

exec: "export": executable file not found in $PATH

here's how I wrote the script:

exe = client.exec_create(container=my_container, cmd='export MYENV=1')
res = client.exec_start(exec_id=exe)

so my question is how can I run a shell command (inside the container) using docker-py?

Mahyar
  • 1,011
  • 2
  • 17
  • 37
  • What are you trying to achieve with this? Even if it did work (you probably need to prefix with `bash -c `) then the variable would be exported in the child process, but no where else. Maybe use `os.environ[MYENV] = '1'`? – cdarke Sep 18 '16 at 18:00
  • Not sure how to use `os.environ[MYENV]` inside the container here? `export` was just an example, I want to run a shell command inside the container. I finally need to set an env variable inside the container and run psql inside the container (then obviously run some queries). but first step is to run the shell command itself inside the container using docker-py. Also not sure how to prefix `bash -c` via docker-py and `exec_create` – Mahyar Sep 18 '16 at 18:09
  • 2
    `cmd='bash -c "export MYENV=1"'` – cdarke Sep 18 '16 at 18:13
  • If you have control over starting the container, you can use the `environment` parameter of `docker.client.containers.run` to set environment variables. – jkr May 08 '20 at 02:07

2 Answers2

1

You did it quite right. But you confused shell commands with linux executables. exec_create and and exec_start are all about running executables. Like for example bash. export in your example is a shell command. You can only use it in a shell like bash running inside the container.

Additionally what you are trying to achieve (setting a environment variable) is not going to work. As soon as your exec finishes (where you set the env var) the exec process will finish and its environment is been torn down.

You can only create global container environment variables upon creation of a container. If you want to change the env vars, you have to tear down the container and recreate it with your new vars. As you probably know, all data in the container is lost upon a remove unless you use volumes to store your data. Reconnect the volumes on container creation.

That said your example was nearly correct. This should work and create an empty /somefile.

exe = client.exec_create(container=my_container, cmd=['touch', '/somefile'])
res = client.exec_start(exec_id=exe)

To execute shell commands, use this example. It calls sh and tells it to run the interpreter on the given command string (-c)

exe = client.exec_create(container=my_container, 
                         cmd=['/bin/sh', '-c', 'touch /somefile && mv /somefile /bla'])
res = client.exec_start(exec_id=exe)
joanis
  • 10,635
  • 14
  • 30
  • 40
itsafire
  • 5,607
  • 3
  • 37
  • 48
0

For actually , when execute cmd docker exec in docker container export MYENV=1. It will fail and report this error

exec: "export": executable file not found in $PATH

Because export is a shell builtin, could run the cmd in shell.

whereis export
type export

can not find export in /usr/bin/ or somewhere else.

There is some ways to pass through this problem.

case1: use -c parameter

/bin/bash -c 'export MYENV=1 ; /bin/bash'

case2: append export cmds to a rcfile, then use this file.

echo "exprot MYENV=1" >> <some_file_path> ; /bin/bash --rcfile <some_file_path>

case3: open a terminal, then enter the cmds to export env parameters , then open a new terminal, the env parameters will work.

/bin/bash
exprot MYENV=1
/bin/bash # open a new terminal
ica10888
  • 81
  • 1
  • 4