Skip to main content

Docker In Details

 

Course Contents:-

1. Overview of Docker
2. Difference between Virtualization & Containerization
3. Installation & Configuration of Docker Runtime on Linux & Windows
4. Practice on Docker commands
5. launch a Webserver in a container
6. Launch public & official images of application like Jenkins, Nginx, DB etc..
7. Launch a base OS Container
8. How to save changes inside the container & create a fresh image(commit)
9. How to ship image & container from one hardware to another.
10. How to remove stop/rm multiple container/images
11. Docker Registry
12. Docker Networking
      Check current docker network         
      Docker Network Bridge            
      Docker Network Weaving         
      Launch our own Docker Cluster with our defined Network          
      Launch containers in our own isolated network 

13. Dockerfile
      Importance of Dockerfile
      How to write Dockerfile
      How to build Dockerfile
      How to launch containers using Dockerfile
      How to export Dockerfile to CI tool
      

  14. Docker-compose      How to launch fleet of docker containers using docker-compose


Getting started

Install Docker on RHEL based VM

1. yum update -y
2. yum install docker -y
3. systemctl start docker
4. systemctl status docker
5. ifconfig

Commands

To list docker version
docker --version

To list running containers
docker ps

To list all stopped/terminated/Exited containers
docker ps -a

To pull image
docker pull "image_name"

To run a container in interactive mode
once you existed the container will terminate.
docker run -it image_name --name=name of your container   process(bash)
ex: docker run -it --name="container_name" "image_name" bash

To run container for long time.
docker run -itd --name=my-<container name> <image name> bash

To remove all stopped containers from the list ex docker ps -a
docker rm $(docker ps -a -q)

To list images
docker images

to find images in local machine

cd /var/lib/docker/

To remove image
docker rmi -f imagename

To go inside a running container
docker exec -it "container_id/container_name" bash

To save changes from container to image
docker commit "container_id" image_name

pull httpd image:
docker pull httpd

Or

docker run -itd --name webserver  httpd
docker images
docker ps 
curl -kv http://localhost:80
docker stop <httpd container id>
docker run -dit --name <container name> -p 8080:80 <httpd>
docker ps 
curl -kv http://localhost:80
browse with public ip and port
docker exec -it <container name> bash
ls htdocs/index.html
cat htdocs/index.html
docker run -dit --name my-web-server  -p 8090:80 httpd

to list container files without logging into container

docker exec <container name>  ls -l

Docker Commit:

docker run -itd --name my-centos centos:latest
docker ps
docker exec -it my-centos bash
yum install httpd -y
ls /var/www/html
mkdir /tmp/imperial
exit
docker ps 
docker images
docker commit <container name> my-new-centos:v1.0
docker images
docker run -itd --name my-new-web  -p 9999:80 mycentos:v1.0 /usr/sbin/httpd -D FOREGROUND 
docker ps
browse with public ip
login to container
go to document root /var/www/html
create index.html with content
and refresh browser

Docker Volume:

go to efs location 
cd /tmp/myefs/myworkspace
ls myworkspace/my_scripted_pipeline/webapp/target/webapp
mkdir docker-volume
cp -rf myworkspace/my_scripted_pipeline/webapp/target/webapp docker-volume/
ls docker-volume

docker images
docker run -itd --name my-con-with-vol -v /tmp/myefs/docker-volume/:/var/www/html -p 8888:80 mycentos:v1.0 /usr/sbin/httpd -D FOREGROUND
browse from browser with 8888
docker exec -it my-con-with-vol ls /var/www/html

Check docker container log

docker logs <container id>

To save the current image to ship it to another device or hardware
docker save "image name" > "my_image_name".tar

To load the above image back to container after shipping
docker load < "my_image_name".tar

To export the running container to another hardware or device with existing configuration
docker export "container_name" > "my_export_image".tar

To import the container
docker import  - "new_image_name" < my_export_image.tar

Create container out of imported image.
docker run -itd --name=<container name> <Image name> bash

To push docker image to your Docker Hub Account.
Initially,

docker login

docker tag imagename yourdockerusername/imagename:tag

docker push yourdockerusername/imagename:tag

