What is Docker ? How to use it ?

Reading Time: 3 minutes
Docker logo

Docker is a software containerization platform, meaning you can build your application, package them along with their dependencies into a container and then these containers can be easily shipped to run on other machines.

Now let’s talk about containers and containerization.

Containers

A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings.

Containerization

Containerization

Containerization is the packaging together of software code and all it’s necessary components like libraries, frameworks and other dependencies so that they are isolated in their own container.

This is the software application within the container can be moved and run consistently in any environment and on any infrastructure, independent of that environment or infrastructure’s operating system. The container acts as a kind of bubble or like a computing environment surrounding the application and keeping it independent of its surroundings. It’s basically a fully functional and portable computing environment.

Docker Image

Docker Images are the blue-print for creation of containers. It acts like instructions to spin up docker containers.
We can use same image to spin multiple same containers, it helps to create the containers fast.

Docker File

Docker file is a text document which contains all the instructions to create a docker image. It consists of libraries, dependencies, build tools, base images, etc.

The Structure of the docker file looks like:

FROM
RUN
CMD
EXPOSE
ENV
ADD
COPY
ENTRYPOINT
USER
WORKDIR

docker processing

Multi Stage Builds

Multistage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain.

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

Eg: Dockerfile

FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]  

Before multi stage builds

One of the most challenging things about building images is keeping the image size down. Each instruction in the Dockerfile adds a layer to the image, and you need to remember to clean up any artifacts you don’t need before moving on to the next layer. To write a really efficient Dockerfile, you have traditionally needed to employ shell tricks and other logic to keep the layers as small as possible and to ensure that each layer has the artifacts it needs from the previous layer and nothing else.

Reference

For more detailed information about Multi stage builds you can use this link.

Use this link for official docker documentation.

Written by 

Pallav is working as Software Consultant at Knoldus Inc. He is a technology enthusiast and a keen learner with more than 2 years of experience. He works as a Quality Catalyst in the organization. He has a good understanding of programming languages like Java and Scala. He likes to listen to songs and playing table tennis.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading