Showing posts with label Docker. Show all posts
Showing posts with label Docker. Show all posts

Mar 4, 2023

Docker - working steps | Part-5 Manage Docker with Docker Hub

Create your own docker account. 

Your Docker ID becomes your user namespace for hosted Docker services, and becomes your username on the Docker forums. To create a new Docker ID: Go to the Docker Hub signup page. Enter a username that will become your Docker ID.

We can consider Docker Hub as a service from Docker for locating and distributing containers for each image within the specified team. It is the most comprehensive repository of container images, with an order of all content sources, including open-source projects, developing the sharing of the application source code, and community developers using containers.

Let us work with Docker and Docker hub:

-- find your images from your docker server

# docker images
REPOSITORY     TAG       IMAGE ID       CREATED       SIZE
demoimagevol   latest    44787e8aa6c0   5 days ago    77.8MB
ubuntu         latest    58db3edaf2be   5 weeks ago   77.8MB

-- create tag for one of image

docker tag <docker-host-imagename> <docker-hub-username>/<image-name>

e.g.,

# docker tag demoimagevol gmohapat/demoimagevol

# docker images
REPOSITORY              TAG       IMAGE ID       CREATED       SIZE
gmohapat/demoimagevol   latest    44787e8aa6c0   5 days ago    77.8MB
demoimagevol            latest    44787e8aa6c0   5 days ago    77.8MB
ubuntu                  latest    58db3edaf2be   5 weeks ago   77.8MB

-- login to docker hub from docker server

# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: gmohapat
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
#


-- Now push your docker image to docker hub

# docker push gmohapat/demoimagevol
Using default tag: latest
The push refers to repository [docker.io/gmohapat/demoimagevol]
c5ff2d88f679: Mounted from library/tomcat
latest: digest: sha256:681c232cd9bdd7f14b5f468271fcdcb4d0acd5f7fa7bb4f6167113662e3350e3 size: 529
#


Now login to docker hub and see.
Yes, I able to my docker image in docker hub.














-- Now pull the same image from docker hub to docker server.

before doing that you need to delete the image what is created now. with same name we can't keep two images.

# docker images
REPOSITORY              TAG       IMAGE ID       CREATED       SIZE
gmohapat/demoimagevol   latest    44787e8aa6c0   5 days ago    77.8MB
demoimagevol            latest    44787e8aa6c0   5 days ago    77.8MB
ubuntu                  latest    58db3edaf2be   5 weeks ago   77.8MB

# docker rmi -f 44787e8aa6c0
Untagged: gmohapat/demoimagevol:latest
Untagged: gmohapat/demoimagevol@sha256:681c232cd9bdd7f14b5f468271fcdcb4d0acd5f7fa7bb4f6167113662e3350e3
Untagged: demoimagevol:latest
Deleted: sha256:44787e8aa6c0882df2780cbd691d0b85c01a5a0f750dca40256686f38e1d71d8
Deleted: sha256:54317c9383d4ebe8087d3d77f7103dc612ae37cc8581f4fe7ffdd88920a75f8c

# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    58db3edaf2be   5 weeks ago   77.8MB
#

-- Now pull the image from docker hub

# docker pull gmohapat/demoimagevol
Using default tag: latest
latest: Pulling from gmohapat/demoimagevol
10ac4908093d: Already exists
Digest: sha256:681c232cd9bdd7f14b5f468271fcdcb4d0acd5f7fa7bb4f6167113662e3350e3
Status: Downloaded newer image for gmohapat/demoimagevol:latest
docker.io/gmohapat/demoimagevol:latest
#

-- Now verify

# docker images
REPOSITORY              TAG       IMAGE ID       CREATED       SIZE
gmohapat/demoimagevol   latest    44787e8aa6c0   5 days ago    77.8MB
ubuntu                  latest    58db3edaf2be   5 weeks ago   77.8MB
#

Yes, we able to pull the image from our own docker hub account.

Test with your own risk:

-- start all docker containers at one go

# docker start $(docker ps -a)

-- stop all docker containers at one go

