×
☰ See All Chapters

docker build

What is the Docker builder?

Docker builder builds docker images which relies on Dockerfile describing how to build an image and relies on cache for storing previous builds and have quick iterations. Input to the docker builder is a directory containing Dockerfile which is known as docker context.

The docker build command

Below is the docker build command to build an image from a Dockerfile:

docker build -t <imagename> .

<imagename>: Name of the image. This should be in lowercase.

There are a lot of flags listed that you can pass when building your image. But out of all of these options, we only need to use --tag or its shorthand -t to name our image.

Docker run command should be executed from directory having Dockerfile. If you wish to execute the docker run command from folder having no docker file then you should specify the path to the Dockerfile using --file/-f switch as below:

docker build -t <imagename> --f <PathToDockerfile>

Let's use the handy --help switch on the docker build command to view all supported switch we can use with run command.

docker image build --help

 

docker-build-0
 

Docker .dockerignore file

Docker build process will send all files in the Dockerfile folder to docker daemon. The .dockerignore file is used to exclude those files or folders we don't want include in the docker build process. .dockerignore file should go in the folder where the Dockerfile was placed. The exclusion list in the file can have both the fully specified file or directory name and the wild cards. Each exclusion should be kept in a separate new line.

Example:

.metadata

.git

bin/

tmp/

*.tmp

*.bak

*.swp


All Chapters
Author