Search code examples
dockerdocker-composedockerfilecontainers

Docker: error copying file from host to container-file not found


I am trying to copy a file from my host (Ubuntu 18.04) to a python container but I am having trouble copying it over. The purpose is to take my specific configuration for my default sound device and transfer it over so I don't have to do it manually later.

I've tried several variations and I am wondering if I am misunderstanding how to copy files over. I've checked the documentation and other various questions people on stackoverflow have asked regarding this like this one:

How to copy file from host to container using Dockerfile

But I get an error when I try the traditional command:

docker-compose build 
...
Step 7/7 : COPY /config/.asoundrc ~/.asoundrc
COPY failed: file not found in build context or excluded by .dockerignore: stat config/.asoundrc: file does not exist

I don't have a docker ignore file in my root directory. My directory looks like this:

dejavu (root name)
-config
--.asoundrc
-docker-compose.yaml
...

To be clear, I want to copy the file config/.asoundrc from my host to the container image, specifically the directory destination as ~/.asoundrc

Here is my dockerfile:

FROM python:3.7
RUN apt-get update -y && apt-get upgrade -y
RUN apt-get install \
    gcc nano \
    gdebi alsa-utils usbutils ffmpeg libasound-dev portaudio19-dev libportaudio2 libportaudiocpp0 \
    postgresql postgresql-contrib pulseaudio -y
RUN pip install numpy scipy matplotlib pydub pyaudio psycopg2 sounddevice

WORKDIR /code
COPY /config/.asoundrc ~/.asoundrc

Here is the original repo, it's currently being modified from an open source project: https://github.com/datafaust/dejavu


Solution

  • I believe you have two issues in your COPY command:

    1. Your source path starts with a slash, it shouldn't
    2. Your destination path contains a ~, which will not be converted to the home directory, as it does in a shell script.

    Try this:

    COPY config/.asoundrc /root/.asoundrc
    

    In addition, after reviewing your docker-compose.yml, it is clear that the main problem is there.

    Your docker-compose file defines a build context:

    services:
      python:
        build:
          context: ./docker/python
    

    Which means this is the directory that all COPY commands are relative to. Since this directory only contains the Dockerfile, and not the requested config/.asoundrc file, the build failed with a clear error.

    You should place each Dockerfile in the same directory as the application it builds. So, to fix it:

    • Move the Dockerfile from docker to dejavu.
    • Change the build context in docker-compose.yml from ./docker/python to dejavu (also note, no leading dot needed).