# docker stop $(docker ps -a)


Mar 3, 2023

Docker - working steps | Part-4 Port expose

What is Docker Expose Port?

The expose keyword in a Dockerfile tells Docker that a container listens for traffic on the specified port. So, for a container running a web server, you might add this to your Dockerfile.

Assume, your web application running on one container with specific port. But when any applucation accessed from outside world, first connection comes to host machine then it ll go the container. So you nned to expose a port to container to access your web application port.

# mkdir app_dir
# cd app_dir/
docker run -it --name webserver -p 8080:80 ubuntu /bin/bash
root@080e8df03b2c:/# 
app_dir]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                  PORTS                                   NAMES
080e8df03b2c   ubuntu         "/bin/bash"   55 seconds ago   Up 54 seconds           0.0.0.0:808o->80/tcp, :::8080->80/tcp   webserver


Note:
port mapping:
<custom-port(host-machne)> : <actual-port(container)>

Lunching one more container with detach mode:

# docker run -td --name webserver1 -p 8081:80 ubuntu
0aa9f6d250c4f224fbd60cc6bc33dd16a00b01c5b9360bbd257243ac96e57290

# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                  PORTS                                   NAMES
0aa9f6d250c4   ubuntu         "/bin/bash"   56 seconds ago   Up 55 seconds           0.0.0.0:8081->80/tcp, :::8081->80/tcp   webserver1
080e8df03b2c   ubuntu         "/bin/bash"   9 minutes ago    Up 9 minutes            0.0.0.0:8080->80/tcp, :::8080->80/tcp   webserver
here, host (0.0.0.0:8081) forwardig port to 80.


we can verify also using docker port command:

[root@ip-172-31-38-12 app_dir]# docker port webserver1
80/tcp -> 0.0.0.0:8081
80/tcp -> :::8081

Let us attach the container and create a web page and access it. install nginx to access webpage

root@0aa9f6d250c4:/# apt-get update -y
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
...
Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [22.4 kB]
Fetched 26.1 MB in 7s (3668 kB/s)
Reading package lists... Done

root@0aa9f6d250c4:/#

let us install web application. example - ngnix.

root@0aa9f6d250c4:/# apt-get install nginx -y

-- check the nginx version

root@0aa9f6d250c4:/# nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
root@0aa9f6d250c4:/#
root@0aa9f6d250c4:/# which nginx
/usr/sbin/nginx
root@0aa9f6d250c4:/# cd /var/www/html
root@0aa9f6d250c4:/var/www/html# pwd
/var/www/html
root@0aa9f6d250c4:/var/www/html# cat > index.html
Hello world, this is nginx web container running on ec2 instance

-- restart nginx
root@0aa9f6d250c4:/var/www/html# service nginx restart

Now, you can access the web page from nginx web server.

http://54.169.93.29:8081

Note: you need to allow 8081 port to ec2 machine security group.
after adding this port to security group, webpage is accessible.






It works!!!

similarly you can run many applications in different containers with different ports.

Docker - working steps | Part-3 Docker File System, Docker Volumes

 To Understand Docker Volume, you need to go though below sequence.
Docker File System
Bind Mounts
Docker Volumes

1) Docker File System
A docker container runs the software stack defined in an image. Images are made of a set of read-only layers that work on a file system called the Union File System. When we start a new container, Docker adds a read-write layer on the top of the image layers allowing the container to run as though on a standard Linux file system. So, any file change inside the container creates a working copy in the read-write layer. However, when the container is stopped or deleted, that read-write layer is lost.

2) Bind Mounts
A Docker bind mount is a high-performance connection from the container to a directory on the host machine. It allows the host to share its own file system with the container, which can be made read-only or read-write.
This allows us to use a container to run tools that we don't want to install on our host, and yet still work with our host's files. For example, if we wanted to use a custom version of bash for a particular script, we might execute that script in a bash container, mounted to our current working directory:

e.g.,

$ docker run -v $(pwd):/var/opt/project bash:latest \ 
  bash -c "echo Hello > /var/opt/project/file.txt"

