Docker for absolute beginners
Hello everyone, This is the Docker beginner hands-on approach course. I hope you will like it. So let's get started.
You have to install Docker on your machine according to your distro/OS. After successfully downloading. Let's get started further.
You can also start the docker engine and docker container by
sudo dockerd
Run docker in daemon mode
sudo dockerd --debug
Start docker in background mode
systemctl start docker
Run the first container
docker run hello-world
Run ubuntu image
docker run -itd ubuntu
-it interactive mode, -d for detached mode/run in the background (detached from terminal). If you don't give any version of the image docker will automatically give it the latest tag (ubuntu:latest)
docker ps
#list all running continer
docker ps -a
#show all containers (stopped containers also)
docker stop 'here_id_of_container/or/first/2-5_letters_possibly'
#to stop the running container
#you can again start the container
docker start "container_id"
#to remove a container
docker rm "container_id"
# you cannot remove a running container
Remove all stopped containers
docker container prune
docker images
#list all images
docker image ls
#same as above
docker rmi "image_name"
#to remove the image
docker image prune
# remove all the images that are not attached to any container
Image vs Container -
docker rm - for removing the container
docker rmi - for removing the images
If you only want to run an image not want to run it
docker pull ubuntu
#by default latest image
docker image ls
#list all images
run the image
docker run ubuntu
Here it will execute and stop not in the state of running
docker run -it ubuntu
#connected to ubuntu machine in shell
#now you can run any command inside container
#again run the container in detached mode
docker run -itd ubuntu
Get into the Ubuntu container
docker exec -it 'containerid' /bin/bash
#now a interactive shell open
#exit by ctrl+d or type - exit
docker exec "container_id" ls
#by typing this command make sure the container is in running 1
#list the root dir of the container/ubuntu here
docker inspect "container_id"
#show jSON file / info abou the container
#similarly to get more info about contianer image
docker image inspect "image_id"
#same for network and volume
Deploying a web application Jenkins as a docker container -
Go to the docker hub find an official image of Jenkins and copy the pull command
docker pull jenkins
#maybe this will not work because latest tag is not availabe
docker pull jenkins/jenkins
#this will pull jenkins
docker image ls
#here you see jenkins image also
Now run Jenkins as a container
Find the exposed port here 8080 is the port where the actual Jenkins run
docker image inspect "image_id_of_jenkins"
#see here exposed ports /maybe there is 50000 and 8080 also
To access the Jenkins container through this machine, we have to map some external ports/host port
docker run -d -p 80:8080 jenkins/jenkins
#host_port:exposed_port /host port 80 you can replace with 3000 also
docker ps
#here you see jenkins running
By this Jenkins runs in detached mode and exposes port 80 on the host.
You can verify this by typing in the browser - localhost:80 /localhost:3000
Now connect the running container-
First of all, copy that Jenkins path is given in the browser
Go to the terminal again and run the command
docker exec "container_id" cat /var/jenkins_home/secrets/initialAdminPassword
#a pasword appears in alphanumeric form copy that
Paste that password in localhost:80 - Jenkins page. Now by this Jenkins will unlock
Click on --> Install selected plugins ---> After that setup --> Username, Password etc. ---> Instance URL --> remains same ---> Save and Exit. Now you are referred to the Jenkins dashboard page.
You can stop it by
docker stop "container_id"
This is how we successfully Deploy the Jenkins web application as a container. And is running there on the browser.
Docker Networks -
There are 3 types of docker networks -
Bridge Network
None Network
Host Network
docker network ls
To inspect /check info about the docker network -
docker network inspect "network_id"
Bridge Network -
It is the default network. When you run the container without giving any other parameter w.r.t. network.
It's the most commonly used network type for running containers on a single host. It provides a private internal network for containers on the same host, allowing them to communicate with each other and with the host system. Containers connected to a bridge network can communicate with each other as well as with the host machine. You can create a bridge network explicitly using the docker network create
command.
How bridge network works - When you install the docker engine on your machine one network called bridge network is created which can be identified by docker0 . You can ensure this by -
ip link
#here you will see docker0
#here you can also see the current status
#you can also see it has ip address assign
ip addr
Now create a container and assign a network to it -
Run the container without any network -
docker run -itd ubuntu
#check status by
docker ps
#inspect container
docker inspect "container_id"
If you want to create 2 separate networks that don't communicate to each other/isolated from each other. This can be done by custom network -
docker network create --driver=bridge --subnet=182.1.0.1/16 isolatedNetwork
#now you see new network is created verify by running the command
docker network ls
docker network inspect "network_id"
Now let's run a container that connects to this network
docker run -itd --name=testUbuntu --net=isolatedNetwork "image_name"
#verify it by
docker ps
#inspec by
docker inspec "that container id"
Now let's assume a scenario where your container is running and you want to connect it to another/new network
docker network connect "network_name" "container_id"
#verify by
docker inspect "container_id"
# see there in network for more details
You can also disconnect by -
docker network disconnect "network_name" "container_id"
Stop all the running containers at once -
docker stop $(docker ps -aq)
#and also remove all the container by
docker rm $(docker ps -aq)
Remove all the containers by -
docker rm $(docker ps -aq)
To delete any network -
docker network ls
#list docker network
docker network rm "network_id"
#to delete all the network at once
docker network prune
#delete all the custom networks in one go
None Network -
The "none" network mode is a special network mode that completely isolates a container from the network. When you run a container in "none" network mode, it doesn't have any network interfaces or network connectivity. This can be useful for scenarios where you want to run a container without any network access for security or isolation purposes. to run a container in "none" network mode using the docker run
command:
docker run --network none my_container_image
#by default 3 networks are there - Bridge,Host and Null
--network none
: This flag specifies that the container should run in "none" network mode.my_container_image
: Replace this with the name of the Docker image you want to run.
Host Network -
The "host" network mode in Docker allows a container to share the network namespace with the host system. In other words, when you run a container in "host" network mode, it doesn't have its isolated network stack; instead, it uses the network stack of the host machine. This can be useful in scenarios where you want maximum network performance or when you need a container to have direct access to the host's network interfaces and ports.
docker run --network host my_container_image
--network host
: This flag specifies that the container should use the host's network stack.
Communicating between two docker containers -
First, create a new network -
docker network create --driver=bridge --subnet=182.0.1.1/16 isolatedNetwork
#by this network is created
Now create a container of CentOS image -
docker run -itd centos
#now by this a centOS container is running
#check it by
docker ps
Now let's run another container
docker run -itd --name=test1 --net=isolatedNetwork centos
#now check the status
docker ps
#inspect these two networks by
docker inspect "container_id"
Now let's try to make communication happen between these two containers -
docker exec -it "container_id" /bin/bash
#now you entered in the container terminal and try
ping test1
#here you see name or service not known
#disconnect by
exit
means your container doesn't know about test1.
Now let's connect the test1 container to the serene_archimedes container.
docker network connect isolatedNetwork serene_archimedes
#now it got connected
#go to again in bin/bash
docker exec -it d2 /bin/bash
ping test1
#now you see these two containers connected to each other
#and ping is working
#you can try this ping with ip as well
That means two containers can connect with the name or ID.
Docker Volumes -
in this we will see how docker persist the data .
Why we need docker volume?. So whenever we create a docker container it consists of two layers 1)Container Layer(Writable Layer) 2) Docker Image Layer(Read Only Layer)
You cannot make changes in docker image layer. Any changes done in the container is Container Layer called writable layer
Go to the path
cd /var/lib/docker
cd volumes
ls -lrt
#here you see the directories
By default, you create any volume and map it to the container it gets created inside Volume. We can create our own volume by
docker volume create data_volume
cd data_volume
#here by default _data directory created
cd _data
#this folder is empty
Now let's run a container
docker run -itd -v data_volume:/www ubuntu
#here www is the directory inside the container which we want to map as data volume
docker ps
-v for volume, data_volume -(name of volume): (here we map some directory inside the container). Here www, then the name of the image
Now connect to the container
docker exec -it "container_id" /bin/bash
# now you are inside the container
ls -lrt
#here we see www folder is created
go inside the www folder
cd www
echo "for testing purposes" > test.txt
ls #here you will see a file created called test
#now lets disconnect from there and check the host folders inside docker folders
exit
Now let's stop the container
docker stop "containe_id"
#by this you see docker container stopped and nothing is running
docker ps
Now check again if the file is present in host os or not
ls
#file is still there
Now remove the container
docker rm 26
#26 = container_id
#here you see file is still there
Now let's run another container and connect to this volume
docker run -itd -v data_volume:/www ubuntu
#here Ubuntu is an image
docker ps #check
Here we map the same volume with a new container
So connect to it by
docker exec -it "container-id" /bin/bash
This is how we map the volume, and even if we delete the container file still exists. This is the default volume that we create.
Docker Bind Volumes -
Difference between volume mount and bind mount is
#make a directory in host os
mkdir data
#to see the directory
ls -ltr
Now we create a volume rather than run a container
docker run -itd -v /home/helloabhii/data:/www ubuntu
docker ps
Now let's connect this container
docker exec -it 73 /bin/bash
Now go to the directory
#here we are inside the container
cd www
ls
#till now this is empty
echo "test data " > test.txt
ls
#here now we see the test.txt file
#now disconnect the container by
exit
Now go to the host directory data and check the data
cd data
ls
#here you see
#that file exist
docker volume ls
Docker introduce a new way to create bin volumes
docker run -itd --mount type=bind,source=/home/helloabhii/data,target=/www ubuntu
docker ps
docker exec -it "container_id" /bin/bash
now go to the path
cd www
ls
#now here you see that test.txt exists
exit
#exit out of container
This is how we create a bind type using the new command.
Now to delete any volume -
docker volume ls #list the volumes
#first stop the container
docker stop $(docker ps -aq)
#then remove
docker remove$(docker ps-aq)
#to remove the volume
docer volume rm data_volume
#now to confirm see the volume list
docker volume ls
This is the introduction to the Docker.
That's it For Now BYE BYE !!!