Docker
What is Docker?
Docker is basically a software which helps up for creating rapper around the files of the code, operating
system and the libraries that we required for particular code to work.
Docker is a software or tool which helps us containerizing an application.
Architecture of Docker:
Docker installation:
Installation on MAC:
We need to install docker toolbox for MAC, for using docker
https://store.docker.com/editions/community/docker-ce-desktop-mac
Installation on WINDOWS:
https://store.docker.com/editions/community/docker-ce-desktop-windows
installation on UBANTU:
sudo apt-get update
sudo apt-get install docker.io
Docker contained lifecycle:
Common docker operations:
docker –version
sudo docker pull <image-name>
sudo docker images
sudo docker run -it -d <image-name>
-it – make the container interactive with the terminal
-d – run the container in the background
sudo docker ps
show list of containers which are running currently on system
sudo docker stop <container-id>
stop the running container
sudo docker ps -a – see all containers available on system
sudo docker exec -it <container-id> bash
get inside in the container
bash – run this container in the current terminal space that I am working in
exit
make you come out to host operating system
docker kill <container-id>
kill the container by stopping its execution immediately.
The difference between ‘docker kill’ and ‘docker stop’ is ‘docker stop’ gives the container time
to shutdown gracefully, in situation when it is taking to much time for getting the container stop, one
can opt to kill it.
sudo docker rm <container-id>
remove container from system
rm – remove a stopped container from system
sudo docker rm -f <container-id>
delete a container which is running
sudo docker rmi <image-id>
delete an image from system
sudo dokcer rm -f $(sudo dokcer ps -a -q)
remove all the containers at once which are present in system
Creating a docker hub account:
Common docker operations:
Saving changes to a container
docker commit <container-id> <name-for-image>
with this command, a new image is created which can be seen under docker images with the
same as passed in the command
Whenever you want an image push to docker hub account there is certain nomenclature that you have
to follow when you naming when naming image as follows
<user-id-of-docker-hub-account>/<new-name-of-container>
e.g. amson/apache
sudo dokcer run -it -p -d amson/apache
-p – it does port mapping
sudo dokcer run -it -p 82:80 -d amson/apache
Map internal port of the container to the outside host operating system
Port 82 is port of host operating system
Port 80 is port of container
Due to this we can access apache install in container using the port that mapped(82:80) to the
container.
Pushing container to Docker Hub:
Login to docker dub:
sudo docker login
login to the docker hub
after above command enter username and password of docker hub account.
sudo docker push amson/apache
push custome image to docker hub
Introduction to Dockerfile:
What is Dockerfile?
A Dockerfile is a text document that contains all the commands a user could call on the command line to
assemble an image. Using docker build users can create an automated build that executes several
command-line instructions in succession.
Various commands in Dockerfile:
The first line in dockerfile is always FROM
FROM:
FROM keyword is used to define the base image on which we will be building.
e.g. FROM ubantu
ADD:
ADD keyword is used to add files to the container being build.
Syntax: ADD <place where files are present> <location inside the container where all those files we want
to copy>
e.g. ADD . /var/www/html
RUN:
RUN keyword is used to run any command in the container
RUN keyword is used to add layer to the base image by installing components. Each RUN statement
adds a new layer to the docker image.
e.g. RUN apt-get update
RUN apt-get -y install apache2
CMD:
The CMD keyword is used to run commands on the start of the container. These commands run only
when there is no argument specified while running the container in the docker run command.
e.g. CMD apachectl -D FOREGROUND
ENTRYPOINT:
The ENTRYPOINT keyword is used strictly to run commands the moment the container initializes. The
difference between CMD and ENTRYPOINT is, ENTRYPOINT will run irrespective of the fact whether
argument is specified or not.
CMD and ENTRPOINT can be used interchangeably.
e.g. ENTRYPOINT apachectl -D FOREGROUND
ENV:
The ENV keyword is used to define environment variables in the contained runtime.
e.g. ENV name Devops intellipaah
Note: The name of the docker file must be Dockerfile
e.g. Dockerfile
FORM ubantu // ubantu image to be called
RUN apt-get update // update this image
RUN apt-get -y install apache2 // install apache inside image
ADD . /var/www/html // Add all the files from this directory to /var/www/html directory
ENTRYPOINT apachectl -D FOREGROUND // run apache in the foreground
ENV name <intellipaat>
Build this docker file:
sudo docker build . -t new_dockerfile
Dockerfile will execute after this command and build the new image through Dokcerfile.
Run dokcer commands without sudo:
sudo usermod -aG docker $USER – run this command and then re-login into your session.
Introduction to Docker Volumes:
Docker Volumes are used to persist data across the lifetime of a container.
It basically posts the storage outside the container and maps it to inside the container that is storage is
there in host system rather than on the container. Rather than the files are retain on the container itself
they are retain outside the container and that location is basically mapped inside container.
There are two ways to do this.
1. Bind mount
Mount the particular file location inside the container
docker run -it -v /home/ubantu/dockerfile:/app
Bind Mount only work with the file system is the same that you have specified. It will not work
in different environments.
It does not manage by the docker because we create the directory, we decide the which
directory our data is gone a resided and inside the directory we are making the changes and that have
been reflected in that container.
It is difficult to migrate the data.
2. Docker Volumes
Docker volume is basically as storage which is managed by docker.
If we create a volume, the docker engine automatically identified where this volume has to
exists and then create volume over there.
It is very easy to migrate the data.
Syntax : docker valume create <name-of-volume>
docker valume ls
listout all the volumes present in the system
Syntax for mounting a volume : dokcer run -it –mount source=<name-of -volume>,target=<path-
to-directory> -d <image-name>
Docker Compose:
It is a tool for defining and running multi-container applications. With docker compose you use a YAML
file to configure your application services. Then, with a single command you create and start all the
services from your configuration. Run docker compose up and compose starts and runs your entire app.
e.g. Docker Compose file:
Docker compose filename should eigher docker-compose.yml or docker-compose.yaml
docker-compose up -d
start all service which are configure in docker-compose file.
Container Orchestration:
If there is a container which goes down or not healthy any more then Docker Swarm is automatically
repair it by stopping the container and launching the new one in place of it.
There are lot of container orchestration tools. With docker pre-packaged comes Docker Swarm.
What is Docker Swarm?
Docker Swarm is a clustering and scheduling tool for docker containers. With Swarm, IT administrators
and developers can establish and manage a cluster of docker nodes as a single virtual system.
While launching worker, we need to some steps on instance to see this is the instance that I want the
leader/manager for my Docker Swarm cluster.
Any node which has to join this manager/leader they will have to pass below command
The command should be passed on the worker node to join the docker swarm cluster.
docker swarm init --advertise-addr=<ip-address-of-leader>
advertise-addr – it is a private ip address of the instance
sudo docker swarm leave
To leave the cluster
sudo docker swarm leave –force
The master to leave the swarm
Deploying an app on Docker Swarm:
What is Service?
Containers on the cluster are deployed using service on the Docker Swarm. A service is a long running
docker container that can be deployed to any node worker.
Creating a service:
dokcer service create --name <name-of-service> --replicas <no-of-replicas> <image-name>
e.g. docker service create –name apache –replicas 3 80:80 amson/webapp
docker service scale <service-name> = <no-of-replicas>
e.g. dokcer service scale apache = 2
docker service rm <name-of-service>
remove service from the container