2016-10-12 8 views
2

에 사본을 사용하여 호스트합니다. 도커 컨테이너에서 호스트 컴퓨터로 파일을 복사하고 싶습니다. 고정 표시기-PY 문서에서내가 고정 표시기-평을 사용하고 고정 표시기 - 평

:

copy 

Identical to the docker cp command. Get files/folders from the container. 

Params: 

    container (str): The container to copy from 
    resource (str): The path within the container 

Returns (str): The contents of the file as a string 

내가 컨테이너를 만들고 시작하지만 호스트 컨테이너에서 복사됩니다 파일을 얻을 수 없습니다 수 있습니다. 내가 누락 된 부분을 지적하면 누군가 나를 도울 수 있습니까? 호스트에 복사를 시도한 나의 도커 컨테이너에 /mydir/myshell.sh가 있습니다. 누군가가 나에게 그것을 복사하거나 파일을 복사 여부를 알아내는 데 도움이 경우

>>> a = c.copy(container="7eb334c512c57d37e38161ab7aad014ebaf6a622e4b8c868d7a666e1d855d217", resource="/mydir/myshell.sh") >>> a 
<requests.packages.urllib3.response.HTTPResponse object at 0x7f2f2aa57050> 
>>> type(a) 
<class 'requests.packages.urllib3.response.HTTPResponse'> 

매우 도움이 될 것입니다.

+0

가? –

답변

3

: 여기

는 컨테이너에 파일을 복사하는 방법에 대한 코드입니다 아티팩트 폴더. 이 어떤 진행

+0

좋은 해결 방법. – npatel

6

copy는 고정 표시기에 사용되지 않는 방법이며 바람직한 방법 put_archive 방법을 사용하는 것이다. 그래서 기본적으로 우리는 아카이브를 생성하고 그것을 컨테이너에 넣어야합니다. 이상하게 들리지만 API가 현재 지원하는 것입니다. 당신이 나 같은이가 개선 될 수 있다고 생각하면 문제/기능 요청을 열어 주시기 나는 그것을 upvote에 있습니다. 내가 그렇게 내 파일에 고정 표시기 실행에서 생성 된 얻을 수있는 docker run -it -v artifacts:/artifacts target-build로 고정 표시기를 실행하는 전화를 추가 내 파이썬 스크립트에서

def copy_to_container(container_id, artifact_file): 
    with create_archive(artifact_file) as archive: 
     cli.put_archive(container=container_id, path='/tmp', data=archive) 

def create_archive(artifact_file): 
    pw_tarstream = BytesIO() 
    pw_tar = tarfile.TarFile(fileobj=pw_tarstream, mode='w') 
    file_data = open(artifact_file, 'r').read() 
    tarinfo = tarfile.TarInfo(name=artifact_file) 
    tarinfo.size = len(file_data) 
    tarinfo.mtime = time.time() 
    # tarinfo.mode = 0600 
    pw_tar.addfile(tarinfo, BytesIO(file_data)) 
    pw_tar.close() 
    pw_tarstream.seek(0) 
    return pw_tarstream 
+0

나는 볼륨 옵션을 함께 할 수 있었다. 다른 제안을 주셔서 감사합니다. 좋은 데요 –

+0

@Sweety. 코드를 게시하는 것이 친절한가요? –