Docker PDF
Docker PDF
Docker PDF
Components of Docker:
A. Docker Daemon:
➢ Docker daemon runs on host O.S.
➢ It is responsible for running containers to manages docker services.
➢ Docker daemon can communicate with other daemons.
B. Docker Client:
➢ Docker users can interact with docker through a client.
➢ Docker client uses commands and REST API to communicate with the docker
daemon.
➢ When a client runs any server command on the docker client terminal, the client
terminal sends these docker commands to the docker daemon.
➢ It is possible for docker client to communicate with more than one daemon.
C. Docker Host:
➢ Docker host is used to provide an environment to execute and run applications.
➢ It contains the docker daemon, images, containers, networks and storages.
E. Docker Image:
➢ Docker images are the read only binary templates used to create docker containers.
or
➢ Single file with all the dependencies and configuration required to run a program.
F. Docker Containers:
➢ Containers hold the entire packages that is needed to run the application.
Or
➢ In other words, we can say that the image is a template and the container is a copy
if that template.
➢ Container is like a virtual machine.
➢ Images becomes container when they run on docker engine.
Basic Docker Commands:
To see all images present in your local repo:
# docker images
To start container
#docker start container_name
To go inside container
# docker attach container_name
To stop container
# docker stop container_name
To delete a container
# docker rm container_name
#cd tmp/
Now if you want to see the difference between the basic image and the changes on it
# docker diff container_name image_name
Dockerfile:
Dockerfile is basically a text file. It contains some set of instructions. Automation of docker image
creation.
Dockerfile components:
FROM: for base image, this command must be on the top of the dockerfile.
RUN: to execute commands, it will create a layer in image
MAINTAINER: author/ owner/ description
COPY: copy files from local system (docker vm) we need to provide source, destination (we can’t
download file from internet and any remote repo.)
ADD: similar to copy but it provides a feature to download files from internet, also extract file at
docker image side.
EXPOSE: to expose ports such as port 8080 for tomcat , port 80 for nginx etc.
CMD: execute commands but during container creation.
ENTRYPOINT: similar to CMD but has higher priority over CMD, first commands will be
executed by ENTRYPOIN only.
ENV: environment variables
Dockerfile
➢ Create a file named Dockerfile
➢ Add instructions in Dockerfile
➢ Build dockerfile to create image
➢ Run image to create container
# vi Dockerfile
FROM ubuntu
RUN echo “Nagarjuna hota” > /tmp/testfile
To create image out of Dockerfile
# docker build -t myfile
#docker ps -a
# docker image
Now create container from the above image
#docker run -it --name mycon mying /bin/bash
#cat /tmp/testfile
#vi dockerfile
FROM ubuntu
WORKDIR /tmp
RUN echo “thank you” > /tmp/testfile
ENV myname naga
COPY testfile1 /tmp
ADD test.tar.gz /tmp
Docker Volume:
➢ Volume is simply a directory inside our container.
➢ Finally, we have to declare this directory as a volume and then share volume.
➢ Even if we stop the container still, we can access volume.
➢ Volume will be created in one container.
➢ You can declare a directory as a volume only while creating container.
➢ You can’t create volume from existing container.
➢ You can share one volume across any number of containers.
➢ Volume will not be included when you update an image.
➢ You can map volume in two ways:
a. Container to container
b. Host to container
Benefits of Volume:
➢ Decoupling container from storage.
➢ Share volume among different containers.
➢ Attach volume to containers.
➢ On deleting container volume doesn’t delete.
Creating Volume from Dockerfile:
Create a Dockerfile and write
FROM ubuntu
VOLUME “myvolume”
Now after creating container2, myvolume is visible. Whatever you do in one volume, can see from
other volume.
#touch /myvolume/samplefile
#docker start container1
# docker attach container1
#ls/myvolume
You can see sample file here then exit.
1. If you specify neither expose nor -p, the service in the container will only be accessible
from inside the container itself.
2. If you expose a port, the service in the container is not accessible from outside docker but
from inside other docker containers so this is good for inter-container communication.
3. If you expose and -p a port, the service in the container is accessible from anywhere even
outside docker.
If you do –p but do not expose docker does an implicit expose. This is because if a port is open to
the public, it is automatically also open to the other docker containers. Hence -p includes expose.