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
# 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:
-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
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
-- 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
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.
No comments:
Post a Comment