Docker :
A container is a standard unit of software that packages up code and all its dependencies so tthe application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everthing needed to run an application: code, runtime, system tools, system libraries and settings.
1. # /home => it is home directory for other users (eg : /home/ram, /home/gues..)
2. /usr => by default, software’s are installed in /usr (unix shareable resource)
3. /bin => It contains commands used by all users (ls, ping, cat etc..)
4. /sbin => This directory contains essential system binaries(super user, system admin – fdisk, init, reboot …)
5. /var =>
Pull Image -> Run Image [Create Container] -> Run Appliation Program
Docker :
1. “docker info”
2. “docker –version”
3. To download docker image from HUB : “docker pull hello-world”
4. “show images : docker images”
5. run images : “docker run hello-world”
6. process status : “docker ps”
7. stopped containers : “docker ps -a”
8. Build Docker Image : “docker build -t my-simple-image”
Running a Node Container
9. To download image from hub : “docker pull node”
10. After running a image then only container will be created : “docker run -it node”
Here “-it” is used to make container interactive with nodejs “i- interactive”, “t-TTY”
11. We can run container either by ID or Name : docker start “
12. To stop docker container : docker stop “
13. To start the existing container : “docker start
14. To start the existing container with interactive (Running commands inside Container) :
“docker exec -it jolly_sinoussi node”
“docker exec
“docker exec -it jolly_sinoussi /bin/bash”
15. root@xyz: /# ls => list linux os directories
16. To go to node directory/binaries in linux : “cd /usr/local/bin” => # ls => node npm yarn npx docker-entrypoint.sh nodejs
17. Now while execute node shell command it will run in it : # node
Every container as a OS , with in that OS , our applcation program (node) will exist
18. Tage Container Name : “docker run –name node_image node”
19. Rename Container : “docker rename node_image my_node_image(dest name)”
20. To delete stopped containers all : docker container prune
21. Copying File to Container :
1. create a mini OS : “docker pull alpine”
2. create container : “docker run -it –name my_alpine_container alpine(new name)”
3. it will open cmd prompt : /# => ls
4. Open new window cmd prompt, dont stop/close the previous cmd prompt
4. Go to windows file location(source) in local machine
5. docker cp .\my-example.txt my_alpine_container:/example.txt
docker cp .\
docker cp .\my-example.txt my_alpine_container:/var/example.txt
6. Go to container location , find : /# cd var/ => ls =>
7. Open the file to read by cmd : /var # cat example.txt
22. Copy file from Container :
1. Open windows cmd prompt & run :
docker cp my_alpine_container:/example.txt ./my-example-copy.txt
23. Publishing Docker Images :
1. Build Image : “src/my-simple-image”
Dockerfile
FROM alpine:latest
CMD echo “Hello World from DIT”
2. “docker build -t my-simple-image .(current directory/folder path)”
24. Publishing Image to Docker Hub :
1. Login into Docker HUB
2. Create a repository
3. In local machine run the cmd to build: “docker build -t first-dock-repo .”
4. Docker image file will be prepared, if you check in local docker desktop we can see the image file “first-dock-repo” , this is going to be published in docker hub.
5. Repository name “kprabha25/first-repo” and local image name “first-dock-repo”
6. Login local machine using cmd prompt : “docker login”, it will ask username & password
7. Before push you can tag name like ‘latest’ :
docker tag
docker tag first-dock-repo kprabha25/first-repo:latest
8. Push into docker hub : docker push kprabha25/first-repo:latest
9. Pulling & Running DockerHub Image
1. In Docker desktop now it will be two images , one from local and another with username/repository
2. Delete those two before pull it
3. “docker pull kprabha25/first-repo:latest”
4. To check : docker run -it kprabha25/first-repo:latest
25. To come out from container : linux cmd /# exit
26. To remove container : docker rm docker-nginx
27. To run a container in background, use “-d”: “docker run –name
28. To find container ip address : docker inspect
29. To stop container : docker stop
30. docker attache dckr-nginx (get into container)
Docker for Beginners in Tamil | Docker Hub | 1 Hour Full Tutorial
www.docker.com/play-with-docker
https://www.virtualbox.org/
https://busybox.net – light weight linux os
sudo yum update
sudo yum install yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager –add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
docker run ubuntu bash -> it will pull image and run and exit immediately docker run
docker run -it ubuntu bash -> it will pull image and run continously. After run container then it will go to terminal
/# ls, /# hostname
/# ls bin -> Container directory structures list
1.every time we run image a new container id will be created
/# hostname => new id (container id or OS id)
[ec2-user@abc ~]$ ls bin //this is normal user
[ec2-user@abc ~]$ sudo su// switch to super user
[ec2-user@abc ~]$ ls bin
[ec2-user@abc ~]$ pwd -> /home/ec2-user
Building a web page to serve on Nginx
Docker Desktop Installed in the Windows Machine can run Linux Based Docker Containers and Windows-based Docker Container. But You cannot run Windows Based Docker Containers on Docker Engine installed in Linux. Refer the below image.
the -p 3000:3000 option in the docker run command maps port 3000 on the host machine to port 3000 inside the container. This means that any network traffic sent to port 3000 on the host will be forwarded to port 3000 in the container.
Docker on Windows uses a virtualization layer to run Linux containers, typically through Hyper-V or WSL2 (Windows Subsystem for Linux 2). This setup creates a virtualized environment that runs a Linux kernel, which Docker uses to run containers. Even though the container runs in a Linux environment, Docker for Windows provides seamless networking between the Windows host and the Linux containers.
When you run a Docker container on Windows with the -p option, the following happens:
Virtualization Layer:
Docker Desktop on Windows uses a lightweight VM (with Hyper-V) or WSL2 to run Linux containers.
This VM or WSL2 environment has its own IP address and networking setup.
Port Mapping:
The -p 3000:3000` option tells Docker to forward traffic from port 3000 on the host (Windows) to port 3000 in the container.
Docker Desktop configures this networking so that the ports on the Windows host are mapped correctly to the virtualized environment running the containers.
Accessing from Windows:
When you access http://localhost:3000 on your Windows machine, Docker Desktop forwards this traffic to the container running inside the VM or WSL2 environment.
This makes it appear as if the container is running directly on your Windows host, even though it’s actually running inside a virtualized Linux environment.
——–
Nginx
To install vim (or any other package) in a running Docker container, you need to follow these steps:
0.Run or attach to the Nginx container:
docker run –name dckr-nginx -p 80:80 -d nginx
docker run –name dckr-node -d node
1.Access the running container’s shell:
docker exec -it dckr-nginx /bin/bash
mkdir -p ~/dckr-nginx/html
cd ~/dckr-nginx/html/
pwd
vim index.html
Insert mode
esc + shit : + wq
cat index.html
2.Update the package list inside the container:
apt-get update
3.Install vim:
apt-get install vim -y
Once vim is installed, you can use it to edit files within the container.
Here’s the complete process:
1.Start your Nginx container (if it’s not already running):
docker run –name dckr-nginx -p 80:80 -d nginx
2.Access the running container:
docker exec -it dckr-nginx /bin/bash
3.Update the package list:
apt-get update
4.Install vim:
apt-get install vim -y
5.Edit your HTML file using vim:
vim /usr/share/nginx/html/index.html
6.Make your changes, save the file, and exit vim:
-Press i to enter insert mode.
-Make your changes.
-Press Esc to exit insert mode.
-Type :wq and press Enter to save and exit vim.
7.Exit the container’s shell:
exit
——–
Copy the file from windows directory to nginx container and run localhost from windows machine to test
To run an Nginx container in detached mode while copying a file from your local machine to the container volume, you can use the following docker run command. This command will create and start the container, bind mount the local directory to a directory in the container, and expose port 80.
0.Run or attach to the Nginx container:
docker run –name new-dckr-nginx -p 80:80 -d nginx
1.Access the running container’s shell:
docker exec -it new-dckr-nginx /bin/bash
docker run –name new-dckr-nginx -p 80:80 -d -v
“docker run –name new-dckr-nginx -p 80:80 -d -v C:/Users/Admin/Desktop/mlc/Linux/my-repo:/usr/share/nginx/html nginx”
5. Stop incase nginx running in windows machine
6. Test http://localhost/
4.Exit the container shell:
exit
5.To Stop and remove the temporary container:
docker stop new-dckr-nginx
docker rm new-dckr-nginx
6.Copy the HTML file from the container to your local machine (if you want to edit it locally):
docker cp new-dckr-nginx:/usr/share/nginx/html/index.html C:/Users/Admin/Desktop/mlc/Linux/dest-loc
Successfully copied 2.05kB to C:\Users\Admin\Desktop\mlc\Linux\dest-loc
7.Edit the HTML file locally: Use your preferred text editor to make the necessary changes to the HTML file located in /path/to/your/local/directory.
8.Copy the edited HTML file back into the container from local windows :
1. Copy a Single HTML File:
docker cp C:/Users/Admin/Desktop/mlc/Linux/dest-loc/index.html new-dckr-nginx:/usr/share/nginx/html/
2. Copy All Files from a Local Directory to the Container’s HTML Directory:
docker cp C:/Users/Admin/Desktop/mlc/Linux/dest-loc new-dckr-nginx:/usr/share/nginx/html
9. Restart the Container (if necessary):
docker restart new-dckr-nginx
—–
Identify the HTML file location: By default, Nginx serves files from /usr/share/nginx/html
To determine the appropriate container path where you need to copy files or mount a directory, you usually refer to the documentation of the container image you are using. For the Nginx container, the common paths are:
Nginx container, the common paths are
/usr/share/nginx/html for static website files.
/etc/nginx/nginx.conf for the main configuration file.
/etc/nginx/conf.d/ for additional configuration files.
You can also inspect a running container to see the filesystem structure and find the correct path. Here’s how you can do it:
1.Run the Nginx container (without mounting the volume first):
docker run –name temp-nginx -d nginx
2.Execute a shell inside the running container:
docker exec -it temp-nginx /bin/bash
3.Explore the filesystem to find the path you need:
For example, you can list the contents of the Nginx configuration directory:
ls /etc/nginx
Linking the container to the local filesystem
– to handle data in container, we need volume , we use “-v”
Here’s how it works:
-p 80:80: Maps port 80 of the host to port 80 of the container.
4.To Stop and remove the temporary container:
docker stop new-dckr-nginx
docker rm new-dckr-nginx
——
docker run -v $PWD:/app -w node node index.js