hello-dockapplication resides inside the sub-directory with the same name.
package.jsonfile into the image.
vitedevelopment server by executing
npm run devcommand.
Dockerfile.dev, the file should look like as follows:
lts-alpinetag indicates that you want to use the Alpine variant, long term support version of the image. Available tags and necessary documentation for the image can be found on node hub page.
USERinstruction sets the default user for the image to
node. By default Docker runs containers as the root user and according to Docker and Node.js Best Practices this can pose a security threat. So a better idea is to run as a non-root user whenever possible. The node image comes with a non-root user named
nodewhich you can set as the default user using the
RUN mkdir -p /home/node/appinstruction creates a directory called
appinside the home directory of the
nodeuser. The home directory for any non-root user in Linux is usually
/home/<user name>by default.
WORKDIRinstruction sets the default working directory to the newly created
/home/node/appdirectory. By default the working directory of any image is the root. You don't want any unnecessary files sprayed all over your root directory, do you? Hence you change the default working directory to something more sensible like
/home/node/appor whatever you like. This working directory will be applicable to any subsequent
COPYinstruction here copies the
package.jsonfile which contains information regarding all the necessary dependencies for this application. The
npm installcommand which is the default command for installing dependencies using a
package.jsonfile in Node.js projects. The
.at the end represents the working directory.
COPYinstruction copies rest of the content from the current directory (
.) of the host filesystem to the working directory (
.) inside the image.
CMDinstruction here sets the default command for this image which is
npm run devwritten in
vitedevelopment server by default runs on port
3000hence adding an
EXPOSEcommand seemed like a good idea, so there you go.
Dockerfile.devyou can execute the following command:
Dockerfileyou have to explicitly pass the filename using the
--fileoption. A container can be run using this image by executing the following command:
http://127.0.0.1:3000to see the
hello-dockapplication in action.
vitedevelopment server. Changes made to the file system inside the container will reflect on your local file system as well.
-voption for the
container startcommands. Just to remind you, the generic syntax is as follows:
hello-dock-devcontainer, and start a new container by executing the following command:
--detachoption and that's to demonstrate a very important point. As you can see that the application is not running at all now.
node_modulesdirectory on the project root.
node_modulesdirectory containing all the dependencies. Hence the
vitepackage goes missing.
hello-dockcontainer with both volumes should be as follows:
node_modulesdirectory from inside the container and tuck it away in some other directory managed by the Docker daemon on your host file system and will mount that directory as
node_modulesinside the container.
npm run servecommand starts a development server that serves the application to the user. That server not only serves the files but also provides the hot reload feature.
nodeas the base image and build the application.
nginxinside the node image and use that to serve the static files.
nodeimage is big and most of the stuff it carries is unnecessary to serve your static files. A better approach to this scenario is as follows:
nodeimage as the base and build the application.
nodeimage to a
nginxand discard all
hello-dockproject directory and put following content in there:
Dockerfilelooks a lot like your previous ones with a few oddities. Explanation for this file is as follows:
node:lts-alpineas the base image. The
as buildersyntax assigns a name to this stage so that it can be referred to later on.
RUN npm run buildcommand actually compiles the entire application and tucks it inside
/appis the working directory and
/distis the default output directory for
nginx:stable-alpineas the base image.
EXPOSE 80is added.
--from=builderpart indicates that you want to copy some files from the
builderstage. After that it's a standard copy instruction where
/app/distis the source and
/usr/share/nginx/htmlis the destination. The destination used here is the default site path for NGINX so any static file you put inside there will be automatically served.
nginxbase image containing only the files necessary for running the application. To build this image execute following command:
hello-dockapplication in all it's glory. Multi-staged builds can be very useful if you're building large applications with a lot of dependencies. If configured properly, images built in multiple stages can be very optimized and compact.
gitfor some time now, you may know about the
.gitignorefiles in projects, containing a list of files and directories to be excluded from the repository. Well Docker has a similar concept. The
.dockerignorefile contains a list of files and directories to be excluded from image builds. You can find a pre-created
.dockerignorefile in the
.dockerignorefile has to be in the build context. Files and directories mentioned here will be ignored by the
COPYinstruction but if you do a bind mount, the
.dockerignorefile will have no effect. I've added
.dockerignorefiles where necessary in the project repository.