Inside our test application, create a new file called Dockerfile (capital D, no extension):
FROM node
ENV MONGO_DB_USERNAME=admin \
MONGO_DB_PASSWORD=qwerty
RUN mkdir -p /home/app
COPY . /home/app
CMD ["node", "/home/app/server.js"]Since we are inside the test-app folder during development and can run:
node server.jsdirectly, everything works locally.
But when the Docker container runs, we will be at the root filesystem level, where no server.js exists in the same location.
That is why we write the complete path:
/home/app/server.jsSo Docker knows exactly where the file is inside the container.
To build a Docker image from the Dockerfile, run:
docker build -t test-app:1.0 .-t → tag (image name + version)test-app:1.0 → image name and version. → current directory (where Dockerfile exists)Run this command inside the test-app folder because that is where the Dockerfile is located.
Verify the image:
docker imagesNow you will see:
test-app:1.0mongomongo-expressdocker run test-app:1.0You will see:
Server running on port 5050This means:
docker run -it test-app:1.0 /bin/bashOnce inside, if we run:
lsWe will see that:
test-app folder and all files have been successfully copied into the containerdocker build creates an image from the Dockerfiledocker run starts a container from the image/home/app inside the containerCMD