Docker Overview
Difference Between Docker & Hypervisor
Docker Architecture
===================================================================================
==========
Docker Installation
Docker Configuration files
Docker basic commands
* docker version
* docker info
* docker images
* docker pull
* docker run
* docker exec
* docker ps
* docker ps -a
* docker rm
* docker rmi
* docker start/stop/restart
* docker log
* docker inspect
===================================================================================
===========
Dockerhub Account Creation
===================================================================================
===========
Docker Advanced commands
* docker build
* docker login
* docker logout
* docker push
* docker pull
===================================================================================
=========
Docker tagging of Images: docker tag
===================================================================================
=========
Dockerfile Instructions & Creation
How to build image and run container ?
===================================================================================
=========
Docker save & load
Docker export & import
Docker volumes
===================================================================================
=====================
E2E httpd build & deployment via Docker
E2E Tomcat build & deployment via Docker
===================================================================================
=========
Docker: Docker is lightweight container or container management tool.
Docker is used for running applications on lightweight container without OS
dependencies.
Docker is portable means this will run applications on any OS.
Docker uses operating system related requirements from base operating
system
Why Docker?
Difference between Hypervisor and Docker?
==================================================================================
Architecture of Docker:
==================================================================================
Docker is 2 tier architecture
Docker uses client-server model/architecture
Docker host:
* Docker daemon : Docker daemon is process which runs the docker service.
* Docker container : lightweight container which runs an application using images
* Docker images : Image contains all OS files, application files and linux
packages etc
Docker Client:
* Docker build : Docker build is used for building images.
* Docker pull : Docker pull is used for pulling images from dockerhub.com
* Docker run : Docker run is used to create the containers and run applications.
Docker registry:
* dockerhub.com : Storing images in dockerhub.com which an hosted service.
registry: docker.io/account-name/repository
===================================================================================
=
Installation of Docker:
===================================================================================
=
Pre-requisite:
Machine requirement - OS Redhat
Instance type t2.micro
HD - 10 GB
1. Install java : sudo su
yum install java-1.8.0* -y
2. Upgrade the repository
yum update -y
3. Configure the repository - https://download.docker.com/linux/rhel/
vi /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
4.Install Docker Package
yum install docker-ce docker-ce-cli containerd.io -y
4. Enable and start docker service
systemctl enable docker
systemctl start docker
5. Start docker service
systemctl status docker
6. Verify Docker is running :
docker version
docker info
===================================================================================
=========================================
Docker basic commands:
===================================================================================
========================================
1. docker version: Version of docker package for client and server
2. docker info: Information about containers, images, networking, volumes, OS
details etc
3. docker pull: Docker pull command pulls the images from dockerhub.com
command: docker pull redhat/ubi9 // by default, it will pull latest
images via latest tag
docker pull redhat/ubi9:latest
docker pull redhat/ubi9:9.1 / pull specific docker images via tag
[root@ip-172-31-28-17 ~]# docker pull redhat/ubi9
Using default tag: latest
latest: Pulling from redhat/ubi9
1041e8041676: Pull complete
Digest: sha256:572155f3053e0267874da447743adec560458824c12d3f8acd429f781656cf33
Status: Downloaded newer image for redhat/ubi9:latest
docker.io/redhat/ubi9:latest
[root@ip-172-31-28-17 ~]#
4. docker images : Docker images will display or list all images
command: docker images
[root@ip-172-31-28-17 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redhat/ubi9 latest 781f92ea81ad 11 days ago 209MB
[root@ip-172-31-28-17 ~]#
===================================================================================
================================
5. docker run : Docker run creates the containers
command: docker run -itd --name container_name image_name:tag_name
eg: docker run -itd --name redhat-container redhat/ubi9:latest
-it interactive
-d daemon(run process in background)
[root@ip-172-31-28-17 ~]# docker run -itd --name redhat-container
redhat/ubi9:latest
fe1847dcdc3a6f82c5e808a0f1af1e97b843e04ae6a1300d70f1c825ceb312a2
[root@ip-172-31-28-17 ~]#
===================================================================================
=================================
6. docker ps : docker ps will list all running containers
command: docker ps
[root@ip-172-31-28-17 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
fe1847dcdc3a redhat/ubi9:latest "/bin/bash" 4 minutes ago Up 4 minutes
redhat-container
[root@ip-172-31-28-17 ~]#
===================================================================================
=================================
7. docker ps -a : docker ps -a will list all running and non running containers
command: docker ps -a
[root@ip-172-31-28-17 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
fe1847dcdc3a redhat/ubi9:latest "/bin/bash" 4 minutes ago Up 4 minutes
redhat-container
[root@ip-172-31-28-17 ~]#
===================================================================================
================================
8. How to login/go inside a/into container ?
command: docker exec -it container_name/container_id /bin/bash
eg: docker exec -it redhat-container /bin/bash
[root@ip-172-31-28-17 ~]# docker exec -it fe1847dcdc3a bash
[root@fe1847dcdc3a /]#
[root@fe1847dcdc3a /]
===================================================================================
================================
9. How to stop/start/restart container ?
command: docker stop container_name/container_id
docker start container_name/container_id
docker restart container_name/container_id
[root@ip-172-31-28-17 ~]# docker stop fe1847dcdc3a
fe1847dcdc3a
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
fe1847dcdc3a redhat/ubi9:latest "/bin/bash" 7 minutes ago Exited (137) 10
seconds ago redhat-container
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker start fe1847dcdc3a
fe1847dcdc3a
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
fe1847dcdc3a redhat/ubi9:latest "/bin/bash" 8 minutes ago Up 4 seconds
redhat-container
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
fe1847dcdc3a redhat/ubi9:latest "/bin/bash" 8 minutes ago Up 7 seconds
redhat-container
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker restart fe1847dcdc3a
fe1847dcdc3a
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
fe1847dcdc3a redhat/ubi9:latest "/bin/bash" 9 minutes ago Up 5 seconds
redhat-container
[root@ip-172-31-28-17 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
fe1847dcdc3a redhat/ubi9:latest "/bin/bash" 9 minutes ago Up 15 seconds
redhat-container
[root@ip-172-31-28-17 ~]#
===================================================================================
=================================
10. docker inspect: Docker inspect provide detailed information about containers
and images.
command: docker inspect container_name/container_id
docker inspect image_name/image_id
===================================================================================
=================================
11. docker logs : This displays logs of container/application.
command: docker log -f container_id/container_name
===================================================================================
=================================
12. docker rm : docker rm deletes/removes the container.
command: docker rm -f container_name/container_id
[root@ip-172-31-28-17 ~]# docker rm -f fe1847dcdc3a
fe1847dcdc3a
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@ip-172-31-28-17 ~]#
===================================================================================
=================================
13. docker rmi : docker rmi deletes/removes the images
command: docker rmi -f image_name/image_id
[root@ip-172-31-28-17 ~]# docker rmi -f 8e9c11168e6d
Untagged: redhat/ubi9:9.1
Untagged:
redhat/ubi9@sha256:49124e4acd09c98927882760476d617a85f155cb45759aea56b2ab020563c4b8
Deleted: sha256:8e9c11168e6d9de29f6bbd7e59eca89f868cab89028f266dae17c684046b1479
Deleted: sha256:11939111cd6623e79e2b583306543b67986f0fb22b0c051ac16f563a2f7672c2
[root@ip-172-31-28-17 ~]#
===================================================================================
==================================
DockerHub Account creation:
===================================================================================
==================================
Advanced Commands:
===================================================================================
==================================
1. docker login: To login into dockerhub.com via command line interface
command : docker login
[root@ip-172-31-28-17 ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't
have a Docker ID, head over to https://hub.docker.com to create one.
Username: trainercloud
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# cat /root/.docker/config.json
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "dHJhaW5lcmNsb3VkOlJlZGhhdEBAMDAx"
}
}
}[root@ip-172-31-28-17 ~]#
2. docker logout : Disconnect from dockerhub.com via command line interface
command: docker logout
[root@ip-172-31-28-17 ~]# docker logout
Removing login credentials for https://index.docker.io/v1/
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# cat /root/.docker/config.json
{
"auths": {}
}[root@ip-172-31-28-17 ~]#
3. docker push: Docker push is used for publishing the image to dockerhub.com and
store image in docker registry.
command: docker push account/image_name:tag_name // pushing to private
repository
docker push image_name:tag_name // pushing to public
repository - not allowed
[root@ip-172-31-28-17 ~]# docker push trainercloud/nginx-image:1.23
The push refers to repository [docker.io/trainercloud/nginx-image]
563c64030925: Pushed
6fb960878295: Pushed
e161c3f476b5: Pushed
8a7e12012e6f: Pushed
d0a62f56ef41: Pushed
4713cb24eeff: Pushed
511780f88f80: Pushed
1.23: digest: sha256:48a84
4. docker pull: Docker pull is used for downloading image from public/private
repository.
command: docker pull account/image_name:tag_name // pulling to private
repository
docker pull image_name:tag_name // pulling to public
repository
[root@ip-172-31-28-17 ~]# docker pull trainercloud/nginx-image:1.23
1.23: Pulling from trainercloud/nginx-image
52d2b7f179e3: Pull complete
fd9f026c6310: Pull complete
055fa98b4363: Pull complete
96576293dd29: Pull complete
a7c4092be904: Pull complete
e3b6889c8954: Pull complete
da761d9a302b: Pull complete
Digest: sha256:48a84a0728cab8ac558f48796f901f6d31d287101bc8b317683678125e0d2d35
Status: Downloaded newer image for trainercloud/nginx-image:1.23
docker.io/trainercloud/nginx-image:1.23
[root@ip-172-31-28-17 ~]#
5. docker tag: Tag is bookmark or unique indentification number which specify a
version for an image.
eg: default tag is latest
Tags: 1.0, 5.5.1, tomcat-5.3, java11 etc
How to create an tag for an existing image?
command: docker tag nginx:latest nginx:1.23.3
docker tag nginx:latest nginx-image:1-20-new-nginx
docker tag nginx:latest trainercloud/nginx-image:latest
6. docker build: Building an image from Dockerfile.
command: docker build -t trainercloud/image_name:tag_name -f Dockerfile .
===================================================================================
====================================
Dockerfile :
Instructions of Dockerfile
How to build an image using Dockerfile?
===================================================================================
====================================
Dockerfile: Dockerfile is an instruction file to build images, it allows to
customize images, running all linux commands, adding users, export ports, run some
startup scripts etc
Dockerfile name should always start with captial "D" and Dockerfile is a text file.
===============================================================================
Instructions of Dockerfile:
===============================================================================
1. FROM: FROM instruction is used for mentioning the base docker image.
eg: FROM ubuntu:latest/22.04
FROM redhat/uib9:latest/9.1
FROM nginx:latest
===============================================================================
2. MAINTAINER: Here we mention who is the author of Dockerfile.
eg: MAINTAINER trainercloud2023@gmail.com
===============================================================================
3. RUN: RUN instruction is used for running any linux commands.
This instruction will be triggered at build time of Docker image.
eg: RUN yum install java-1.8.0* -y
RUN mkdir /opt/tomcat
RUN useradd john
===============================================================================
4. CMD: CMD instruction is used mostly at the docker runtime when container starts
or container creation/running.
This instruction will trigger any startup commands , scripts for
application to start etc
When only CMD is used, only one CMD instruction can be used in
Dockerfile, in case if you mention more than one CMD, it will ignored.
eg: CMD ["/opt/tomcat/bin/startup.sh"]
CMD ["/opt/tomcat/bin/catalina.sh, run]
CMD ["bin/execute.sh"]
===============================================================================
5. ENTRYPOINT: ENTRYPOINT instruction is used mostly at the docker runtime when
container starts or container creation/running.
This instruction will trigger any startup commands , scripts for
application to start etc
With Entrypoint we can mention more than one CMD command.
eg: ENTRYPOINT ["/opt/tomcat/bin/catalina.sh"]
CMD ["run"]
CMD [""]
===============================================================================
6. USER: This instruction will switch to user account.
eg: USER john
USER root
=================================================================================
7. ENV: Setting an environment variables. This can be used at runtime of docker
container.
eg: ENV tomcat-user=tomcat
ENV tomcat-password=""
ENV hostname=10.0.0.5
Note: Once we go inside container (docker exec -it container_id bash)
-> env //list all environment variables
-> echo $tomcat-user
===================================================================================
===
8. WORKDIR: This instruction will change/switch the working directory (same as cd
command in linux)
eg: workdir /opt/tomcat/webapps
workdir /app/devops
===================================================================================
=======
9. COPY: COPY instruction is used for coping files from local machine into docker
build path location of Dockerfile and at build time it will be part of docker image
eg: COPY /opt/project/jelly.jar /usr/lib/jelly.jar
COPY /mnt/project/file.txt /mnt/project/file.txt
===================================================================================
==========================================10. EXPOSE: Exposing the docker container
application port number to access outside world via browser
eg: EXPOSE 80
EXPOSE 8080
===================================================================================
========================================
How to build an image using Dockerfile?
===================================================================================
========================================
command: docker build -t image_name:tag_name . // default Dockerfile will be
picked up from current directory and .(dot)is used as current directory as a
workspace
docker build -t image_name:tag_name -f Dockerfile_httpd . // Specific
Dockerfile forcefully from current directory
-f force
-t tagging - name of docker image
===================================================================================
===================================
vi Dockerfile
FROM ubuntu:latest
MAINTAINER devopscloudtrainer2022@gmail.com
RUN apt-get update -y
RUN apt-get install nginx -y
vi Dockerfile_Redhat
FROM redhat/ubi9:latest
MAINTAINER devopscloudtrainer2022@gmail.com
RUN yum update -y
RUN yum install nginx -y
===================================================================================
====================================
E2E httpd build & deployment via Docker
E2E Tomcat build & deployment via Docker
===================================================================================
===================================
vi index.html
<H1> Docker Image and container </H1>
vi Dockerfile-httpd
=========================================================================
FROM redhat/ubi9:latest
MAINTAINER trainercloud2023@gmail.com
RUN yum update -y
RUN yum install httpd -y
RUN yum install procps -y
COPY index.html /var/www/html/index.html
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D" ,"FOREGROUND"]
Execution: docker build -t redhat-httpd-image:latest -f Dockerfile-httpd .
docker run -itd --name redhat-httpd-container -p 80:80 redhat-httpd-
image:latest
docker exec -it redhat-httpd-container bash
redhat-httpd-container-id > cd /var/www/html
ls -lrt // index.html
curl http://172.17.0.2:80/index.html
Open security group : custom tcp 80 anywhere 0.0.0.0/0
HIT URL : http://Public-Ip:80/index.html
===================================================================================
=============================
How to expose application port to outside world?
===================================================================================
================================
vi Dockerfile-tomcat
===================================================================================
================================
FROM redhat/ubi9:latest
MAINTAINER demo@training.com
ENV tomcat_test 123
RUN yum update -y
RUN mkdir -p /opt/tomcat/
WORKDIR /opt/tomcat
RUN curl -O http://archive.apache.org/dist/tomcat/tomcat-8/v8.5.40/bin/apache-
tomcat-8.5.40.tar.gz
RUN tar xvfz apache*.tar.gz
RUN mv apache-tomcat-8.5.40/* /opt/tomcat/
RUN yum -y install java-1.8.0*
RUN java -version
WORKDIR /opt/tomcat/webapps
RUN curl -O -L
https://github.com/AKSarav/SampleWebApp/raw/master/dist/SampleWebApp.war
EXPOSE 8080
CMD ["/opt/tomcat/bin/catalina.sh", "run"]
Execution:
docker build -t webapp-tomcat -f Dockerfile-tomcat .
docker run -itd --name webapp-tomcat-container -p 8080:8080 webapp:latest
docker exec -it container_id bash
container-id> curl://http:container-IP:8080/SampleWebApp
env or echo $tomcat_test
Open Security group : 22 SSH
80 HTTP
8080 CUSTOM TCP
CUSTOM ICMP IPV4
Browser - URL : http://Public-IP:8080
Tag and Push Image :
docker tag webapp-tomcat:latest account/webapp-tomcat:8.5
docker push account/webapp-tomcat:8.5
===================================================================================
==============================
Docker save & load: Backup and restore
===================================================================================
=============================
docker save: Docker save is used for bundling the docker image into tar file and
further we can ship this tar to someone or store in some location.
When we create image tar file, this can be used as backup file.
syntax: docker save -o image.tar image_name:tag_name
eg: docker save -o nginx-redhat-image.tar nginx-redhat-image:latest
[root@ip-172-31-28-17 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-redhat-image latest 8c3384411d61 24 hours ago 233MB
[root@ip-172-31-28-17 ~]# docker save -o nginx-redhat-image.tar nginx-redhat-
image:latest
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# ls -lrt
total 234740
-rw-------. 1 root root 240354816 Sep 8 02:54 nginx-redhat-image.tar
[root@ip-172-31-28-17 ~]#
Docker load: Docker load is used for untar/extract docker image out of the tar
file. Whenever we have image tar file, we can restore the docker image.
syntax: docker load -i image.tar
command: docker load -i nginx-redhat-image.tar
[root@ip-172-31-28-17 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redhat-httpd-image latest 8f0e240ec7d4 24 hours ago 256MB
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker load -i nginx-redhat-image.tar
Loaded image: nginx-redhat-image:latest
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redhat-httpd-image latest 8f0e240ec7d4 24 hours ago 256MB
nginx-redhat-image latest 8c3384411d61 24 hours ago 233MB
[root@ip-172-31-28-17 ~]#
===================================================================================
==========================================
Docker export & import: Commands are run on running containers ie we can create an
images out of running container.
===================================================================================
==========================================
docker export: Creating an image tar file from running container.
syntax: docker export container_id > http_redhat-running-image.tar
docker import: Creating an docker image out of the tar file exported which we got
from running container.
syntax: docker import - http-new-image-export:1.0 < http-redhat-running-image.tar
Create an container out of imported image:
docker run -itd --name import-export-http-redhat-container -p 8080:80 http-new-
image-export:1.0 bash
docker exec -it import-export-http-redhat-container bash
import-export-http-redhat-container> verify changes
[root@ip-172-31-28-17 ~]# docker import - http-new-image-export:1.0 < http_redhat-
running-image.tar
sha256:3b8c7ef51542f5ca389be5a715dcf177a1f483102dca83879eb5f91e3f324e73
[root@ip-172-31-28-17 ~]#
[root@ip-172-31-28-17 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
http-new-image-export 1.0 3b8c7ef51542 6 seconds ago 228MB
redhat-httpd-image latest 8f0e240ec7d4 24 hours ago 256MB
nginx-redhat-image latest 8c3384411d61 24 hours ago 233MB
[root@ip-172-31-28-17 ~]#
===================================================================================
===============================
Docker volumes: Volumes are persistent storage location for storing any data inside
container.
Even if container is deleted still data exist on host machine ie
where volumes are persistent.
===================================================================================
==============================
1. Bind Mount volumes: Bind mount volumes allows to provide an user defined name
for volume and can create volume anywhere in container and host machine.
Host Machine: /opt/data
container: /opt/data
command: docker run -itd --name bind-mount-container-vol -v /opt/data:/opt/data
nginx:latest
docker exec -it container_name bash
container-id> cd /opt/data
touch file1.txt
Host Machine: cd /opt/data
ls -lrt // file1.txt
===================================================================================
=========================================
2. Unnammed volumes: Unnamed volumes are created by docker which are stored
under /var/lib/docker/volumes which random name.
Container volumes details mentioned inside Dockerfile.
Container: VOLUME /app/devops // path to container volume
Host machine: /var/lib/docker/volumes/unique_id/
vi Dockerfile-volume-unnamed
FROM ubuntu:latest
MAINTAINER trainercloud2023@gmail.com
VOLUME /app/devops
RUN apt-get update -y
RUN apt-get install nginx -y
docker build -t nginx-ubuntu-image-unnamed -f Dockerfile-volume-unnamed .
docker run -itd --name nginx-ubuntu-container-unnamed nginx-ubuntu-image-
unnamed:latest
docker exec -it nginx-ubuntu-container-unnamed bash
nginx-ubuntu-container-unnamed > cd /app/devops
touch f1 f3 f4 f5
Host Machine: /var/lib/docker/volumes/unique_id/_data
ls -lrt //f1 f3 f4 f5
===================================================================================
==========================================
3. Named volumes: Create name volumes in host machine using docker volume command
command: docker volume create vol_1
docker volume ls
docker volume rm vol_1
docker inspect vol_1
Host Machine : volumes are stored under /var/lib/docker/volumes/vol_1
container volume :/mnt/data
docker run -itd --name nginx-ubuntu-container-named --mount
source=vol_1,target=/mnt/data nginx:latest
docker exec -it nginx-ubuntu-container-named bash
nginx-ubuntu-container-named > cd /mnt/data
touch file1
Host machine: cd /var/lib/docker/volumes/vol_1
ls -lrt /file1
===================================================================================
===================
Docker network:
===================================================================================
===================
1. Bridge
2. host
3. none
===================================================================================
===================
===================================================================================
==================
Networking :
===================================================================================
=
1. bridge -> This is used for connecting your container from host machine.
-> container IP : PORT
-> host IP :PORT
-v PORT:PORT
Network bridge ->
2. host -> Directly acces host IP for container access
3. none -> No IP address or no network required.
=================================================================
Docker networking is primarily used to establish communication between Docker
containers and the outside world via the host machine where the Docker daemon is
running. …
You can run hundreds of containers on a single-node Docker host,
so it’s required that the host can support networking at this scale.
How does networking work with Docker?
Docker secures the network by managing rules that block connectivity between
different Docker networks.
Behind the scenes, the Docker Engine creates the necessary Linux bridges, internal
interfaces, iptables rules,
and host routes to make this connectivity possible.
How do I connect to a Docker network?
Connect a container to a network when it starts
You can also use the docker run --network= option to start a container and
immediately connect it to a network.
=================================================================
Bridge :
=================================================================
The network with the name bridge is the default network a container is attached to
by default.
It is created from the bridge driver. A driver is like a template for the network
with specific behavior and capability.
A bridge driver creates an isolated pool of private IP addresses (a private subnet)
on the host which usually
starts with an IP address 172.X.X.X.
Commands for Docker Networking:
docker network ls
docker network inspect bridge
=============================
Docker Network create :
================================
docker network create -d/--driver bridge mybridge
docker network ls
==============================================
Attach Network to container:
====================================================
docker run -itd --name container_name --net mybridge image:latest
or
docker run –it –network=new_nw ubuntu:latest /bin/bash
docker exec -it container_Id bash
hostname -i
============================================================
Connect a newtork device to container
============================================================
docker network connect <network> <container_id>
=====================================================
Disconnect a newtork device to container
======================================================
docker network connect <network> <container_id>
======================================
How to delete network driver
================================================
docker network rm <network>
============================================================================
Host Network :
==============================================================================
A host network uses the host’s network, therefore containers are no longer
isolated in a private network and they do not receive a public IP address.
They are virtually a service spawned on the host’s network which is why they
consume the host’s ports.
A host network uses the host driver and only one instance of the host network can
exist on a host machine.
Therefore we can’t create a network of the type host using the $ docker network
create -d host command as
Docker already creates a host network for us.
=====================================
How to user docker host type network :
=========================================
docker run -itd --name container_name --net host -p 80:80 nginx:latest
verify the host and container IP both are same, do exec and hostname -i
docker exec -it container_Id bash
hostname -i
===================================================================================
====
===================================================================================
=
None Network :
===================================================================================
=====
===================================================================================
=====
A none network doesn’t provide any networking capability to the container which
means the container is like a black box to the host.
The host or any other container won’t be able to communicate with the container.
A none network can’t be created with command " docker network create -d none"
So let’s create a container from image but with the --net=none flag.
With this, the container won’t get any public IP address.
The only IP address it has is its loopback IP address (localhost).
=====================================
How to user docker none type network :
=========================================
docker run -itd --name container_name --net none -p 8080:8080 image:latest
verify the host and container IP both are same, do exec and hostname -i
docker exec -it container_Id bash
hostname -i
==============================================================
How to remove docker custom docker network created:
==========================================================
docker network prune
docker network rm <network>
===================================================================================
=====================