Home » Docker build command

Docker build command

  • by
Docker build command

1. Overview

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:

  1. Git repositories
  2. Pre-packaged tarball contexts
  3. 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
Sample docker build output

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.

3. Conclusion

To sum up, we have learned to use the build command to construct docker images.

Leave a Reply

Your email address will not be published. Required fields are marked *