0

As I'm continuing to work in docker-machine and Django, I'm trying to make a setup script for my project that auto-detects platform and decides how to set up Docker and the required containers. Auto-detection works fine. One thing I can't figure out is how to automatically set the environment variables needed for docker-machine to work on Mac OS X. Currently, the script will just tell the user to manually set the environment variable using the command

eval $(docker-machine env dev)

where dev is the name of the VM. This prompt happens after initial setup is successfully completed. The user is told to do this because the following subprocess call does not actually set the environment variables:

subprocess.call('eval $(docker-machine env dev)', shell=True)

If an error occurs during creating the VM because the VM already exists, then I use subprocess to see if Docker is already installed:

check_docker = subprocess.check_call('docker run hello-world', shell=True)

If this call is successful, then the script tells the user that Docker was already installed and then prompts the user to manually set the environment variables to be able to start the containers needed for the Django server to run. I had originally thought that the script behaved correctly in this scenario, but it turns out that it only appeared that way because I had already set the environment variables manually. Of course, I see now that the docker run command needs the environment variables to be set in order to work, and since the environment variables never get set in the script, the docker run test doesn't work. So, how am I supposed to correctly set the environment variables from Python? It seems like using subprocess is resulting in the wrong environment getting these variables set. If I do something like

subprocess.call('setdockerenv.sh', shell=True)

where setdockerenv.sh has the correct eval command, then I run into the same problem, which I'm guessing is rooted in using subprocess. Would os have something to do this properly where subprocess can't? It's important that I do this in the Python script, or else having the user manually set the environment variables and then manually test to see if docker is installed defeats the purpose of having the script.

2
  • You can't modify the environment of the current process using another process; eval works because as a shell built-in, it doesn't fork another process, but executes in the current process. There is no way to modify the environment of your current shell by running a Python script. Commented Aug 18, 2015 at 18:11
  • Apologies for responding to this late, but yes, that was the problem. Could you make that into an answer? Also, I was able to solve my problem by using os.environ and manually setting the environment variables that way. I think what this does is set the env variables for the Python process running the script, so that child subprocesses can use those variables. Commented Aug 21, 2015 at 15:49

1 Answer 1

1

You cannot use subprocess to change the environment, since any changes it makes are local to that process. Instead, (as you found) you can change your current environment via os.environ, and that is inherited by any other processes you subsequently create.

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

Comments

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.