docker build

Turn a Dockerfile into an image you can run, with a name and a version tag.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • docker build -t my-app:v1 .
  • docker images
  • docker run -d -p 4000:3000 --name my-app my-app:v1
  • curl localhost:4000
  • docker build -t my-app:v2 -f Dockerfile .

How it works

docker build reads the Dockerfile (the recipe: base image, files to copy, commands to run) and makes an image from it, layer by layer.

-t name:tag gives the image a name and a version, so you can run it with docker run name:tag. The . at the end is the build context: the folder whose files the Dockerfile can copy.

Syntax

docker build [OPTIONS] PATH

Options

OptionWhat it does
-tname and tag the image: NAME:TAG
-fuse another Dockerfile
.the build context: this folder

Good to know

  • Each Dockerfile line is a layer. Docker reuses the layers that didn't change, so put the lines that change most (like COPY . .) near the end.
  • Without -t the image has no name, only an ID: you will want the name later.