Docker

Docker

Docker

Docker, represented by a logo with a friendly looking whale, is an open-source centralized platform designed to create, deploy and run applications.

  • ?It is a set of Platform as a Service (PaaS) that uses OS level Virtualization, also known as Containerization whereas Virtual Machines like VMware uses Hardware level Virtualization.
  • Docker was first released in March 2013. It was developed by Solomon Hykes.
  • We can install Docker on any OS but Docker Engine runs natively on Linux distribution.
  • Docker uses Container on the Host OS to run applications. It allows applications to use the same Linux Kernel of the Host Computer rather than creating a whole virtual OS.

# Advantages of Docker: -

  • No pre-allocation of RAM.
  • Docker enables you to build a Container Image and use that image across every step of the deployment process.
  • Less cost.
  • It is light in weight.
  • It can run on Physical hardware, Virtual hardware or on Cloud.
  • It took very less time to create containers.

COMPONENTS OF DOCKER

1. Docker Daemon

  • Docker daemon runs on the Host OS.
  • It is responsible for running containers and manages Docker Services.
  • Docker daemon can communicate with other daemons.

2. Docker Client: - Docker users can interact with Docker Daemon through a Docker Client (CLI).

  • ?When a client runs any server command on the docker client terminal, the client terminal sends these docker commands to the Docker Daemon.
  • ?It is possible for Docker Client to communicate with more than one Daemon.

3. Docker Host: - Docker Host is used to provide an environment to execute and run applications.

  • It contains Docker Daemon, Images, Containers, Networks and Storages.

DOCKER HUB/REGISTRY

Docker registry manages and stores the Docker Images.

There are two types of registries in the Docker:

1. ? ? ? Public Registry: - It is also called Docker Hub.

2. ? ? ? Private Registry: - It is used to share images within the enterprise.

DOCKER IMAGES

Docker images are the read only binary templates used to create Docker Containers.

OR

??Single file with all dependencies and configurations required to run a program.

# WAYS TO CREATE AN IMAGE

1. Take an image from Docker Hub.

# docker run -it ubuntu /bin/bash

2. Create image from Dockerfile.

# docker build -t image .

3. Create image from existing docker container.

???????????# docker commit containername image?

DOCKER CONTAINERS

??The container holds the entire packages that are needed to run the application.

???????OR

??In other words, we can say that the image is a template and the container is a copy of that template.

  • ?A container is like a Virtual Machine.
  • ?Images become Containers when they run on Docker Engine.

# BASIC COMMANDS

To install docker - yum install docker -y

To check version of the docker - docker -v

To see all the images, present in your local machine - docker images

To find out images (eg. jenkins) in docker hub - docker search jenkins

To download image from Docker Hub to local machine - docker pull jenkins

To give name to a Container - docker run -it --name containername ubuntu /bin/bash

To check, service is start or not - service docker status

To start container - docker start container_name

To go inside container - docker attach container_name

To see all the containers - docker ps -a

To see only running containers - docker ps

To stop container - docker stop containername

To delete container - docker rm containername

?#DOCKERFILE

Dockerfile is basically a text file. It contains some information.

#DOCKER COMPONENTS

  • FROM - For base image this command must be on the top of the Dockerfile.
  • RUN - To execute commands, it will create a layer in image.
  • MAINTAINER - Author/Owner/Description.
  • COPY - Copy files from local system to container.
  • ADD - Similar to COPY but, it can provide a feature to download files from the internet, also we can extract files.
  • EXPOSE - To expose ports such as port 8080 for Tomcat, port 80 for nginx etc.
  • WORKDIR - To set working directory for a container.
  • CMD - Execute commands but only during Container creation.
  • ENTRYPOINT - Similar to CMD, but has higher priority over CMD, first command will be executed by ENTRYPOINT only.
  • ENV - Environment Variables (To call env, type $Variable_name).

#VOLUME

  • Volume is simply a directory inside our container.
  • Firstly, we have to declare the directory as a volume and then share volume across containers.
  • Even if we stop a container, still we can access volume.
  • Volume will be created in one container and then we can share it among multiple containers.
  • You can declare a directory as a Volume only while creating a container.
  • You can't create volume from an existing container.
  • Volume will not be included when you update an image.
  • You can map volume in two ways:

1.? ? ? ? Container - Container

2.? ? ? ? Host - Container

?# Creating Volume from Dockerfile

?Create a Dockerfile and write:

???FROM ubuntu

???VOLUME [ "/myvolume"]

?For sharing volume across containers

# docker run -it --name container2 --privileged=true --volumes-from container1 ubuntu /bin/bash

?Creating volume by using Command

# docker run -it --name container3 -v /volume2 ubuntu /bin/bash

?Create one more container and share volume2

# docker run -it --name container4 --privileged=true --volumes-from container3 ubuntu /bin/bash

?#DOCKER PORT

Create a container and attach Host and Container port to it

# docker run -td --name myconntainer -p 80:80 ubuntu?

?PUSH/PULL IMAGES IN DOCKERHUB

Docker login

First, give tag to your image

# docker tag image1 dockerid/newimage

?Push command

# docker push dockerid/newimage

?For pulling the same image in another account

# docker pull dockerid/newimage

?For making a container using that image

# docker run -it --name mycontainer dockerid/newimage /bin/bash

Stop running all containers

# docker stop $(docker ps -a -q)

Delete all stopped containers

# docker rm $(docker ps -a -q)

Delete all images

# docker rmi -f $(docker images -q)

Abhay Kumar

Securing Application | Shifting Left | DevSecOps

2 年

Congratulations ?? in first writeup buddy Keep it up ??

要查看或添加评论,请登录

社区洞察

其他会员也浏览了