What is docker?

What is Docker?

Docker can be defined as below:

  • Docker is an image format.
  • Docker is a container run time library which manages container life cycle.
  • A command line tool for packaging and running containers.
  • It provides API for container management.

Container image

Container image is a single binary file that has a unique ID and holds everything needed to run the container. Whenever we run a container image with docker or Kubernates, we need to provide container image id or URL then system will automatically do everything like downloading, unpacking and running container etc.

How to use docker desktop in windows machine to run docker container step by step?

Download docker desktop from https://www.docker.com/products/docker-desktop and install in your windows machine.

After installation of docker desktop follow below steps:

Verify docker version:

docker version

Run a container:

docker container run -p 9999:8888 –name hello cloudnatived/demo:hello

Leave this command running, and point your browser to http://localhost:9999

 You should see a friendly message: Hello, 世界

Download git for your systems.

https://git-scm.com/download/

After installation of git open bash terminal and execute below commands.

git clone https://github.com/cloudnativedevops/demo.git

Checking the Source Code:

cd demo/hello

ls

Dockerfile  README.md  go.mod  main.go

Dockerfile

Dockerfile is a simple text file that contains set of instructions that needs to be executed for building a docker image.

$ cat Dockerfile

FROM golang:1.14-alpine AS build

WORKDIR /src/

COPY main.go go.* /src/

RUN CGO_ENABLED=0 go build -o /bin/demo

FROM scratch

COPY –from=build /bin/demo /bin/demo

ENTRYPOINT [“/bin/demo”]

It uses a standard multi-stage build process for Go containers. The first stage starts from an official golang container image, which is just an operating system (here Alpine Linux) with the Go language environment installed. It runs the go build command to compile the main.go file. The result of this is an executable binary file named demo. The second stage takes a completely empty container image (called a scratch image, as in from scratch) and copies the demo binary into it.

Docker Registries

The docker registry is a repository to store and retrieve docker image by using unique name.

The default registry for the docker container run command is Docker Hub.

Building a container image:                                                                               

$ docker image build -t myhello .

#1 [internal] load .dockerignore

#1 transferring context: 2B 0.1s done

#1 DONE 2.4s

#2 [internal] load build definition from Dockerfile

#2 transferring dockerfile: 237B 1.0s done

#2 DONE 2.7s

#3 [internal] load metadata for docker.io/library/golang:1.14-alpine

#3 DONE 8.8s

#6 [internal] load build context

#6 DONE 0.0s

#4 [build 1/4] FROM docker.io/library/golang:1.14-alpine@sha256:e196a762bd3…

#4 resolve docker.io/library/golang:1.14-alpine@sha256:e196a762bd349b188d66c539354fc23f0e5a0178c214e6dfe4d7894c3c2753f1 0.0s done

#4 sha256:32dc91e030acdaf539c69379095ba2e3d5e714ff1820bb0cd01d7c0d02a92f1d 4.62kB / 4.62kB done

#4 sha256:e196a762bd349b188d66c539354fc23f0e5a0178c214e6dfe4d7894c3c2753f1 1.65kB / 1.65kB done

#4 sha256:ef409ff24dd3d79ec313efe88153d703fee8b80a522d294bb7908216dc7aa168 1.36kB / 1.36kB done

#4 …

#6 [internal] load build context

#6 transferring context: 372B 0.0s done

#6 DONE 0.5s

#4 [build 1/4] FROM docker.io/library/golang:1.14-alpine@sha256:e196a762bd3…

#4 sha256:4c0d98bf9879488e0407f897d9dd4bf758555a78e39675e72b5124ccf12c2580 0B / 2.81MB 0.5s

………………..

#5 [build 2/4] WORKDIR /src/

#5 DONE 2.4s

#7 [build 3/4] COPY main.go go.* /src/

#7 DONE 0.8s

#8 [build 4/4] RUN CGO_ENABLED=0 go build -o /bin/demo

#8 DONE 11.5s

#9 [stage-1 1/1] COPY –from=build /bin/demo /bin/demo

#9 DONE 1.3s

#10 exporting to image

#10 exporting layers

#10 exporting layers 3.6s done

#10 writing image sha256:0ba9d890bb80d14fcf49b7147c9c584cc028bb5949e9ce9a15053bb82b7213c3

#10 writing image sha256:0ba9d890bb80d14fcf49b7147c9c584cc028bb5949e9ce9a15053bb82b7213c3 0.2s done

#10 naming to docker.io/library/myhello

#10 naming to docker.io/library/myhello 0.2s done

#10 DONE 4.0s

Run newly build image:

$ docker container run -p 9999:8888 myhello

docker image
docker image

Port forwarding:

Program running in a container is isolated from other program running on the same machine. They don’t have direct access to network resources like network ports.  Here our demo application listen on port 8888, this is container own private port not machine/computer port.  We need to forward the container port (8888) to the computer port 9999 for example to connect container to network.

Command:

docker container run -p 9999:8888 myhello

Image push / pull in docker hub:

In order to be able to push a local image to the registry, we need to name it using this

format: YOUR_DOCKER_ID/myhello

There is no need of rebuilding image instead we need to run below command for naming image.

$ docker image tag newimage tkdockerid14/newimage

To push image in docker hub, we need to execute below command.

$ docker image push tkdockerid14/newimage

The push refers to repository [docker.io/tkdockerid14/newimage]

61927aadde90: Preparing

61927aadde90: Pushed

latest: digest: sha256:320f46b28dbcb153418601a555a52e344805e09052badc428eb5de6963bef666 size: 527

Go to docker hub and check the image has been pushed successfully.

docker hub
docker hub

Now we can run this image from anywhere if internet connection is there in the system.

Let’s try to run this image

$ docker container run -p 9999:8888 tkdockerid14/newimage

docker container is running
docker container is running