Trusted answers to developer questions

What is the Docker build command?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

The build command is used to build an image from a Dockerfile, but the command has to be run in the same directory as the Dockerfile.

When an image is built, the commands specified in the Dockerfile are executed. The operating system is installed​ along with all the packages required in the Docker container. Because of this, Docker images can often take up a large amount of space.

Syntax

The build command is run on the command-line using the following syntax:

docker build <options> <directory path or URL>

The directory path will be the address of the directory where we want to set up our container.

We can also provide a Git URL instead.

If we want to include all the files/folders that are in the same directory as the Dockerfile, we can build the image like this:

docker build .

The . operator specifies the current directory.

The options component allows us to add different pieces of information to the build command in order to run the container correctly – options will vary depending on the requirement of the container. The full list of options can be found here.

Docker build context

The directory in which the Docker image is built (i.e., the path provided in the build command) becomes the Docker build context. This is the root directory of our Docker container. The memory outside of​ this directory is inaccessible in the container.

In other words, all the data within the build context is available in the container.

1 of 4

RELATED TAGS

docker
build
image
docker command
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?