What is the correct syntax in a Docker file to mention home directory?

I am using Docker on a Mac.

I have the following line on my Docker file.

COPY ~/.pip/pip.conf /root/.pip/pip.conf

this generates an error

    ERROR [5/6] COPY ~/.pip/pip.conf /root/.pip/pip.conf                                                                                                                                                                                                    0.0s
------
 > [5/6] COPY ~/.pip/pip.conf /root/.pip/pip.conf:
------
failed to compute cache key: “/~/.pip/pip.conf” not found: not found

what is the correct syntax to accomplish the above copy command?


What is the correct syntax in a Docker file to mention home directory?

There is none.

Also, you can't COPY from outside of build context.

Copy the files to the build context before building the container and then COPY them from build context into the container.


You can't access an arbitrary directory with COPY; the source is relative to the context. If you want to add your personal pip.conf file to the Docker image, you need to copy it to the context before running docker build.

With the directive

COPY ./.pip.conf /root/.pip/pip.conf

use something like

cp ~/.pip/pip.conf .
docker build -t someimage .

When you build a docker image, COPY statements can only copy files from inside the 'build context'. Usually the context is the directory containing the Dockerfile. The context is the directory and all directories below it. For security reasons, you're not allowed to copy files from outside the build context.

Because of this, it only makes sense to copy files that you reference with regard to their location within the build context. It doesn't really make sense to reference them relative to the user's home directory, even if that directory is inside the build context.