Home > Mobile >  WORKDIR $HOME cannot normalize nothing
WORKDIR $HOME cannot normalize nothing

Time:04-21

I'm new to docker. I've a very simple Dockerfile but when I try to build an image from this Dockerfile, i get "cannot normalize nothing" error.

FROM alpine:latest
RUN apk add vim
CMD ["bash"]
WORKDIR $HOME

For building the image, I use the following command:

$ docker build -t aamirglb/test:1.0 .

I used Play With Docker (PWD) to test this script and here is the output.

pwd output

If I comment out WORKDIR $HOME line, the build is successful.

Any help in understanding this error will be highly appreciated.

Thanks in advance.

CodePudding user response:

For those that can't reproduce, this error does not happen with buildkit:

$ docker build -t test-workdir -f df.workdir-empty .
[ ] Building 0.2s (6/6) FINISHED                                                                                                                                                                                   
 => [internal] load build definition from df.workdir-empty                                                                                                                                                    0.0s
 => => transferring dockerfile: 43B                                                                                                                                                                           0.0s
 => [internal] load .dockerignore                                                                                                                                                                             0.0s
 => => transferring context: 34B                                                                                                                                                                              0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                                                              0.0s
 => [1/3] FROM docker.io/library/alpine:latest                                                                                                                                                                0.0s
 => CACHED [2/3] RUN apk add vim                                                                                                                                                                              0.0s
 => exporting to image                                                                                                                                                                                        0.0s
 => => exporting layers                                                                                                                                                                                       0.0s
 => => writing image sha256:cadafef9a77c95b1b32499bcc1beba016ff0a7071710a1c37eb4e5f32e5d1c94                                                                                                                  0.0s
 => => naming to docker.io/library/test-workdir                                                                                                                                                               0.0s

But if you build with the classic builder, the error does appear:

$ DOCKER_BUILDKIT=0 docker build -t test-workdir -f df.workdir-empty .                                    
Sending build context to Docker daemon  23.04kB                                                          
Step 1/4 : FROM alpine:latest                                                                            
 ---> 14119a10abf4
Step 2/4 : RUN apk add vim
 ---> Running in a286e7f3107a
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/5) Installing xxd (8.2.4708-r0)
(2/5) Installing lua5.3-libs (5.3.6-r0)
(3/5) Installing ncurses-terminfo-base (6.2_p20210612-r0)
(4/5) Installing ncurses-libs (6.2_p20210612-r0)                                                         
(5/5) Installing vim (8.2.4708-r0)
Executing busybox-1.33.1-r3.trigger
OK: 26 MiB in 19 packages
Removing intermediate container a286e7f3107a
 ---> acdd6e1963db
Step 3/4 : CMD ["bash"]
 ---> Running in 6deb306db96a
Removing intermediate container 6deb306db96a
 ---> 960f0de2f376
Step 4/4 : WORKDIR $HOME
cannot normalize nothing

And before you get your hopes up, workdir in the buildkit example is not set to the user's home directory:

$ docker inspect test-workdir:latest --format '{{.Config.WorkingDir}}'
/

So first, what does cannot normalize nothing mean? Normalizing is the process of taking a path like /.//some/../path and turning that into /path, cleaning the string of unnecessary content and converting it into a uniform path. That process doesn't work on nothing, it needs a string or value, or at least the classic builder does (buildkit seems to have a sensible default).

Why doesn't $HOME default to /root? Because that variable was never set in docker. The only environment variable defined in there is a path:

$ docker inspect test-workdir:latest --format '{{.Config.Env}}'
[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]

When you launch a shell like /bin/sh in the container, that shell is what is defining $HOME and various other variables:

$ docker run -it --rm test-workdir /bin/sh
/ # env
HOSTNAME=c68d4370e413
SHLVL=1
HOME=/root
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/

Since there was no ENV or ARG to set $HOME, you cannot use it in the WORKDIR step.

  • Related