Copying files from Docker container to host

后端 未结 18 2238
小蘑菇
小蘑菇 2020-11-22 13:39

I\'m thinking of using Docker to build my dependencies on a Continuous Integration (CI) server, so that I don\'t have to install all the runtimes and libraries on the agents

相关标签:
18条回答
  • 2020-11-22 13:57

    In order to copy a file from a container to the host, you can use the command

    docker cp <containerId>:/file/path/within/container /host/path/target
    

    Here's an example:

    $ sudo docker cp goofy_roentgen:/out_read.jpg .
    

    Here goofy_roentgen is the container name I got from the following command:

    $ sudo docker ps
    
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                            NAMES
    1b4ad9311e93        bamos/openface      "/bin/bash"         33 minutes ago      Up 33 minutes       0.0.0.0:8000->8000/tcp, 0.0.0.0:9000->9000/tcp   goofy_roentgen
    

    You can also use (part of) the Container ID. The following command is equivalent to the first

    $ sudo docker cp 1b4a:/out_read.jpg .
    
    0 讨论(0)
  • docker run -dit --rm IMAGE
    docker cp CONTAINER:SRC_PATH DEST_PATH
    

    https://docs.docker.com/engine/reference/commandline/run/ https://docs.docker.com/engine/reference/commandline/cp/

    0 讨论(0)
  • 2020-11-22 14:00

    Mount a volume, copy the artifacts, adjust owner id and group id:

    mkdir artifacts
    docker run -i --rm -v ${PWD}/artifacts:/mnt/artifacts centos:6 /bin/bash << COMMANDS
    ls -la > /mnt/artifacts/ls.txt
    echo Changing owner from \$(id -u):\$(id -g) to $(id -u):$(id -g)
    chown -R $(id -u):$(id -g) /mnt/artifacts
    COMMANDS
    

    EDIT: Note that some of the commands like $(id -u) are backslashed and will therefore be processed within the container, while the ones that are not backslashed will be processed by the shell being run in the host machine BEFORE the commands are sent to the container.

    0 讨论(0)
  • 2020-11-22 14:00

    With the release of Docker 19.03, you can skip creating the container and even building an image. There's an option with BuildKit based builds to change the output destination. You can use this to write the results of the build to your local directory rather than into an image. E.g. here's a build of a go binary:

    $ ls
    Dockerfile  go.mod  main.go
    
    $ cat Dockerfile
    FROM golang:1.12-alpine as dev
    RUN apk add --no-cache git ca-certificates
    RUN adduser -D appuser
    WORKDIR /src
    COPY . /src/
    CMD CGO_ENABLED=0 go build -o app . && ./app
    
    FROM dev as build
    RUN CGO_ENABLED=0 go build -o app .
    USER appuser
    CMD [ "./app" ]
    
    FROM scratch as release
    COPY --from=build /etc/passwd /etc/group /etc/
    COPY --from=build /src/app /app
    USER appuser
    CMD [ "/app" ]
    
    FROM scratch as artifact
    COPY --from=build /src/app /app
    
    FROM release
    

    From the above Dockerfile, I'm building the artifact stage that only includes the files I want to export. And the newly introduced --output flag lets me write those to a local directory instead of an image. This needs to be performed with the BuildKit engine that ships with 19.03:

    $ DOCKER_BUILDKIT=1 docker build --target artifact --output type=local,dest=. .
    [+] Building 43.5s (12/12) FINISHED
     => [internal] load build definition from Dockerfile                                                                              0.7s
     => => transferring dockerfile: 572B                                                                                              0.0s
     => [internal] load .dockerignore                                                                                                 0.5s
     => => transferring context: 2B                                                                                                   0.0s
     => [internal] load metadata for docker.io/library/golang:1.12-alpine                                                             0.9s
     => [dev 1/5] FROM docker.io/library/golang:1.12-alpine@sha256:50deab916cce57a792cd88af3479d127a9ec571692a1a9c22109532c0d0499a0  22.5s
     => => resolve docker.io/library/golang:1.12-alpine@sha256:50deab916cce57a792cd88af3479d127a9ec571692a1a9c22109532c0d0499a0       0.0s
     => => sha256:1ec62c064901392a6722bb47a377c01a381f4482b1ce094b6d28682b6b6279fd 155B / 155B                                        0.3s
     => => sha256:50deab916cce57a792cd88af3479d127a9ec571692a1a9c22109532c0d0499a0 1.65kB / 1.65kB                                    0.0s
     => => sha256:2ecd820bec717ec5a8cdc2a1ae04887ed9b46c996f515abc481cac43a12628da 1.36kB / 1.36kB                                    0.0s
     => => sha256:6a17089e5a3afc489e5b6c118cd46eda66b2d5361f309d8d4b0dcac268a47b13 3.81kB / 3.81kB                                    0.0s
     => => sha256:89d9c30c1d48bac627e5c6cb0d1ed1eec28e7dbdfbcc04712e4c79c0f83faf17 2.79MB / 2.79MB                                    0.6s
     => => sha256:8ef94372a977c02d425f12c8cbda5416e372b7a869a6c2b20342c589dba3eae5 301.72kB / 301.72kB                                0.4s
     => => sha256:025f14a3d97f92c07a07446e7ea8933b86068d00da9e252cf3277e9347b6fe69 125.33MB / 125.33MB                               13.7s
     => => sha256:7047deb9704134ff71c99791be3f6474bb45bc3971dde9257ef9186d7cb156db 125B / 125B                                        0.8s
     => => extracting sha256:89d9c30c1d48bac627e5c6cb0d1ed1eec28e7dbdfbcc04712e4c79c0f83faf17                                         0.2s
     => => extracting sha256:8ef94372a977c02d425f12c8cbda5416e372b7a869a6c2b20342c589dba3eae5                                         0.1s
     => => extracting sha256:1ec62c064901392a6722bb47a377c01a381f4482b1ce094b6d28682b6b6279fd                                         0.0s
     => => extracting sha256:025f14a3d97f92c07a07446e7ea8933b86068d00da9e252cf3277e9347b6fe69                                         5.2s
     => => extracting sha256:7047deb9704134ff71c99791be3f6474bb45bc3971dde9257ef9186d7cb156db                                         0.0s
     => [internal] load build context                                                                                                 0.3s
     => => transferring context: 2.11kB                                                                                               0.0s
     => [dev 2/5] RUN apk add --no-cache git ca-certificates                                                                          3.8s
     => [dev 3/5] RUN adduser -D appuser                                                                                              1.7s
     => [dev 4/5] WORKDIR /src                                                                                                        0.5s
     => [dev 5/5] COPY . /src/                                                                                                        0.4s
     => [build 1/1] RUN CGO_ENABLED=0 go build -o app .                                                                              11.6s
     => [artifact 1/1] COPY --from=build /src/app /app                                                                                0.5s
     => exporting to client                                                                                                           0.1s
     => => copying files 10.00MB                                                                                                      0.1s
    

    After the build was complete the app binary was exported:

    $ ls
    Dockerfile  app  go.mod  main.go
    
    $ ./app
    Ready to receive requests on port 8080
    

    Docker has other options to the --output flag documented in their upstream BuildKit repo: https://github.com/moby/buildkit#output

    0 讨论(0)
  • 2020-11-22 14:03

    You do not need to use docker run.

    You can do it with docker create.

    From the docs:

    The docker create command creates a writeable container layer over the specified image and prepares it for running the specified command. The container ID is then printed to STDOUT. This is similar to docker run -d except the container is never started.

    So, you can do:

    docker create -ti --name dummy IMAGE_NAME bash
    docker cp dummy:/path/to/file /dest/to/file
    docker rm -f dummy
    

    Here, you never start the container. That looked beneficial to me.

    0 讨论(0)
  • 2020-11-22 14:03

    I used PowerShell (Admin) with this command.

    docker cp {container id}:{container path}/error.html  C:\\error.html
    

    Example

    docker cp ff3a6608467d:/var/www/app/error.html  C:\\error.html
    
    0 讨论(0)
提交回复
热议问题