The –v option can be used for all forms of mounting and specifies, in this instance, the source on the host – the working directory in the output of $(pwd)  – and the target mount point in the container – /var/opt/project.

After running this command, we will find file.txt in the working directory of our host machine. This is a simple way to provide persistent files between invocations of a Docker container, though it is most useful for when the container is doing work on behalf of the host.
One good use case for this would be executing various versions of a language's build tools in Docker to avoid having conflicting installations on a developer machine.

We should note that $(pwd -W) is sometimes needed on Windows bash shells to provide the working directory in a form that the bash shell can pass to Docker.

3) Docker Volumes
A bind mount uses the host file system, but Docker volumes are native to Docker. Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker. Volumes have several advantages over bind mounts:
  • Volumes are easier to back up or migrate than bind mounts.
  • You can manage volumes using Docker CLI commands or the Docker API.
  • Volumes work on both Linux and Windows containers.
  • Volumes can be more safely shared among multiple containers.
  • Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
New volumes can have their content pre-populated by a container.
Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.

In addition, volumes are often a better choice than persisting data in a container’s writable layer, because a volume does not increase the size of the containers using it, and the volume’s contents exist outside the lifecycle of a given container.

Let us do the following scenarios:
  • Creating Volumes
  • Listing Volumes
  • Inspecting Volumes
  • Removing Volumes
  • Pruning Volumes
  • Starting a Container with a Volume

Docker volumes nothing but directory in container and map with base machine. Like it is a network drive which mounted with your base machine.

e.g.,
Assume, you want to copy some files from base machine to containers and files from containers to base machine with regular interval/ daily basis, then docker volume can help you to fullfill this requirement. i.e. both way file movement. Even we can map container to container or container to base machine.

Note:
You need to declared the volume while lunchig the container.
for existing container, we can't declare a volume
Even if a container in stopped state, still we can access volumes

Mapping a volume in two ways:
1) Creating a volume between host to container
2) Creating a volume between container to container


