Docker Notes
Docker Notes
Docker Notes
What is Docker?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to
separate your applications from your infrastructure so you can deliver software quickly. With Docker,
you can manage your infrastructure in the same ways you manage your applications.
Docker release March 2013 by Solomon Hykes and Sebastian
Docker is a platform as a service that uses OS-level virtualization
Docker is an open-source centralized platform designed to create deploy and run
applications
Docker uses a container on the Host OS to run applications. It allows applications to use the
same host computer rather than creating a whole Virtual OS
We can install Docker on any OS but Docker Engine runs natively on Linux distribution.
Docker is written in the Go language.
Docker is a tool that performs OS-level Virtualization, also known as centralization.
Advantage of Docker
No pre-allocation of RAM
Continues Integration (CI) Efficiency -> Docker enables you to build a container image and
use that same image across every step of the deployment process.
Less cost
It is light in weight
It can re-use the image
It can run on physical H/W, Virtual H/W, or on cloud.
It took very little time to create a container.
Disadvantages of Docker
Docker is not a good solution for applications that require rich GUI
Difficult to manage large amounts of containers
Docker does not provide cross-platform compatibility means if an application is designed to
run in a docker container on Windows, then it can`t run on Linux or vice-versa
No solutions for Data recovery and backup
Docker is suitable when the development OS and testing OS are the same.
Docker Client
Docker users can interact with the docker daemon through a client (CLI)
The Docker client uses CLI 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
The client can communicate with more than one daemon.
Docker Daemon
Docker daemon runs on the Host OS
It is responsible for running container to manage docker services
Docker daemon can communicate with other daemon
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. It uses a
YAML file to configure the application's services, networks, and volumes, allowing for easy
management of complex applications.
Docker File
A Dockerfile is indeed a text file containing a set of instructions. These instructions define how
to build a Docker image step by step.
Dockerfiles automate the process of creating Docker images, ensuring consistency and
reproducibility in the deployment of applications.
Docker Container
Docker containers are like portable, self-contained boxes for software applications.
They pack code, runtime, libraries, and tools in a single container.
Containers are created from Docker images, acting as pre-made blueprints.
Containers provide a reliable and consistent environment for software to run.
A vital tool for ensuring software consistency in modern development and deployment.
$ yum update
Step 3: Start and Enable its Service: Docker service will not be started and enabled by default after
you complete its installation. We have to do that manually.
Now, if you also want Docker to start automatically with system boot then use this command:
$ docker -v
Or
$ docker --version
$ docker images
(Run means create and start the container) (-it = interactive mode terminal)
(*Every time new container is created)
(run means create and start container) (-it = interactive mode terminal)(--name = give name to your
container)
Upon executing above command, you'll seamlessly enter the container environment.
Post-Command Task
I have a task for you: explore the command that allows you to create a container exclusively without
entering it.
$ cat /etc/os-release
To exit container
$ exit
$ docker ps -a
To start container
To enter a running container, first use the given command to start the container, and then use
a separate command to access its interactive shell.
To stop Container
To delete container
$ docker rm <container_name>
If you want to see the difference between the base image and changes on it then
A Dockerfile is indeed a text file containing a set of instructions. These instructions define how
to build a Docker image step by step.
Dockerfiles automate the process of creating Docker images, ensuring consistency and
reproducibility in the deployment of applications.
Instruction Description
$ vi Dockerfile
……………………………………………………………………………………………………………………………
FROM Ubuntu
RUN echo “Learning Docker”> /tmp/testfile
:wq
……………………………………………………………………………………………………………………………
( -t = use for tag and .(dot) Use the current directory to build a Docker image using the Dockerfile
located in the current directory)
Now you can play with different Dockerfile and try to do experiment by yourself
So, if you have different containers that need to work together or need a place to store important
information, you can create a Docker volume. It's a way for these containers to talk to each other and
share data, making sure nothing important gets lost when the containers are turned off or removed.
Benefits of Volume
Decupling container from storage
Share volume among different containers.
Attach volume to containers
On delete containers, volumes does not delete
$ vi Dockerfile
……………………………………………………………………………………………………………………………
FROM Ubuntu
VOLUME [“/myvolume1”]
:wq
……………………………………………………………………………………………………………………………
$ ls
$ cd myvolume1
$ exit
After creating volcontainer2, myvolume1 is visible whatever you do in one volume, you can see
from other volume let’s see
$ ls
$ cd myvolume1
$ exit
$ ls
$ cd myvolume2/
$ exit
Now Create another Container and share myvolume2 with new container
$ ls
$ cd myvolume2/
$ exit
You can also create files in volcontainer4 volume and see these files in volcontainer3 volume
Try it
Host to container
A host is simply a device, like a computer or a server, that connects to the internet or a network.
(/home/ec2-user = path in host , /container = container volume path , : = means map both together)
$ docker volume ls
( d use for daemon, p use for port or publish, -td use for create and run
container directly without entering it.)
Because I create container using Ubuntu image in the container I hav to use
Ubuntu commands
$ apt-get update
$ apt-get install apache2
$ exit
Also you can try this
Docker exec creates a new process in the container's environment while docker attach just connect
The standard Input/output of the main process inside the container to corresponding standard
Input/output error of current terminal
Or
Docker exec is specifically for running new things in an already started container, be it a shell or some
Other process
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.
4. 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 other Docker containers. Hence -
p includes EXPOSE. This is effectively same as.
$ docker login
Enter username and password
Done you are now connected with docker hub
To create container
$ docker run –it --name <image-name> <dokerID/new-image-name> /bin/bash