To mount volume on containers -v source:destination
docker run -itd --name=container_name -v /tmp/webapp:/var/www/html image_name

To list container file system from Host
docker exec "container_id" command
ex:- docker exec container_id ls /tmp/data0

or

docker exec container_id cat /etc/passwd

To see docker network
docker network ls

to see network information of the containers
docker network inspect bridge

To create out own network
docker network create --driver=bridge --subnet=192.168.2.0/24 "your_network_name"

To check our network
docker network inspect drivername
ex:- docker network inspect "your_network_name" (network name or network id)

To launch container in our network
docker run -itd --network=my_network --name=myweb -p 9999:80  httpd_image:aamirs /usr/sbin/httpd -D FOREGROUND

or

docker run -itd --network=my_network --name=mytest_conatiner centos

To delete a network
docker network rm "network_id or network_name"


To create a docker Private Registry for docker images
https://docs.docker.com/registry/deploying/#run-a-local-registry

To list docker information
docker info

To list docker system info
docker system df

or

docker system df -v

To list docker Statistics
docker stats "container_id"

To find list of Docker commands
docker --help

To find docker volume mount point information on a container
docker inspect -f "{{ .Mounts }}" container_id

To grep only Mounts from above command
docker inspect -f "{{ json .Mounts }}" container_id | jq

or

docker inspect -f "{{ json .Mounts }}" container_id | python -m json.tool

To find history of docker images
docker history image_name

To find all the information about a container
docker inspect container_id

How to create docker volume
docker volume create "volume_name"

by default it will be created under /var/lib/docker/volumes

If we wish to change default volume location, we change it in below file & restart docker service
ls /lib/systemd/system/docker.service

https://stackoverflow.com/questions/36014554/how-to-change-the-default-location-for-docker-create-volume-command

check

docker volume ls

ls /var/lib/docker/volumes

These volumes can also mounted to a container

docker run -itd -v "your_volume_name":/tmp image_name


Docker Daemon logs
Below command will log docker logs only in RHEL based VM's , Other OS may have different location to log the docker logs.

cat /var/log/messages | grep docker

Docker container logs

cat /var/lib/docker/containers/<container_id>/<container_id>-json.log

Container config info files

cat /var/lib/docker/containers/container_id/config.v2.json

cat /var/lib/docker/containers/container_id/hostconfig.json

========================

Dockerfile


https://gitlab.com/Azam-devops/webserver-docker
https://gitlab.com/Azam-devops/docker-jenkins

Difference between Entrypoint & CMD & RUN command with examples


Entrypoint  Shell Form & Exec Form

  • RUN executes command(s) in a new layer and creates a new image. E.g., it is often used for installing software packages.
  • CMD sets default command and/or parameters, which can be overwritten from command line when docker container runs.
  • ENTRYPOINT configures a container that will run as an executable.

Shell form
<instruction> <command>

 

ENV name John Dow ENTRYPOINT echo "Hello, $name"

docker run -it <image>

Exec form

This is the preferred form for CMD and ENTRYPOINT instructions.

<instruction> ["executable", "param1", "param2", ...]

ENV name John Dow

ENTRYPOINT ["/bin/echo", "Hello, $name"]

docker run -it <image>

ENV name John Dow

ENTRYPOINT ["/bin/bash", "-c", "echo Hello, $name"]

docker run -it <image>

RUN has two forms:

  • RUN <command> (shell form)
  • RUN ["executable", "param1", "param2"] (exec form)

CMD has three forms:

  • CMD ["executable","param1","param2"] (exec form, preferred)
  • CMD ["param1","param2"] (sets additional default parameters for ENTRYPOINT in exec form)
  • CMD command param1 param2 (shell form)

CMD echo "Hello world"

docker run -it <image>

 

docker run -it <image> /bin/bash

 

ENTRYPOINT has two forms:

  • ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
  • ENTRYPOINT command param1 param2 (shell form)

ENTRYPOINT ["/bin/echo", "Hello"]

CMD ["world"]

docker run -it <image>

docker run -it <image> John


  • CMD echo “Hello World” (shell form)
  • CMD ["echo", "Hello World"] (exec form)
  • ENTRYPOINT echo "Hello World" (shell form)
  • ENTRYPOINT ["echo", "Hello World"] (exec form)

 Example Dockerfile for testing all above commands--- >

