What is the docker RUN instruction in the dockerfile?



  • Docker documentation: https://docs.docker.com/engine/reference/builder/

    The RUN instruction has two forms:

    The shell form is like running a command on the shell prompt. The default shell can be change using the SHELL command. The shell command uses the \ backslash to continue a shell command on the next line, same as shell scripts, or shell prompt.

    The exec form execute commands on top of the current image and commit the results, The resulting image will be used for the next step in the dockerfile. The exec form allow you to run executables for instance, when the base image does not contain the executable.

    The exec form is parsed in JSON, use double quotes around words. The exec form does not interpret shell variables like the shell form as the shell command is never invoked, to get the exec form to honour shell variables, the command shell needs to be invoked first, followed by the variable.

    Example Dockerfile using the shell form

    FROM alpine:latest
    
    RUN uptime
    

    Example Dockerfile using the exec form, using the JSON syntax.

    FROM alpine:latest
    
    RUN [ "echo", "home" ]
    

    If we changes "home" to "$HOME" in the exec form is would just print $HOME and substitute the variable as it does in the shell form.

    To do that in the exec form use this syntax:

    FROM alpine:latest
    
    RUN [ "sh", "-c", "echo $HOME" ]
    


© Lightnetics 2024