'docker build' gives error that 'docker run' doesn't. How are they different?

My project is setup like this:

./ -
  Dockerfile
  package.json
  build
    compiled files from frontend and backend directories get put here
  backend
    app.js
  frontend
    frontend files...
  scripts
    startServer.sh
    build.sh

startServer.sh:

  • error in docker build publish plugin
  • Docker GitHub Automated Build Arguments
  • Golang Mac OSX build for Docker machine
  • How to rebuild go project efficiently while using Docker Compose?
  • building in docker using buildpack-deps, but dependencies don't seem to be installed?
  • Docker: Build your own image issue
  • docker build ../ --tag myImage
    
    # The build script compiles all my assets and places 
    #  them in the top level 'build' directory which i am 
    #  trying to link to my docker image so I can recompile 
    #  on each file change and have the changes show in the docker image.
    ./build.sh
    
    docker run --volume /path/to/build/dir:/src/app myImage
    

    Dockerfile:

    FROM node:4.4.7
    RUN ls src/app
    

    The RUN command in the Dockerfile gives me this error when the build command from the startServer script is called:

    ls: cannot access src/app: No such file or directory
    

    If I change RUN to CMD it gives no error. Also, even after the build gives that error, it finishes the build and the docker run command gives no error.

    1. Is the ‘docker build’ command actually trying to add the ‘build’ folder to the image from which containers are launched? Or is it just compiling some commands for the images to use when they are made?

    2. If it is the later, how do you make one Dockerfile that is used for both building and running that works in both cases?

    I feel like I might be missing a crucial concept with Docker, but I’ve gone through the tutorials and docs and couldn’t solve this.

  • Using values of command line arguments in a file other than entrypoint.sh
  • Docker containers in different directories under one daemon
  • How do i deploy from GitLab CI to Google Container Engine instance using Docker?
  • Liberty Admin Center shows Docker server as running while it is not
  • Dockerfile: Permission denied when trying to install ruby-build
  • Use Assume role for Local Docker Containers
  • 2 Solutions collect form web for “'docker build' gives error that 'docker run' doesn't. How are they different?”

    There is no src/app folder in the node image, so this is an expected error. The node image expects you to add your own /usr/src/app, either with a COPY step in your build, or with a volume mapping after the build is finished.

    The RUN gives a step to run to add a layer to the resulting built image, so an ls makes little sense there since you didn’t modify the image with new content.

    The CMD gives a default command to run if one is not passed at the end of the docker run, so if you do a docker run node /bin/bash, the ls src/app CMD will never be run. This also runs after other steps in your build, and after any volume mounts you may be running on your container, which would create this folder.

    When you run the docker image, you mount data volume to /src/app

    But in docker script, you tried to access src/app
    Because the default working directory is not a root. you cannot access src directory.

    so, edit your docker file to

    FROM node:4.4.7
    RUN ls /src/app
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.