1. Home
  2. Docker
  3. Docker Commands
  4. Docker – build

Docker – build

Docker build command

Docker build command is used to build an image from a Dockerfile. For more details visit our Dockerfile and Dockerfile directives tutorial.

Syntax

docker build [OPTIONS] [Dockerfile PATH|URL]

Example

Let’s create an example Dockerfile in your current directory.

vim Dockerfile

and add the following content.

Now build the image using single dot “.”, as Dockerfile is available in the current directory. Also, use -t image_name to specify tag name to the image.

docker build -t apache_ubuntu .

The build command will pull the image “tecadmin/ubuntu-ssh:16.04” from dokcerhub if not available locally. then it will install Apache2 web server on it. Then set the apachectl command on startup. Also, expose the port 80 to bind it with docker host machine port.

Build Dockerfile from URL

You directly use Dockerfile from remote URLs. For example, I have Dockerfile in your Github repository. Use this

docker build -t apache_ubuntu:latest "https://github.com/tecrahul/dockerfile.git"

You can also use a remote tarball to build the image. The archive must have files only, not with the parent directory.

docker build -t apache_ubuntu:latest "https://tecadmin.net/tutorial/static/archives/dockerfile.tar.gz"

Docker Build Options

Docker build command provides multiple options to build your images as per your requirements. Below is the list of few options, which is more useful and frequently used by users.

#1. Tag (-t, –tag list)

Use this option to specify tag name for the image. A tag name must be valid ASCII and may contain lowercase and uppercase letters, digits, underscores, periods and dashes. A tag name may not start with a period or a dash. It may contain a maximum of 128 characters.

docker build . -t apache_ubuntu:16.04
docker build . -t apache_ubuntu:latest
docker build . -t ubuntu:apache2.4

To publish your image on dockerhub, you must specify dockerhub username before the image name. For example my dockerhub user is tecadmin. So I build image as

docker build . -t tecrahul/apache_ubuntu:16.04
docker build . -t tecrahul/ubuntu:apache2.4
#2. Filename (-f, –file string)

Use this option to specify the name of your Dockerfile. For example, you have multiple Dockerfile in the current directory with different-2 names.

docker build -f Dockerfile.nginx .
#3. Suppress Output (-q, –quiet)

This option is used to suppress output during the build process. This will show image id after build completed

docker build . -q
#4. Memory Limit (-m, –memory bytes)

You can specify the maximum memory limit the docker build process can be used. To limit 100MB use below option.

docker build . -m 100000000
Tags , ,