How to Create a Container From Our Own Image (Method-2)
In this article,we will see Create a Container From Our Own Image.
What Are Containers?
Container is considered a new method that can replace virtualization.
An operating system can operate many applications from the cloud. Instead of virtualizing the computer as with VM, containers virtualize the OS.
Containers are placed on a physical server and host OS.
Shared components are read-only.
Every container shares kernel, binaries, and libraries with their host OS.
These shared components are available only as read-only, so there is no need to reproduce operating system codes.
The server will be able to run a lot of workloads in just one OS installation.
Steps to Follow
- Create a container “kt-container”.
- Create a new file “ktexpertsfile” inside the container.
- Verify Changes (what you have done).
- Create a image from the above container “kt-container”.
- Create container “kt1-container” from image “kt1-image”.
1. Create a container “kt-container”
1 2 3 4 5 6 7 |
[root@ip-172-31-4-99 ec2-user]# docker run --name kt-container -it centos //bin/bash Unable to find image 'centos:latest' locally latest: Pulling from library/centos 729ec3a6ada3: Pull complete Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9 Status: Downloaded newer image for centos:latest [root@afeb1d941942 /]# |
To see the OS related files of the container
1 2 |
[root@afeb1d941942 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var |
To see the OS information of the container
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@afeb1d941942 /]# cat /etc/os-release NAME="CentOS Linux" VERSION="8 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="8" PLATFORM_ID="platform:el8" PRETTY_NAME="CentOS Linux 8 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:8" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-8" CENTOS_MANTISBT_PROJECT_VERSION="8" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="8" |
2. Create a new file “ktexpertsfile” inside the container
1 2 3 4 5 6 7 |
root@daebcb4fae46:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@daebcb4fae46:/# cd tmp root@daebcb4fae46:/tmp# ls root@daebcb4fae46:/tmp# touch ktexpertsfile root@daebcb4fae46:/tmp# ls ktexpertsfile |
Exit from the container
1 2 |
root@daebcb4fae46:/tmp# exit exit |
3. Verify Changes (what you have done)
Use below command to show the things what changes you have done in the contianer
C — Change
A — Add
1 2 3 4 5 |
[root@ip-172-31-4-99 ec2-user]# docker diff kt-container C /root A /root/.bash_history C /tmp A /tmp/ktexpertsfile |
4. Create a image from the above container “kt-container”
we can create image in 2 ways
Create from image container and give name to created image.
Give name while creating the image
Create image container and give name to created image
Create image from container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker commit kt-container sha256:5d721a8b702e1b3fb6b21db75e3d34989b4df5f0bc6ecdf1287f30264f46ff8a |
To see the list of images
1 2 3 4 |
[root@ip-172-31-4-99 ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 5d721a8b702e 5 seconds ago 64.2MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB |
Give name to the above image
1 |
[root@ip-172-31-4-99 ec2-user]# docker tag 5d721a8b702e kt1-image |
Verify the image
To see the list of images
1 2 3 4 |
[root@ip-172-31-4-99 ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE kt1-image latest 5d721a8b702e 2 minutes ago 64.2MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB |
Give name while creating the image
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker commit kt-container kt2-image sha256:a3186365413f08c1f5090d205b4883ee684ae108bf7986efa910a2d4273ca49d |
Verify the image
To see the list of images
1 2 3 4 5 |
[root@ip-172-31-4-99 ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE kt2-image latest a3186365413f 5 seconds ago 64.2MB kt1-image latest 5d721a8b702e 5 minutes ago 64.2MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB |
5. Create container “kt1-container” from image “kt1-image”
To see all the containers
1 2 3 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES daebcb4fae46 ubuntu "//bin/bash" 20 minutes ago Exited (0) 17 minutes ago kt-container |
Run below command to create a container
1 2 3 |
[root@ip-172-31-4-99 ec2-user]# docker run --name kt1-container -it kt1-image //bin/bash [root@d9c5a84b639b /]# |
Verify file “ktexpertsfile ” inside the container
1 2 3 4 |
[root@d9c5a84b639b /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@d9c5a84b639b /]# ls /tmp ks-script-0n44nrd1 ks-script-w6m6m_20 ktexpertsfile |
Exit from the container
1 2 |
[root@d9c5a84b639b /]# exit exit |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform