In this article, we will learn the use of the docker build command. You can learn more about docker by referring to these articles.
A docker image is a template or blueprint for containers. It is a sharable package with all the code, required tools, runtime, and dependencies to run that specific code. Containers are based on docker images and concrete running instances of images. Docker is a tool for creating and managing containers.
2. Docker build command
The build command builds an image from a DockerFile in the current directory and a build context. A build’s context is the set of files in the specified path or URL. Later, you can use this image to run a docker container.
The syntax of the build command is:
docker build [options] path | URL
The URL parameter can refer to three kinds of resources:
- Git repositories
- Pre-packaged tarball contexts
- Plain text file
The build process can refer to any of the files in the context. For example, your DockerFile can instruct the build command to use a COPY instruction to reference a file in the context i.e., copies new files or directories from the current build context to the filesystem of the container.
Every container has its own file system.
As soon as you execute the docker build, it loads the build definition by reading the Dockerfile.
A sample Dockerfile:
FROM node:14 // download node image from docker hub WORKDIR /app // create a working directory app COPY package.json . // copy package.json file to container file system RUN npm install // install npm dependencies COPY . . // copy all files to container file system EXPOSE 3000 // expose container port 3000 to host OS CMD [ "node", "app.mjs" ] // Allow usage of node command
The last line of the docker build output shows the image SHA256 code which you can use to run a docker instance by using the docker run command.
To sum up, we have learned to use the build command to construct docker images.