0% found this document useful (0 votes)
14 views5 pages

TechDoc Docker

Uploaded by

bimlapant1902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

TechDoc Docker

Uploaded by

bimlapant1902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

DOCKER

Dockerfile - Set of instructions


docker build -t custom:V1 .
docker run -d -p 80:80 --name nginx <image:tag>
docker inspect
docker prune
docker start/stop
docker rm/rmi <remove>
docker volume
docker

Containers share the kernel of the host machine it is running on. This means that a
containerized windows app will not run on linux based docker host
windows containers require windows host
linux containers require linux host

It is possible to run linux containers on windows host. Its complicated.

Docker Technology - Runtime // Daemon // Orchestrator


When you install docker you get docker client and docker daemon
docker version
docker image - Object that contains OS filesystem // Application // All the
application dependencies
docker image ls

docker image pull ubuntu:latest - This will include a stripped down version of
ubuntu filesystem with few common utilities
mcr.microsoft.com/powershell:lts-nanoserver-1903 image contains a Windows Server
Core OS plus PowerShell

You can refer to an image via its unique ID or Name

docker container run -it ubuntu:latest /bin/bash


docker container run -it mcr.microsoft.com/powershell:lts-nanoserver-1903 pwsh.exe

container run ubunt:latest - Run a container and it should be based on ubuntu


latest image
-it - Make container interactive and to attach current shell to container's
terminal
Finally we tell docker which process we want to run inside of the container. Linux:
Bash // Windows: Powershell

Only long running process inside linux container is actually /bin/bash

CTRL - PQ to come back to the host without terminating your container

Check number of process on the docker host machine - Linux/Windows machine run a
lot of process as compared to their docker counterpart.

docker container exec -it <container name> bash - Attaching your shell to terminal
of the running container

docker container ls <All running containers>


docker container ls -a <All containers running or stopped>

Dockerfile -
FROM alpine
LABEL maintainer="ap597"
RUN apk add --update nodejs nodejs-npm
COPY . /src
WORKDIR /src
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node","./app.js"]

***********************************************************************************
***********************************************************

UNDER THE HOOD - TLDR

Docker Client -> Docker Daemon -> Containerd -> Runc -> Container

***********************************************************************************
***********************************************************

IMAGES - DEEP DIVE

A Docker image is a unit of packaging that contains everything required for an


application to run. This includes application code, application dependencies, and
OS constructs.
If you have an application’s Docker image, the only other thing you need to run
that application is a computer running Docker.

Images are like stopped containers - In fact you can stop a container and create a
new image from it.
Images - Build time constructs
Containers - Run time constructs

The whole purpose of a container is to run a single application or service. This


means it only needs the code and dependencies of the app/service it is running.
It does not need anything else. This results in small images stripped of all non-
essential parts.

/var/lib/docker - Linux docker repository

Cetralized place to share and access images - Registry


REGISTRY -> IMAGE REOSITORY -> IMAGE having multiple versions

IMAGE NAMING AND TAGGING -

First, if you do not specify an image tag after the repository name, docker will
assume you are referring to the image tagged as latest.

docker image ls --filter dangling=true

A dangling image is an image that is no longer tagged, and appears in listings as


<none>:<none>. A common way they occur is when building a new image giving it a tag
that already exists.
When this happens, Docker will build the new image, notice that an existing image
already has the same tag, remove the tag from the existing image and give it to the
new image.

docker image ls --filter "{{.Size}}"


FILTERS -

DANGLING // BEFORE // SINCE // LABEL

DANGLING - docker image ls --filter dangling=true (Dagling accepts True or False)


BEFORE - Requires an image name or ID as argument and returns images created before
it
SINCE - Requires an image name or ID as argument and returns images created after
it
LABEL - Filters images on the basis of labels or labels and values (LABEL -
Instruction in the Dockerfile)

Docker search busybox --filter "is-official=true"

IMAGE is made up of multiple read-only layers. They are stacked together and
represented as a single unified object.
Check layers by inspecting the docker image

ENV EXPOSE CMD ENTRYPOINT - Add metadata to the image but do not add permanent
layers to the image.
All docker images start with a base layer and as changes are made, new layers are
added.

Docker employs a storage driver that is responsible for stacking layers and
presenting them as a single unified filesystem/image.
Examples of storage drivers on Linux include AUFS, overlay2, devicemapper, btrfs
and zfs.
Each one is based on a Linux filesystem or block-device technology, and each has
its own unique performance characteristics.

SHARING LAYERS - Multiple images can and do share layers. This leads to efficiency
in space and performance.

DIGEST (Immutable Identifier)

docker images rm $(docker image ls -aq) -f

***********************************************************************************
***********************************************************

CONTAINERS

Container is the runtime instance of an image.


Containers run until the app they are executing exits.

docker container ls (All running containers)


docker container ls -a (All containers running or stopped)

docker container -d -p 80:80 --name new_container <image>


docker container exec -it <> bash

docker container <name> stop


SIGTERM -> SIGKILL
docker container rm <name> -f
SIGKILL

docker container rm $(docker container ls -aq) -f - Remove all containers


docker run -d -p host-port:container-port --name <name> <image_name>

FROM alpine
LABEL maintainer="[email protected]"
RUN apk add --update nodejs nodejs-npm
COPY . /src
WORKDIR /src
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]

Starts with FROM instruction - Base layer // Rest is your app that is added on top
of this base layer by you.
LABEL - maintainer="xyz". Creates metadata for the image

docker login - Make it available at Docker Hub

docker image tag <OLD/CURRENT TAG> <NEW TAG>

MULTI STAGING BUILDS

FROM node:latest AS storefront


WORKDIR /usr/src/atsea/app/react-app
COPY react-app .
RUN npm install
RUN npm run build

FROM maven:latest AS appserver


WORKDIR /usr/src/atsea
COPY pom.xml .
RUN mvn -B -f pom.xml -s /usr/share/maven/ref/settings-docker.xml
dependency:resolve
COPY . .
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml package -DskipTests

FROM java:8-jdk-alpine AS production


RUN adduser -Dh /home/gordon gordon
WORKDIR /static
COPY --from=storefront /usr/src/atsea/app/react-app/build/ .
WORKDIR /app
COPY --from=appserver /usr/src/atsea/target/AtSea-0.0.1-SNAPSHOT.jar .
ENTRYPOINT ["java", "-jar", "/app/AtSea-0.0.1-SNAPSHOT.jar"]
CMD ["--spring.profiles.active=postgres"]

Each FROM instruction is a Build stage starting from 0


So the stages here are - Stage 0/1/2

DOCKER COMPOSE -

Dokcer compose up
docker compose up -d
docker compose up -f <name of the yml file> -daemon
docker compose stop
docker compose rm
docker compose restart
docker compose down - Newtork and Container are deleted but images pulled and
volume created are not.
docker compose ps

DOCKER NETWORK -

Single host bridge


Multi host overlag
MACVLAN

DOCKER VOLUME -

Docker has its own non persistant storage that is tightly coupled to its lifecycle
If container is deleted so is the storage
You have option to persist data by using docker volume
You create a container and mount a volume to it (one of the filesystems)
Whenever something is written on the directory inside the filesystem at container,
data will also be stored in volume

BEST PRACTICES -

Always add a label with maintainer name


Include multiiple commands as part of single RUN instruction - Using && or \

You might also like