dockerenvironment-variablesbuildout

Docker: How to create an environment variable in the host machine that points to a directory in a docker container?


I am using Docker to run four containers to run a backend web application. The backend web application uses buildout to assemble the software.

However, the frontend, which is installed and runs on the host machine (that is, not using Docker), needs to access the buildout directory inside one of the four docker containers.

Moreover, the frontend uses an environment variable called NTI_BUILDOUT_PATH that is defined on the host machine. NTI_BUILDOUT_PATH must point to the buildout directory, which is inside the aforementioned container.

My problem is that I do not know how to define NTI_BUILDOUT_PATH such that it contains a directory that points towards the buildout directory that is needed by the front end for SSL certificates and other purposes.

I have researched around the web and read about volumes and bind mounts but I do not think they can help me in my case.


Solution

  • One way you can do that is by copying your buildout folder into the host machine using docker cp

    docker cp <backend-container-id>:<path-to-buildout> <path-to-host-folder>
    

    For Example if your backend's container_id is d1b5365c5bca and your buildout folder is in /app/buildout inside the container. You can use the following command to copy it to the host.

    docker cp d1b5365c5bca:/app/buildout /home/mahmoud/app/buildout
    

    After that you docker rm all your containers and recreate new ones with a bind mount to the buildout folder in the host. So following the previous example we'll have

    docker run -v /home/mahmoud/app/buildout:/app/buildout your-backend-image
    docker run -v /home/mahmoud/app/buildout:/app/buildout -e NTI_BUILDOUT_PATH=/app/buildout your-frontend-image