Docker
Docker is a container management service.
The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.
Features of Docker :
- It reduce the size of development by providing a smaller footprint of the operating system via containers.
- It becomes easier for teams across different units, such as development, QA and Operations to work seamlessly across applications.
Docker Hub This is the registry which is used to host various Docker images.
Docker Compose is used to define applications using multiple containers.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Docker — Architecture
Standard and traditional architecture of virtualization.
- Server is the physical server .
- The Host OS is the base machine such as Linux or Windows.
- The Hypervisor is used to host virtual machines.
- Guest OS — multiple operating systems as virtual machines.
- host your applications on top of each Guest OS.
— — — — — — — — — — — — —
- Server is the physical server .
- The Host OS is the base machine such as Linux or Windows.
- Docker engine used to run the operating system which earlier used to be virtual machines as Docker containers.
- Apps now run as containers.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Docker Installation
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Docker Info
To see more information on the Docker running on the system
docker info
The output will provide the various details of the Docker installed on the system such as −
- Number of containers
- Number of images
- The storage driver used by Docker
- The root directory used by Docker
- The execution driver used by Docker
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Docker Images
- Listing of Docker Images
To see list of Docker images on the system
sudo docker images
- TAG − This is used to logically tag images.
- Image ID − This is used to uniquely identify the image.
- Created − The number of days since the image was created.
- Virtual Size − The size of the image.
Downloading Docker Images
The following a command is used to download docker image.
docker pull image
- Image − This is the name of the image which is used to run the container.
Removing Docker Images
docker rmi ImageID
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Creating Docker Images
Docker images are build using a docker file.
#This is a sample Image
FROM ubuntu
MAINTAINER demousr@gmail.com RUN apt-get update
RUN apt-get install –y apache2
RUN apt-get install –y apache2-utils
RUN apt-get clean
EXPOSE 80
CMD [“apache2ctl”, “-D”, “FOREGROUND”]
- The first line “#This is a sample Image” is a comment. You can add comments to the Docker File with the help of the # command
- The next line has to start with the FROM keyword. It tells docker, from which base image you want to base your image from. In our example, we are creating an image from the ubuntu image.
- The next command is the person who is going to maintain this image. Here you specify the MAINTAINER keyword and just mention the email ID.
- The RUN command is used to run instructions against the image.
- The EXPOSE command is used to expose port 80 of Apache in the container to the Docker host.
- Finally, the CMD command is used to run apache2 in the background.
docker build
This method allows the users to build their own Docker images.
docker build -t ImageName:TagName dir
- -t − is to mention a tag to the image
- ImageName − This is the name you want to give to your image.
- TagName − This is the tag you want to give to your image.
- Dir − The directory where the Docker File is present.
Host Docker images to the Docker Hub
sudo docker login
docker tag imageID Repositoryname:TagName
docker push Repositoryname
- TagName − This is the tag you want to give to your image.
- Repositoryname − repository name to which image needs be pushed
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Docker Containers
Containers are instances of Docker images
Running a Container, docker container are build from docker images.
To run CentOS image available in Docker Hub on our Ubuntu machine.
sudo docker run –it centos /bin/bash
We used this command to create a new container and then used the Ctrl+P+Q command to exit out of the container. It ensures that the container still exists even after we exit from the container.
- Here, centos is the name of the image we want to download from Docker Hub and install on our Ubuntu machine.
- ─it is used to mention that we want to run in interactive mode.
- /bin/bash is used to run the bash shell once CentOS is up and running.
To run Jenkins, you need to run the following command −
sudo docker run -p 8080:8080 -p 50000:50000 –v /home/demo:/var/jenkins_home –-name=jenkinsa -d jenkins
- Jenkins is the name of the image we want to download from Docker hub .
- -p is used to map the port number of the internal Docker image to our main Ubuntu server so that we can access the container accordingly.
- –d option is used to run the container as a daemon process.
Listing of Containers
This command is used to return the currently running containers.
docker ps
This command is used to list all of the containers on the system
docker ps --all
Working with Containers
- ContainerID − Container ID for which you want to see the top processes.
To see the top processes within a container.
docker top ContainerID
To stop a running container.
docker stop ContainerID
To delete a container.
docker rm ContainerID
To provide the statistics of a running container.
docker stats ContainerID
To attach to a running container.
docker attach ContainerID
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Docker File Commands
ENTRYPOINT
used to execute commands at runtime for the container.
ENTRYPOINT [“echo”]
While running container
sudo docker run Imagename Hello World
will print
Hello World
ENV
This command is used to set environment variables in the container.
ENV var1=Tutorial var2=point
WORKDIR
This command is used to set the working directory of the container.
WORKDIR /newtemp
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Container Linking
Container Linking allows multiple containers to link with each other. It is a better option than exposing ports.
Source container.
sudo docker run -p 8080:8080 –-name=jenkinsa -d jenkins
Destination container
sudo docker run --name="rec" --link=jenkinsa:alias-src -it ubuntu:latest /bin/bash
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Glossary
Docker Installation
Docker Images
- Listing of Docker Images
- Downloading Docker Images
- Removing Docker Images
Docker Containers
- Running a Container
- Listing of Containers
Working with Containers
- Top processes within a container.
- Stop a running container.
- Delete a container.
- Statistics of a running container.
- Attach to a running container.
Docker File Commands
- ENTRYPOINT
- ENV
Container Linking