Docker image commands- Part-01

The docker build command builds Docker images from a Dockerfile

$ docker image build --help

To list the help on any command just execute the above command, followed by the — help option.

Create a Dockerfile with the below content.

FROM nginx:alpineCMD ["nginx", "-g", "daemon off;"]
  1. Create a docker image
$ docker image build -t myapp:v1.0.0 .

2. Create a docker image with different file locations and a custom dockerfile name

$ docker image build -t myapp:v1.0.0 -f Dockerfile/sample/prod.dockerfile .

3. Build a docker image with memory and CPU limit

$ docker build --cpu-shares=2 --memory=1024m -t myapp:1.0 .

4. Force Docker to build a Docker image without the cache

$ docker build --pull --no-cache --tag myapp:v1 .

--no-cache - This will force rebuilding of layers already available

--pull - This will trigger a pull of the base image referenced using FROM ensuring you got the latest version.

5. Set a build-time variable with — build-arg

FROM alpine:3.9.3#Setting a default value to Argument WELCOME_USER
ARG WELCOME_USER=Bikram
RUN echo "Welcome $WELCOME_USER, to Docker World!" > message.txt
CMD cat message.txt

Building Docker Image with default argument and create a container

$ docker image build -t arg:v1 .
$ docker run arg:v1

Passing the argument(WELCOME_USER) during image build time using –build-arg flag

$ docker image build -t arg:v2 --build-arg WELCOME_USER=Google .
$ docker run arg:v2

6. Docker image history command helps us find all the intermediate layers.

$ docker image history httpd -H

Different Options with history command:

— format string Pretty-print images using a Go template

-H, — human Print sizes and dates in human-readable format (default true)

— no-trunc Don’t truncate output

-q, — quiet Only show image IDs

Cloud and DevOps Enthusiast