Docker
Docker
1. Introduction
1.1. What is Docker?
1.2. Why Use Docker?
1.3. Docker vs. Virtual Machines
2. Getting Started
2.1. Installing Docker
2.2. Starting the Docker Service
3. Docker Basics
3.1. Containers vs. Virtual Machines
3.2. Running Your First Container
4. Docker Commands
4.1. Docker Images
4.2. Docker Containers
4.3. Docker Volumes
4.4. Docker Networking
4.5. Docker Compose
5. Docker Volumes
6. Docker Networking
7. Pushing Docker Images to Docker Hub
8. Dockerfiles
8.1. Sample Dockerfile Examples (Ubuntu, Apache2, Nginx, Node.js, React.js)
9. Basic Questions & Answers
Docker
To run the docker, you have to start the docker service by the below command:
sudo systemctl start docker
Docker runs on Port 80. So, if you couldn’t run docker on your system. Then definitely, there is
some other service that will be running on Port 80 such as Nginx or apache2.
So, first stop that service which is running on Port 80 then, start the docker service.
Theory
Containers
Containers are an abstraction at the app layer that package code and dependencies together.
Multiple containers can run on the same machine and share the same OS kernel with other
containers, each running an isolated process in userspace.
Virtual Machines
Virtual machines are an abstraction of physical hardware turning one server into many servers.
The hypervisor allows multiple VMs to run on a single machine. Each VIM includes a full copy of
an OS, the app, necessary binaries and libraries taking up tens of GBs. VMs can also be slow to
boot.
To Install and configure the Docker on Ubuntu 22.04,
follow the below link:
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-22-0
4
Docker Commands
Docker Images
1. docker pull <image_name> To pull the docker image from the docker
hub.
6. docker image inspect <image_id> To check all the information related to the
image
7. docker image prune To delete all the images from the local server
8. docker image history <image_id> To see all the history related to the image
9. docker image save <image_id> < <desired_image_name>.tar To save the image and all the other
information like history,etc excludingmounted
volumes data.
10. docker image load <desire_image_name>.tar To get the image and all the otherinformation
too.
11. docker image import <desired_image_name>.tar To import the saved image from the
<new_desired_image_name> container as an image.
14. docker run -d -p <images_name>:<tag> To run the container at the specific port
number with the image tag with detach
mode.
20. docker rm $(docker ps -aq) To remove all the containers. But can’t
remove running containers.
21. docker rm -f $(docker ps -aq) To remove all the running containers and
stopped containers.
22. docker run --name <container_id> -d -p 80:80 ngin :latest To give the name to the container.
25. docker inspect <container_id> To see all the details of the specific
container.
26. docker logs <container_id> To see all the logs of the specific container.
27. docker logs -f <container_id> To follow the logs or live logs.
29. docker container top <container_id> To list all the processes running inside the
specific container.
30. docker container stats To see all the CPU utilization, Memory usage,
Inputs, Outputs about every container.
34. docker container attach <container_id> To attach to the container again after
detaching it by -d argument.
36. docker container wait <container_id> Container will wait to kill or stop itself.
Whenever the container will stop or kill then,it
will notify the terminal by “0” status.
39. docker container port <container_id> To know the port mapping of the container
like
40. docker container create <container_name> <command> To create a container only, It will not start the
container. If you want to start the container
then, use docker container start
<container_name> command after this.
41. docker container diff <container_id> To observe the change in the container such
as files creation or deletion, etc from the
previous version.
Note: to check/follow the file creations or
deletion use the watch command:
watch ‘docker container diff
<container_id>’
42. docker container cp <source_file(from local)> To copy the files from the local to the
<container_id:/directory> container's file system.
Such as
44. docker run --name <container_name> --volumes-from To share the container volume
<old_container_name> -d -p 8081:80 ngin :latest
45. docker rm $(docker stop $(docker ps -aq)) The final output of this command is to
remove the containers directly (Time
Saving).
46. docker container run -it --network <your_network_name> It will run the container and attach the
<image_name> network with the container as well.
Volumes
47. docker volume create To create the docker volume
50. docker container commit --author “Author Name” -m “Commit To create the image from the running
Description” <container_id> <desired_image_name> container.
51. docker run --name <container_name> -v $(pwd) To link the file and send the file to the
:/usr/share/ngin /html/:ro -d -p 8080:80 ngin :latest container volume.
52. docker run -d --name 1st -v vocs:/var/lib/mysql -e It will create the docker volume too and run
MYSQL_ALLOW_EMPTY_PASSWORD=true mysql the container as well. So, you don’t have to
write two commands separately instead of
doing both work in one individual command.
53. docker run -it -v It will bind mount your folder into the docker
/home/amanpathak/Documents/:/tmp/new/folder/ ubuntu:latest container.
bash If the destination folder is not present then, it
will auto create the folders that are not
created and If you update any file or folder
on the local machine or host machine then,
the same changes will happen on the docker
container such as modify the file content.
OR
Networking
54. docker network inspect <network_id> To get the all details regarding the docker
network driver
55. docker network create -d <network_type> It will create the network with network driver
<your_network_name> type as well.
56. docker container run -it --network <your_network_name> It will run the container and attach the
<image_name> network with the container as well.
57. docker container run -it --network=none <image_name> It will run the container with a null docker
network driver.
58. docker network connect <network_name> <container_name> To connect the container with th docker
network
59. docker network disconnect <network_name> <container_name> To disconnect the container with th docker
network
Docker Compose
63. docker compose build Build or rebuild services
68. docker compose events Receive real time events from containers.
70. docker compose images List images used by the created containers
75. docker compose port Print the public port for a port binding.
88. docker search <image_name> Allows you to search for Docker images
available on Docker Hub or other container
registries.
89. docker system prune This command cleans up unused data such
as containers, networks, and volumes.
Useful for freeing up disk space.
97. docker image prune -a Removes all unused images, not justdangling
ones.
Docker Volumes:
For the Docker Volume Demo, Go through the video
enp0s10: | en| | --> ethernet v | p0| --> bus number (0) v s10 --> slot number (10)
This is a bridge between the Docker Host and the Linux Host.
Whenever you run a container, it by default attaches with a bridge network driver but if you want
to attach with your desired network driver then, you can do it by the command, in which you
have to specify the network name (assuming that network is already created by you).
Docker Container 1 under the Docker network 1 can access or communicate with Docker
Container 2 and Docker Container 3. Also Every Docker Container can communicate with any
other Docker Container, if that Container is under the same Docker network.
But, any docker container of docker network 1 can’t communicate with any docker container of
docker network 2 because the network is not the same.
So, to access or communicate with other docker network’s containers you can do it with the help
of port mapping.
-> Create two network for Docker containers
-> Create two Docker Containers, First for ubuntu and other one for nginx(with port open by -P
argument)
-> enter in the ubuntu container and use wget to check that the container will communicate with
another network’s container or not.
But as you can see, the wget is not installed then install it first.
-> Now, enter the command wget:<host_machine_ip>
You can see here, that the different docker network container is accessible and can
communicate with the different docker network container.
You can also ping or access the docker container within the same network by the hostname and container
name as well.
I. Create a network
Ii. Create two container, both must be running in detach mode
Iii. Go in the first container and copy the hostname of it by the hostname command.
Iv. Go in the second container and run the command ping <hostname_container1>
When you want to run the docker container with no network driver then, it is a null network
driver.
We cannot attach any network to a container that has none network already attached and we cannot
attach any network if the container is having a host network attach(host cannot be connected to any other
network).
Dockerhub Push commands
This command will build the docker image
docker build --tag ubuntu-mine:latest .
USER user
EXPOSE 80
Apache2/httpd
FROM httpd:latest
COPY . /usr/local/apache2/htdocs/
EXPOSE 80
Nginx
FROM nginx:1.23.0-alpine
COPY . /usr/share/nginx/html/
EXPOSE 80
Nodejs
FROM node:16-alpine
WORKDIR app/
ADD ./package*.json ./
ADD . .
Reactjs
FROM node:16-alpine
WORKDIR app/
ADD package*.json ./
EXPOSE 3000