FROM centos:latest
#ENV name John Dow
#ENTRYPOINT echo "$name"

#ENV name John Dow
#ENTRYPOINT ["/bin/echo", "Hello, $name"]

#ENV name Aamir
#ENV var1 Shaikh
#ENTRYPOINT ["/bin/bash", "-c", "echo Hello, $name, $var1"]

#CMD echo "Hello world"

#ENTRYPOINT ["/bin/echo", "Hello"]
#CMD ["world"]

#CMD ["/bin/ping", "localhost"]
========================

To create a Dockerfile.
Refer gitlab https://gitlab.com/andromeda99/webserver-docker link
create a docker file on any location

To run a dockerfile
run a dockerfile where you create it.
docker build -t "new_image_name"  .

To launch webserver using above link
docker run -itd --name=web1_mywebserver -p 8888:80 "new_image_name"

Now launch in your own network & mount volume
docker run -itd -v /tmp/webapp:/var/www/html/ --name=container_name --network=your_network_name -p 9999:80 image_name

How to check the value of an environment variable in a container

https://phoenixnap.com/kb/ansible-check-if-file-exists


Comments

Popular posts from this blog

Jenkins

Pre-requisites 1. Install a Webserver https://gitlab.com/Azam-devops/webserver/-/blob/main/README.md Code for index.html https://gitlab.com/Azam-devops/webserver 2. Maven Code https://gitlab.com/Azam-devops/imperial-maven-project 1. Install & configure Jenkins Automation Server on Linux Vm. 2. Go through at some of the important options in Jenkins. 3. Manage Jenkins. 4. Plugins 5. Global Tools Configuration. 6. Credentials 7. Users 8. Slave Nodes 9. Configuring CI pipeline using Gitlab. 10. Configuring standalone CICD pipeline using. 11. Automating the CICD pipeline. 12. Jenkins log 13. Introduction to Jenkins file. 14. Basic groovy syntax & file formation. 15. Launching a Pipeline using Jenkins file. 3. DevOps Architecture Description of above DevOps plan. Create Maven based source code in Gitlab. Create a Jenkins job which will execute below stages. Checkout code from Gitlab Build/compile the source code using Maven as a build tool. scan the code virtually. Test...

Ansible

  Ansible is an open-source software provisioning, configuration management, and application-deployment tool. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks. Platform support Control machines have to be a Linux/Unix host (for example SUSE Linux Enterprise, Red Hat Enterprise Linux, Debian, CentOS, macOS, BSD, Ubuntu, and Python 2.7 or 3.5 is required. Managed nodes, if they are Unix-like, must have Python 2.4 or later. For managed nodes with Python 2.5 or earlier, the python-simplejson package is also required. Since version 1.7, Ansible can also manage Windows nodes. In this case, native PowerShell remoting supported by the WS-Managemen...

Basic Linux Commands

  Linux Command Cheat Sheet Hello All, Below are the most common commands used in a day to day life of  linux user. if you are new to linux i will recommend you to go through all of the commands.  this commands will help you to troubleshoot linux issues.   Command Description ls Lists all files and directories from present working directory ls-R Lists files in sub-directories ls-a to list down hidden files. ls-al Lists files and directories with complete details like permissions, size, owner cd or cd ~ To go back to home directory cd .. Move one level up cd To change to a particular directory cd / Move to the root directory cat > filename Creates a new file cat filename Displays the content of a file cat file...

Kubernetes-Update

                                                    https://kubernetes.io/ Kubernetes (K8s)  is an open-source system for automating deployment, scaling, and management of containerized applications. It groups containers that make up an application into logical units for easy management and discovery. Kubernetes builds upon  15 years of experience of running production workloads at Google , combined with best-of-breed ideas and practices from the community. Latest Verion:-  1.19 Kubernetes Objects Kubernetes defines a set of building blocks ("primitives"), which collectively provide mechanisms that deploy, maintain, and scale applications based on CPU, memory or custom metrics. Kubernetes is loosely coupled and extensible to meet different workloads. This extensibility is provided in large part by the Kubernetes API, which is used by int...