Docker - how can I copy a file from an image to a host?

前端 未结 8 683
庸人自扰
庸人自扰 2020-12-02 05:56

My question is related to this question on copying files from containers to hosts; I have a Dockerfile that fetches dependencies, compiles a build artifact from source, and

相关标签:
8条回答
  • 2020-12-02 07:01
    docker cp $(docker create --rm registry.example.com/ansible-base:latest):/home/ansible/.ssh/id_rsa ./hacked_ssh_key
    

    wanted to supply a one line solution based on pure docker functionality (no bash needed)

    edit: container does not even has to be run in this solution

    edit2: thanks to @Jonathan Dumaine for --rm so the container will be removed after, i just never tried, because it sounded illogical to copy something from somewhere which has been already removed by the previous command, but i tried it and it works

    0 讨论(0)
  • 2020-12-02 07:01

    You could bind a local path on the host to a path on the container, and then cp the desired file(s) to that path at the end of your script.

    $ docker run -d \
      -it \
      --name devtest \
      --mount type=bind,source="$(pwd)"/target,target=/app \
      nginx:latest
    

    Then there is no need to copy afterwards.

    0 讨论(0)
提交回复
热议问题