What is Docker and why it is used?
Docker is an open-source containerization platform. It enables developers to package applications into containers with standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.
Why should we use Docker?
As Docker reduces the need for more infrastructure resources for development and the container created for individual processes can be shared with other apps with instances of these containerized apps using less memory compared to virtual machines – it makes the development and deployment process more cost-effective.
What is Docker Registry?
Docker registry is a centralized storage location for docker images Docker registry helps in image management, image scanning & image signing workflow.
Docker hub is the default online public registry However, we can have our private registry using Docker hub, AWS ECR, Azure container registry ACR, Google container registry, JFrog container registry.
Docker Architecture
Docker uses a client and server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface. Another Docker client is Docker Compose, which lets you work with applications consisting of a set of containers.
The Docker client
The Docker client is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to docker, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.
Docker registries
A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can even run your private registry.
When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry.
Docker Images
An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.
For example, you may build an image that is based on the Ubuntu image but installs the Apache web server and your application, as well as the configuration details needed to make your application run.
You might create your images or you might only use those created by others and published in a registry. To build your image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it.
Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt.
This is part of what makes images so lightweight, small, and fast when compared to other virtualization technologies.
Sample Docker File Syntax
FROM defines the base used to start the build process.
ADD copies the files from a source on the host into the container’s filesystem at the set destination.
COPY copies the files from a source on the host into the container’s filesystem at the set destination.
CMD can be used for executing a specific command within the container.
ENTRYPOINT sets a default application to be used every time a container is created with the image.
ENV sets an environment variable.
EXPOSE associates a specific port to enable networking between the container and the outside.
MAINTAINER defines the full name and email address of the image creator. RUN is the central executing directive for Docker files. USER sets the UID or (or username) which is to run the container.
VOLUME is used to enable access from the container to a directory on the host machine.
WORKDIR sets the path where the command, defines with CMD, is to be executed.
LABEL allows you to add a label to your docker image.
Docker file example:
FROM ubuntu:18.04
LABEL maintainer "admin@gmail.com"
RUN apt-get update && apt-get -y install apache2
EXPOSE 80
ENTRYPOINT ["/usr/sbin/apachectl"]
CMD ["-D","FOREGROUND"]
Commands for Installing Docker on Ubuntu Instance
Docker Installation Steps
Launch Ubuntu Instance and Run the below commands
• Sudo apt-get update (Update VM)
• Sudo apt-get install docker.io -y (install docker)
• Docker version (check the version of docker)
• Sudo chmod -R 777 /var/run/docker.sock (give sudo Permission to Docker Demon )
Docker Commands
• Docker search ubuntu (docker search command will give you the list of available versions )
• Docker pull ubuntu (pull docker image ex. Ubuntu with the latest tag)
• Docker images (This command will list the available images)
• Docker run -itd ubuntu (run docker image with an interactive terminal )
• Docker ps (to view the list of running commands )
• Docker ps -a ( to see all the running and stopped containers )
• Docker stop ContainerID
ex: docker stop f75f1408c595
• Docker attach ( enter inside of the container ) attach command will enter into the container.
ex: docker attach ce28b2a85f7e
• Apt-get update -y (update the container again )
• Apt-get install default-jdk -y (install default java packages )
• Apt-get install wget -y (install wget packages )
• Apt-get install vim -y (install vim packages)
• Apt-get install apache2 -y (install apache2 webserver packages )
*ctrl+pq to exit the container without stopping.
*Docker login (login docker hub with username& password)
• Docker commit {ex. Docker repo/mydemorepo/myimage: tagname}
• Docker push imagename:tag (push docker image from local to remote repository) (Refresh the docker hub repository our local docker image will be reflected at remote docker hub repository)
ex: docker push raghava0617/image1:latest
The docker image got pushed from the Local instance to DockerHub
Pulling Docker Image from Dockerhub to local Virtual machine
• The command is docker pull raghava0617/image1:latest
• docker run -itd -p 8080:80 raghava0617/image1 (Running the container in interactive mode & mapping the port to container )
• docker logs -f ce28b2a85f7e (to view the logs of Running container)
• docker start (CID container id & CNAME container name) ( start the container )
• docker stop (CID & CNAME) (for stopping the Running container use docker stop)
• docker rm (CID & CNAME) delete container [before deleting container stop the container and delete the container] (use the docker rm command to delete the container make sure to stop the running container before deleting )
for more information kindly check this page
https://dockerlabs.collabnix.com/docker/cheatsheet/
Thanks for reading :)