[root@ip-172-31-38-12 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    58db3edaf2be   4 weeks ago   77.8MB
[root@ip-172-31-38-12 ~]#
[root@ip-172-31-38-12 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@ip-172-31-38-12 ~]#
[root@ip-172-31-38-12 docker_class]# ls
dockerfile

-- create Docker file like below 

[root@ip-172-31-38-12 docker_class]# cat Dockerfile
FROM ubuntu
MAINTAINER gmohapat
VOLUME /data

-- Create image from dockerfile

[root@ip-172-31-38-12 docker_class]# docker build -t demoimagevol .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM ubuntu
 ---> 58db3edaf2be
Step 2/3 : MAINTAINER gmohapat
 ---> Running in 7ad715c26e23
Removing intermediate container 7ad715c26e23
 ---> 54317c9383d4
Step 3/3 : VOLUME /data
 ---> Running in 123e7f1c699f
Removing intermediate container 123e7f1c699f
 ---> 44787e8aa6c0
Successfully built 44787e8aa6c0
Successfully tagged demoimagevol:latest
[root@ip-172-31-38-12 docker_class]#


[root@ip-172-31-38-12 docker_class]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
demoimagevol   latest    44787e8aa6c0   55 seconds ago   77.8MB
ubuntu         latest    58db3edaf2be   4 weeks ago      77.8MB

Now we are good to lunch a container

[root@ip-172-31-38-12 docker_class]# docker run -it --name container9 demoimagevol /bin/bash

root@5a158a8f3ce2:/# ls
bin  boot  data  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

root@5a158a8f3ce2:/#

Here you see, data volume is there. This 'data' directory is not normal directory. It is OS docker directory.
Let us go inside data volume and create some files and test below.

root@5a158a8f3ce2:/data# touch file1 file2 file3

root@5a158a8f3ce2:/data# ls
file1  file2  file3

Now, come-out from container with exit option.

root@5a158a8f3ce2:/data# exit
exit

[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
5a158a8f3ce2   demoimagevol   "/bin/bash"   4 minutes ago   Exited (0) 4 seconds ago             container9

[root@ip-172-31-38-12 docker_class]#


Now, Create a new contaier and map 'data' volume to new container

[root@ip-172-31-38-12 docker_class]# docker run -it --name container91 --privileged=true --volumes-from container9 ubuntu /bin/bash
root@204b1b533717:/# ls
bin  boot  data  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@204b1b533717:/# cd data/
root@204b1b533717:/data# ls
file1  file2  file3
root@204b1b533717:/data#

Here, you can see, data volume mapped from previous container and all there files are there in data volume.
Now, do manage some files and check that is reflected in container9.

[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
204b1b533717   ubuntu         "/bin/bash"   2 minutes ago    Exited (0) 14 seconds ago             container91
5a158a8f3ce2   demoimagevol   "/bin/bash"   15 minutes ago   Exited (0) 10 minutes ago             container9

[root@ip-172-31-38-12 docker_class]# docker start container9
container9
[root@ip-172-31-38-12 docker_class]# ls
Dockerfile
[root@ip-172-31-38-12 docker_class]# docker attach container9
root@5a158a8f3ce2:/# ls
bin  boot  data  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@5a158a8f3ce2:/# cd data/
root@5a158a8f3ce2:/data# ls
file1  file2
root@5a158a8f3ce2:/data#

Here you can see, in another container changes reflected.

Declare a volume using command line:

[root@ip-172-31-38-12 docker_class]# docker run -it --name container8 -v /backup ubuntu /bin/bash
root@d0912a3f8083:/# ls
backup  bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@d0912a3f8083:/#

Now map the volume to host machine.

[root@ip-172-31-38-12 docker_class]# docker run -it --name container7 -v /home/ec2-user/newdir:/datavol --privileged=true ubuntu /bin/bash

root@494ef9cc4365:/# ls
bin  boot  datavol  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@494ef9cc4365:/# 
root@494ef9cc4365:/# cd datavol/
root@494ef9cc4365:/datavol# touch samplefile1 samplefile2
root@494ef9cc4365:/datavol# ls
samplefile1  samplefile2
root@494ef9cc4365:/datavol# exit
exit

[root@ip-172-31-38-12 newdir]# cd /home/ec2-user/newdir
[root@ip-172-31-38-12 newdir]# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 26 19:41 samplefile1
-rw-r--r-- 1 root root 0 Feb 26 19:41 samplefile2
[root@ip-172-31-38-12 newdir]#

Yes, now we can see what files we created in container volume, we able to see in host machine file system.

In real time, can access log files, source files, artifacts, configuration files.

Feb 26, 2023

Docker - working steps | Part-2 Dockerfiles

Dockerfiles

Docker builds images automatically by reading the instructions from a Dockerfile -- a text file that contains all commands, in order, needed to build a given image. A Dockerfile adheres to a specific format and set of instructions.

-- Create docker file

[root@ip-172-31-38-12 ~]# cd docker_class/
[root@ip-172-31-38-12 docker_class]# touch dockerfile

[root@ip-172-31-38-12 docker_class]# vi dockerfile

FROM ubuntu
MAINTAINER gmohapat
RUN apt-get update
RUN echo "Hi docker" > /tmp/demofile

[root@ip-172-31-38-12 docker_class]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    58db3edaf2be   4 weeks ago   77.8MB

# docker build -t mynewimage .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM ubuntu
 ---> 58db3edaf2be
Step 2/4 : MAINTAINER gmohapat
 ---> Running in c70490836769
Removing intermediate container c70490836769
 ---> da0ef27edd59
Step 3/4 : RUN apt-get update
 ---> Running in 22211fb6e919
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]
...
Get:17 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [22.4 kB]
Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [49.0 kB]
Fetched 25.8 MB in 9s (2777 kB/s)
Reading package lists...
Removing intermediate container 22211fb6e919
 ---> 32e0c4cba503
Step 4/4 : RUN echo "Hi docker" > /tmp/demofile
 ---> Running in 40cd8fb3ace0
Removing intermediate container 40cd8fb3ace0
 ---> db016147e5c7
Successfully built db016147e5c7
Successfully tagged mynewimage:latest
[root@ip-172-31-38-12 docker_class]#

# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
mynewimage   latest    db016147e5c7   About a minute ago   119MB
ubuntu       latest    58db3edaf2be   4 weeks ago          77.8MB

Here, all steps of docker file executed when we build new image. you can see "mynewimage" created and alog with that as per docker file it created a file in /tmp location as well.
let us try to create a container and see this.

# docker run -it --name container51 mynewimage
root@59105008f353:/#
root@59105008f353:/# cd /tmp
root@59105008f353:/tmp# ls
demofile
root@59105008f353:/tmp# cat demofile
Hi docker

Perfect. Here in the container, the required file also created.
I real-time, we can do/ run post steps/ configuration steps as soon as container created.

Note: dockerfile is default. But any customized name can be used as per industry standard but call of that file is different.

example:

Assigment-3


1) create scenario

[root@ip-172-31-38-12 docker_class]# touch index.html
[root@ip-172-31-38-12 docker_class]# touch config
[root@ip-172-31-38-12 docker_class]#
[root@ip-172-31-38-12 docker_class]# tar -cvf config.tar config
config
[root@ip-172-31-38-12 docker_class]# gzip config.tar
[root@ip-172-31-38-12 docker_class]# ls
config  config.tar.gz  dockerfile  Dockerfile_dev  index.html
[root@ip-172-31-38-12 docker_class]#

2) now create docker file

