简体   繁体   中英

Get absolute path of current .py file in docker container

Im trying to get the absolute path of a.py file to access an other file which is located in the same directory ( /files ). All files are mounted in a docker container as part of a jenkins pipeline. In the.py file I use the following syntax to get the absolute path:

    from pathlib import Path

    current_dir = f"{Path(__file__).resolve().parent}/result.txt"

The file exists in the container but im receiving the following error:

No such file or directory: '/tmp/.tmpAA7qY3/files/result.txt'

The Jenkins file looks like this:

node('docker') {
    docker.image("circleci/python:3.7-buster").inside('--user root:root'+' -v /var/run/docker.sock:/var/run/docker.sock') {
        stage("Scan dependencies") {
            checkout scm
            sh 'poetry run pytest'
        }
    }
}

Jenkins will create a new temporary folder which is called workspace for each specific build instance. Therefore your code is really in /tmp/.tmpAA7qY3 . Since you are running in the tmp folder, it's not strange that the __file__ will resolve to that path.

You need to find out where exactly the file you are searching will be located relative to your temporary folder, or as an absolute path. If you are not in control of how the file was created, easiest is to execute the find / -name result.txt and get the result.

If the file is created in another stage in the Jenkins pipeline flow, the file might not be in the container since each stage can create it's own container as explained here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM