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
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 psTo 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
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 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
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 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
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
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
========================
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)
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"]
========================
#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
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
docker exec my_testwebserver bash -c 'echo $DocumentRoot'
Docker Compose
https://gitlab.com/andromeda99/docker_compose
https://docs.docker.com/compose/install/
Some Useful links:-
https://docs.docker.com/registry/deploying/#run-a-local-registry
https://gitlab.com/andromeda99/webserver-docker.git
https://stackoverflow.com/questions/30133664/how-do-you-list-volumes-in-docker-containers
https://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/
https://stackoverflow.com/questions/30969435/where-is-the-docker-daemon-log
https://sematext.com/blog/docker-logs-location/
https://docs.docker.com/engine/reference/commandline/volume_ls/
https://stackoverflow.com/questions/28722548/updating-path-environment-variable-permanently-in-docker-container/38905161
https://www.aelius.com/njh/subnet_sheet.html
Docker Compose
https://gitlab.com/andromeda99/docker_compose
https://docs.docker.com/compose/install/
Some Useful links:-
https://docs.docker.com/registry/deploying/#run-a-local-registry
https://gitlab.com/andromeda99/webserver-docker.git
https://stackoverflow.com/questions/30133664/how-do-you-list-volumes-in-docker-containers
https://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/
https://stackoverflow.com/questions/30969435/where-is-the-docker-daemon-log
https://sematext.com/blog/docker-logs-location/
https://docs.docker.com/engine/reference/commandline/volume_ls/
https://stackoverflow.com/questions/28722548/updating-path-environment-variable-permanently-in-docker-container/38905161
https://www.aelius.com/njh/subnet_sheet.html
https://phoenixnap.com/kb/ansible-check-if-file-exists
Comments
Post a Comment