0 0 Share PDF

'Unknown flag: chown' error when building from Dockerfile

Article ID: KB000876

Issue

Unknown flag: chown error when building from Dockerfile using COPY –-chown flag.

# cat Dockerfile
FROM httpd:2.4
COPY --chown=www-data:www-data ./public-html/ /usr/local/apache2/htdocs/

# docker build -t my-apache2 .
Sending build context to Docker daemon   2.56kB
Step 1/2 : FROM httpd:2.4
2.4: Pulling from library/httpd
f17d81b4b692: Already exists
06fe09255c64: Pull complete
0baf8127507d: Pull complete
07b9730387a3: Pull complete
6dbdee9d6fa5: Pull complete
Digest: sha256:90b34f4370518872de4ac1af696a90d982fe99b0f30c9be994964f49a6e2f421
Status: Downloaded newer image for httpd:2.4
 ---> 55a118e2a010
Step 2/2 : COPY --chown=www-data:www-data ./public-html/ /usr/local/apache2/htdocs/
Unknown flag: chown

Root Cause

COPY –-chown flag implemented in Docker 17.09 and above with the exception of Docker EE 2.0. For additional information please see the Moby issue

Resolution

  1. Remove --chown from the COPY line in the Dockerfile and use it under RUN flag as below:

    # cat Dockerfile
    FROM httpd:2.4
    COPY ./public-html/ /usr/local/apache2/htdocs/
    RUN chown -R www-data:www-data /usr/local/apache2/
    
  2. Build the image:

    # docker build -t my-apache2 .
    Sending build context to Docker daemon   2.56kB
    Step 1/3 : FROM httpd:2.4
    2.4: Pulling from library/httpd
    f17d81b4b692: Already exists
    06fe09255c64: Pull complete
    0baf8127507d: Pull complete
    07b9730387a3: Pull complete
    6dbdee9d6fa5: Pull complete
    Digest: sha256:90b34f4370518872de4ac1af696a90d982fe99b0f30c9be994964f49a6e2f421
    Status: Downloaded newer image for httpd:2.4
    ---> 55a118e2a010
    Step 2/3 : COPY ./public-html/ /usr/local/apache2/htdocs/
    ---> 74c3102638c4
    Removing intermediate container 747563f654ec
    Step 3/3 : RUN chown -R www-data:www-data /usr/local/apache2/
    ---> Running in ca87f421ef37
    ---> 982e5cf4263e
    Removing intermediate container ca87f421ef37
    Successfully built 982e5cf4263e
    Successfully tagged my-apache2:latest
    
  3. Run the docker image and verify the permission:

    # docker run -dit --name my-apache -p 8080:80 my-apache2
    2e506ea97d48eb5b1c13b450d4a132395943f11d4ce19e694d4534c55502431a
    
    # docker exec -it 2e506ea97d48 bash
    root@2e506ea97d48:/usr/local/apache2# ls -ltrh /usr/local/ |grep apache
    drwxr-sr-x 1 www-data www-data 4.0K Oct 24 22:21 apache2