[root@ip-172-31-38-12 docker_class]# touch Dockerfile_dev
[root@ip-172-31-38-12 docker_class]# vi Dockerfile_dev
FROM ubuntu
MAINTAINER gmohapat
# assume /tmp is our application file location
WORKDIR /tmp
RUN apt-get update
RUN echo "hi kudos, setting app" > /tmp/setupfile
#create environment variable
ENV NAME variable1
# copy a file
COPY index.html /tmp/html/
# add containt
ADD config.tar.gz /tmp/newdir/

3) Now build docker file

[root@ip-172-31-38-12 docker_class]# docker build -t testingimage1 -f Dockerfile_dev .
Sending build context to Docker daemon   5.12kB
Step 1/8 : FROM ubuntu
 ---> 58db3edaf2be
Step 2/8 : MAINTAINER gmohapat
 ---> Using cache
 ---> da0ef27edd59
Step 3/8 : WORKDIR /tmp
 ---> Running in 84ee2d0c7642
Removing intermediate container 84ee2d0c7642
 ---> 962604ce9ba8
Step 4/8 : RUN apt-get update
 ---> Running in 2a9d7e1a2ad2
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
...
Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [22.4 kB]
Fetched 25.8 MB in 7s (3620 kB/s)
Reading package lists...
Removing intermediate container 2a9d7e1a2ad2
 ---> ce9942e3c96f
Step 5/8 : RUN echo "hi kudos, setting app" > /tmp/setupfile
 ---> Running in 1c824429816f
Removing intermediate container 1c824429816f
 ---> 2313e4610ec0
Step 6/8 : ENV NAME variable1
 ---> Running in 01b199067fac
Removing intermediate container 01b199067fac
 ---> e3e9371cb4ed
Step 7/8 : COPY index.html /tmp/html/
 ---> 3b45e19ba67b
Step 8/8 : ADD config.tar.gz /tmp/newdir/
 ---> 50cf5debc384
Successfully built 50cf5debc384
Successfully tagged testingimage1:latest
[root@ip-172-31-38-12 docker_class]#

Here, docker file build successfully and all instructions passed successfully.

Now lunch a container and validate all.

[root@ip-172-31-38-12 docker_class]# docker run -it --name container61 testingimage1 /bin/bash

root@704302e1dac8:/tmp# ls
html  newdir  setupfile
root@704302e1dac8:/tmp# cd newdir/
root@704302e1dac8:/tmp/newdir# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 24 17:54 config
root@704302e1dac8:/tmp/newdir#

