What is Docker🔽
Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises. Docker is also a company that promotes and evolves this technology, working in collaboration with cloud, Linux, and Windows vendors, including Microsoft.
Docker containers can run anywhere, on-premises in the customer datacenter, in an external service provider or in the cloud, on Azure. Docker image containers can run natively on Linux and Windows. However, Windows images can run only on Windows hosts and Linux images can run on Linux hosts and Windows hosts.
Developers can use development environments on Windows, Linux, or macOS. On the development computer, the developer runs a Docker host where Docker images are deployed, including the app and its dependencies. Developers who work on Linux or on macOS use a Docker host that is Linux based, and they can create images only for Linux containers.
Containers include the application and all its dependencies. However, they share the OS kernel with other containers, running as isolated processes in user space on the host operating system.
A simple analogy🔽
In Docker, each layer is the resulting set of changes that happen to the filesystem after executing a command, such as, installing a program.
So, when you "look" at the filesystem after the layer has been copied, you see all the files, included the layer when the program was installed.
You can think of an image as an auxiliary read-only hard disk ready to be installed in a "computer" where the operating system is already installed.
Similarly, you can think of a container as the "computer" with the image hard disk installed. The container, just like a computer, can be powered on or off.
A package with all the dependencies and information needed to create a container. An image includes all the dependencies (such as frameworks) plus deployment and execution configuration to be used by a container runtime. Usually, an image derives from multiple base images that are layers stacked on top of each other to form the container's filesystem. An image is immutable once it has been created.
A text file that contains instructions for building a Docker image. It's like a batch script, the first line states the base image to begin with and then follow the instructions to install required programs, copy files, and so on, until you get the working environment you need.
The action of building a container image based on the information and context provided by its Dockerfile, plus additional files in the folder where the image is built. You can build images with the following Docker command:
An instance of a Docker image. A container represents the execution of a single application, process, or service. It consists of the contents of a Docker image, an execution environment, and a standard set of instructions. When scaling a service, you create multiple instances of a container from the same image. Or a batch job can create multiple containers from the same image, passing different parameters to each instance.
Offer a writable filesystem that the container can use. Since images are read-only but most programs need to write to the filesystem, volumes add a writable layer, on top of the container image, so the programs have access to a writable filesystem. The program doesn't know it's accessing a layered filesystem, it's just the filesystem as usual. Volumes live in the host system and are managed by Docker.
A mark or label you can apply to images so that different images or versions of the same image (depending on the version number or the target environment) can be identified.
Support me here to help me write more usefull content and get some perks!
A collection of related Docker images, labeled with a tag that indicates the image version. Some repos contain multiple variants of a specific image, such as an image containing SDKs (heavier), an image containing only runtimes (lighter), etc. Those variants can be marked with tags. A single repo can contain platform variants, such as a Linux image and a Windows image.
A service that provides access to repositories. The default registry for most public images is Docker Hub (owned by Docker as an organization). A registry usually contains repositories from multiple teams. Companies often have private registries to store and manage images they've created. Azure Container Registry is another example.
A public registry to upload images and work with them. Docker Hub provides Docker image hosting, public or private registries, build triggers and web hooks, and integration with GitHub and Bitbucket.
Runs on host machine creates and manages docker objects such as images, containers, network, volume, data, etc.
UI for docker which accepts commands from user and communicate with docker host.
When using Docker, a developer creates an app or service and packages it and its dependencies into a container image. An image is a static representation of the app or service and its configuration and dependencies.
To run the app or service, the app's image is instantiated to create a container, which will be running on the Docker host. Containers are initially tested in a development environment or PC.
The registry is like a bookshelf where images are stored and available to be pulled for building containers to run services or web apps. There are private Docker registries on-premises and on the public cloud. Docker Hub is a public registry maintained by Docker.
🌟Concept of Docker:
- Build image
- Ship image
- Run image
First we have to start the docker daemon byservice docker start
To view status of the service use this commandservice docker status
To stop service use this commandservice docker stop
NOTE ~ if Docker was unable to find image on machine it will try to find on DockerHub
🌟Run an Image:
Run an image(hello-world) from DockerHubdocker run hello-world
hello-world is an image. we just made container from it. It will first try to find this image locally then on repository. Default repository is DockerHub
To check existing imagesdocker image ls
We can pull images from registry without making container of it with this commanddocker pull hello-world
To list all the containersdocker ps
By default it will only show running containers. To get all containers we can usedocker ps -a
To get container sizes usedocker ps -s
🌟Launching First Ubuntu Container:
Now we are going to run our Ubuntu container.docker run -it ubuntu:latest bash
- -it flag is for making it interactive.
- We can also specify version I used latest version here.
- Now we can select what we want to use we are using bash here.
- After that we go inside container which contains Ubuntu
- We can execute any ubuntu command that we want inside our containers
- we can install anything that we can install on ubuntu
- when we exit ubuntu & terminal with exit command we also exit from container
Start containerdocker start "container name/id"
- To go inside container use command
- 'Attaching to a container' is the act of starting a terminal session within the context that the container (and any programs therein) is running. This is primarily used for debugging purposes, but may also be needed if specific data needs to be passed to programs running within the container.docker attach "container name/id"
To go inside container without stoping it usectrl q
To get help on all docker commandsdocker --help
To get help on docker container commandsdocker container --help
To get help on docker image commandsdocker image --help
List containersdocker ps -a
List imagesdocker images
Remove containerdocker rm "container id"
Remove imagedocker rmi "image id"
Remove image forcefullydocker rmi -f "image id"
🌟Put container in tar:
To save hello-world container as hello-world.tardocker save -o hello-world.tar hello-world:latest
To add hello-world to registry from tardocker load -i hello-world.tar
🌟Changing image name:
- To create a copy of image hello-world with name hello and add it to registry
- But there image id would be samedocker tag hello-world:latest hello:2
🌟Commiting images to DockerHub:
First we have to commit our changes in that containerdocker commit "image id"
now we have to change name of imagedocker tag "image id" "dockerhub username"/"imagename":tag
now login to our docker accountdocker login
push container image to dockerhubdocker push "dockerhub username"/"imagename"
Done now we can use this image to create containers
Step 1- Create a dockerfilevim dockerfile
Step 2- Write Directives
There are various directives of Dockerfile
FROM shows the base image for exampleFROM python 2.7
WORKDIR sets the working directoryWORKDIR /app
COPY copies new files or directories to containerCOPY ./app
- RUN executes any command in new layer on top of current image and commit result
- Install any needed package specified in requirments.txtRUN pip install -r requirements.txt
CMD provide defaults for executing containerCMD python hello.py
Step 3- Build Image
Build docker image with commanddocker build