Copying files in a directory to a docker container

2019-07-16 16:28发布

问题:

I am trying to copy content of a directory while creating the docker image in the Dockerfile but while copying its giving me error-

COPY failed: stat /var/lib/docker/tmp/docker-builder108255131/Users/user_1/media : no such file or directory

I have the following file in the Dockerfile-

COPY /Users/user_1/media/. /code/project_media/

The media directory is in a seperate directory level then Dockerfile.

回答1:

Sorry per dockerfile documentation:

Multiple resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build

https://docs.docker.com/engine/reference/builder/#copy

I usually will have a script for building the docker, and in that script will copy the files need for the COPY command into either the same directory or sub-directory of the "context of the build" (a very confusing way of saying the same directory as the dockerfile is in)

DOCKER_BUILD_DIR="./build/docker"
DOCKERFILE="./docker/Dockerfile"
MEDIA_FILES="/Users/user_1/media"

mkdir -p "${DOCKER_BUILD_DIR}/${MEDIA_FILES}"
cp -f "${DOCKERFILE}" "${DOCKER_BUILD_DIR}/"
cp -rf "${MEDIA_FILES}" "${DOCKER_BUILD_DIR}/${MEDIA_FILES}/.."

docker build "${DOCKER_BUILD_DIR}"


标签: docker