Perfect. As per the instruction image created as per dockerfile content.
The ADD command copy the config.tar.gz to /tmp/newdir wth extracted mode. This is the significance of ADD command over COPY command.

Feb 24, 2023

Docker - working steps | Part-1 Docker Setup & Basics

Note: I created amzone machine image in AWS to test all these commands.

-- Install Docker

# mkdir docker_class
# cd docker_class/
# yum install docker -y

-- check docker version

[root@ip-172-31-38-12 docker_class]# docker --version
Docker version 20.10.17, build 100c701

-- start docker service

[root@ip-172-31-38-12 docker_class]# service docker start
Redirecting to /bin/systemctl start docker.service

-- check docker status

# service docker status
# service docker status| grep active
Redirecting to /bin/systemctl status docker.service
   Active: active (running) since Thu 2023-02-23 14:45:32 UTC; 4min 4s ago

-- enable docker service to start auotmatically when rebooted by os level

[root@ip-172-31-38-12 docker_class]# chkconfig docker on
Note: Forwarding request to 'systemctl enable docker.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
-- Docker information
[root@ip-172-31-38-12 docker_class]# docker info
Client:
 Context:    default
 Debug Mode: false
Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.17
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc version: 5fd4c4d144137e991c4acebb2146ab1483a97925
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 5.10.165-143.735.amzn2.x86_64
 Operating System: Amazon Linux 2
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 964.8MiB
 Name: ip-172-31-38-12.ap-southeast-1.compute.internal
 ID: GGL3:DVIA:JY5H:DZ7G:EF4W:3ATA:NJDP:5R3Y:4QD6:TYVH:VGYX:3CHB
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
[root@ip-172-31-38-12 docker_class]#

Note:
All docker related file will be stored in below location as per above command.
Docker Root Dir: /var/lib/docker
[root@ip-172-31-38-12 docker_class]# ls /var/lib/docker
buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes


Docker Basic commands

-- Lunch a container with download

docker run -i -t --name <container-name> <image-name> /bin/bash

[root@ip-172-31-38-12 docker_class]# docker run -i -t --name container1 ubuntu /bin/bash

-i - interactive
-t - terminal
can be used as:

docker run -it --name container1 ubuntu /bin/bash

Note: This command ll go to docker hub(hub.docker.com) and it ll download ubuntu image first and then it lunch container

e.g.,
docker run -it --name container1 ubuntu /bin/bash
[root@ip-172-31-38-12 docker_class]# docker run -it --name container1 ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
677076032cca: Pull complete
Digest: sha256:9a0bdde4188b896a372804be2384015e90e3f84906b750c1a53539b585fbbe7f
Status: Downloaded newer image for ubuntu:latest
root@2d00a04c0b77:/#
Note: container created and we are in the container by default.


-- Verify the Operating system

root@2d00a04c0b77:/# cat /etc/os*

PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
root@2d00a04c0b77:/#

Note: VERSION="22.04.1 LTS (Jammy Jellyfish)"  as per above output and it is the stable version.

-- To comeout from container
way-2:
exit -- stop and come out
Ctrl + pq  -- come out from container without stopping container

-- Now check running containers

docker ps -a
[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
2d00a04c0b77   ubuntu    "/bin/bash"   24 minutes ago   Up 24 minutes             container1

Assignment-1
Create a container without passing any name and use 'exit' command to comeout from container. The list out all containers
[root@ip-172-31-38-12 docker_class]# docker run -it ubuntu /bin/bash
root@6d5b321d998e:/# exit
exit
[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                     PORTS     NAMES
6d5b321d998e   ubuntu    "/bin/bash"   15 seconds ago   Exited (0) 4 seconds ago             determined_einstein
2d00a04c0b77   ubuntu    "/bin/bash"   27 minutes ago   Up 27 minutes                        container1
[root@ip-172-31-38-12 docker_class]#

Note: Here docker given a random name to newly created container

-- search images available using docker console command

docker search <image-name>

 [root@ip-172-31-38-12 docker_class]# docker search tomcat

NAME                                  DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
tomcat                                Apache Tomcat is an open source implementati…   3491      [OK]
tomee                                 Apache TomEE is an all-Apache Java EE certif…   102       [OK]
bitnami/tomcat                        Bitnami Tomcat Docker Image                     48                   [OK]
arm32v7/tomcat                        Apache Tomcat is an open source implementati…   12
arm64v8/tomcat                        Apache Tomcat is an open source implementati…   8
rightctrl/tomcat                      CentOS , Oracle Java, tomcat application ssl…   7                    [OK]
...

Here you may have long list for 'tomcat' images. But it is advised to use OFFICIAL[OK] images.

-- Download the image only

docker pull <image-name>
[root@ip-172-31-38-12 docker_class]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
10ac4908093d: Pull complete
6df15e605e38: Pull complete
2db012dd504c: Pull complete
8fa912900627: Pull complete
f8fe20946c04: Pull complete
8093daf900d2: Pull complete
49c22a329043: Pull complete
Digest: sha256:9e2525bd79c5386c9bd9ba56fe450263d7af605e41db9fead44e1969379b588a
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@ip-172-31-38-12 docker_class]#

-- List all images

docker images

[root@ip-172-31-38-12 docker_class]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
tomcat       latest    2362f0cdbf14   3 weeks ago   474MB
ubuntu       latest    58db3edaf2be   4 weeks ago   77.8MB
Note: We can manage our images either with ID or image name.
-- start container 
docker start <container-id | container-name>
[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
6d5b321d998e   ubuntu    "/bin/bash"   17 minutes ago   Exited (0) 17 minutes ago             determined_einstein
2d00a04c0b77   ubuntu    "/bin/bash"   44 minutes ago   Up 44 minutes                         container1
[root@ip-172-31-38-12 docker_class]#
[root@ip-172-31-38-12 docker_class]# docker start 6d5b321d998e
6d5b321d998e
[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
6d5b321d998e   ubuntu    "/bin/bash"   17 minutes ago   Up 5 seconds              determined_einstein
2d00a04c0b77   ubuntu    "/bin/bash"   45 minutes ago   Up 45 minutes             container1
[root@ip-172-31-38-12 docker_class]#

-- Go to container/ attach container

docker attach <container-id | container-name>

[root@ip-172-31-38-12 docker_class]# docker attach 6d5b321d998e
root@6d5b321d998e:/#

-- stop a container

docker stop <container-1 [,container-2, ... >


[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
6d5b321d998e   ubuntu    "/bin/bash"   27 minutes ago   Up 9 minutes              determined_einstein
2d00a04c0b77   ubuntu    "/bin/bash"   54 minutes ago   Up 54 minutes             container1
[root@ip-172-31-38-12 docker_class]#
[root@ip-172-31-38-12 docker_class]# docker stop determined_einstein
determined_einstein
[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                       PORTS     NAMES
6d5b321d998e   ubuntu    "/bin/bash"   27 minutes ago   Exited (137) 3 seconds ago             determined_einstein
2d00a04c0b77   ubuntu    "/bin/bash"   55 minutes ago   Up 55 minutes                          container1
[root@ip-172-31-38-12 docker_class]#


-- delete a container 

docker rm <container-name>

delete a container force fully ( delete even if running)
docker rm -f <container-name>
[root@ip-172-31-38-12 docker_class]# docker rm determined_einstein
determined_einstein
[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS                       PORTS     NAMES
2d00a04c0b77   ubuntu    "/bin/bash"   About an hour ago   Exited (137) 3 minutes ago             container1
[root@ip-172-31-38-12 docker_class]#
[root@ip-172-31-38-12 docker_class]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
tomcat       latest    2362f0cdbf14   3 weeks ago   474MB
ubuntu       latest    58db3edaf2be   4 weeks ago   77.8MB
Note: Here containers ll be deleted but not the images. If you want to delete the images, you can delete them separatly 

-- delete docker image

docker rmi -f <image-name>

[root@ip-172-31-38-12 docker_class]# docker rmi -f tomcat
Untagged: tomcat:latest
Untagged: tomcat@sha256:9e2525bd79c5386c9bd9ba56fe450263d7af605e41db9fead44e1969379b588a
Deleted: sha256:2362f0cdbf14867a1c1ef2e1cc8dc4e78c4f8b66351acdbe71c8ab938e54f364
Deleted: sha256:301da3f7c3d21a8f2b8277a503094c073f8590d50aa14d3ff61474a408b24173
Deleted: sha256:8a77a541c44a41c746957cffbfae4371460b1f3fb830e1efdafc54851410c95f
Deleted: sha256:193b5f6e1d63da1e1e77210f6eaf54ad85e74da97924e0c9a778972bdc04337b
Deleted: sha256:946053b8886e3d663150c166cac63e94978ea8c29f087da3154d64f5a66a56ce
Deleted: sha256:2a6cefd88a47e238c7f3353ceccb865862bf9a8e490320cc781daa7085d041ca
Deleted: sha256:f773875b27ea4af5d222542ae85c25c34b50ae09a6b620ccf7c5198ed1a3aca8
[root@ip-172-31-38-12 docker_class]#

-- Creating an image from an existing container

(Just like take backup of an existing container)

[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS         PORTS     NAMES
2d00a04c0b77   ubuntu    "/bin/bash"   About an hour ago   Up 2 seconds             container1
[root@ip-172-31-38-12 docker_class]#
[root@ip-172-31-38-12 docker_class]# docker attach container1
root@2d00a04c0b77:/#
root@2d00a04c0b77:/# apt-get update -y

Here, we are inside the container and we updated the image first before creating the image
before that we can do some chages in container. Then we can take backup and later when we restore, then we can verify whether they are avialble or not

-- To take image backup

docker commit <source-container-name>

-- add tag to image

docker tag <image-id> <tag-name>

Assignment#2

Take a image of an running container and create one more container and verify all changes are in it or not.


root@2d00a04c0b77:/# cd /tmp
root@2d00a04c0b77:/tmp# mkdir workfiles
root@2d00a04c0b77:/tmp# cd workfiles/
root@2d00a04c0b77:/tmp/workfiles# touch myfile1 myfile2 myfile3
root@2d00a04c0b77:/tmp/workfiles# read escape sequence
[root@ip-172-31-38-12 docker_class]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS         PORTS     NAMES
2d00a04c0b77   ubuntu    "/bin/bash"   About an hour ago   Up 7 minutes             container1
[root@ip-172-31-38-12 docker_class]#
[root@ip-172-31-38-12 docker_class]# docker commit container1
sha256:a5203026bd0ca03bc93715ea66e657c4b5c76c71aef6aa11a4a3e4f8ff57c2e8
[root@ip-172-31-38-12 docker_class]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
<none>       <none>    a5203026bd0c   7 seconds ago   119MB
ubuntu       latest    58db3edaf2be   4 weeks ago     77.8MB
[root@ip-172-31-38-12 docker_class]# docker tag a5203026bd0c sampleimage
[root@ip-172-31-38-12 docker_class]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED              SIZE
sampleimage   latest    a5203026bd0c   About a minute ago   119MB
ubuntu        latest    58db3edaf2be   4 weeks ago          77.8MB
[root@ip-172-31-38-12 docker_class]# docker run -it --name container22 sampleimage /bin/bash
root@120fee2c9008:/# cd /tmp
root@120fee2c9008:/tmp# ls -l
total 0
drwxr-xr-x 2 root root 51 Feb 23 18:09 workfiles
root@120fee2c9008:/tmp# cd workfiles/
root@120fee2c9008:/tmp/workfiles# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 23 18:09 myfile1
-rw-r--r-- 1 root root 0 Feb 23 18:09 myfile2
-rw-r--r-- 1 root root 0 Feb 23 18:09 myfile3
root@120fee2c9008:/tmp/workfiles#

Here we saw, all changes are also available in new container what we created from image backup.


Translate >>