How to Create a Container From Docker Image (Method-1)

Share via:

How to Create a Container From Docker Image (Method-1)

Dear Readers,

In this article,we will see Create a Container From Docker Image (Docker Hub)

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.

Generally we create Docker Containers from Docker images.

Ways to create Docker Images

We can create Docker Images in 3 ways
1.Take image from Docker Hub.
2.Create image from existing docker containers.
3.Create image from Docker file.

Steps to Follow

  1. Take Image from Docker Hub.
  2. create a container from image (take from docker hub).
  3. Search images in in online docker registry (Docker Hub).
  4. Download images from online docker registry (Docker Hub).
  5. Create container from image “chef/chefdk”.
  6. Create container “ktexperts-container” (give container name while creating a container).
  7. Rename the containers.
  8. Go inside the exisitng container “ktexperts-container”.
  9. Delete Containers.
  10. Delete Images.

Create a Container from Docker Image
1. Take Image from Docker Hub

For creating a container,we need image
Take image “ubuntu” from Docker Hub
Docker Hub
it is a docker online registry.
Here we can see list of docker images.
Go to Docker Hub official website by using below link.
https://hub.docker.com/
we can see the mainpage of the Docker Hub.

Search the image “ubuntu”
we can see ubutntu image by searching.
Note
we can get any images by searching whatever you want (if Available in Docker Hub).
Take name of image “ubuntu”

2. create a container from image (take from docker hub)
docker — The docker command will be started with docker.
Run — To crete a container.
i — Interactive mode.
t — Terminal
/bin/bash —  shell
First Slash (/) — Top level root directory
Second Slash (/) — Seperation
Why give extra Slash?
Extra /(slash)
Some operating systems will be negated the first (/) slash.
Even Though it ignores first slash we have another slash that works as a root directory.
Event though it removes first slash it will be work but It will be given for saferside.
we can see unable to find image “ubuntu” locally,it is pulling all layers of image from Docker Hub Registry.

Note
When you run above command dooker will search for the image in your base machine,If image is there it will take copy of the image and create the container.
if image is not there docker will pull from docker hub and take copy of the image then create the container.
Docker will pull the image  docker hub to base machine from there it will copy of the image then it will create the container.
When you run the command container will be created and by default you will be inside the container.(Interactive mode)
bash is a default shell in linux under root bin directory.
Every terminal has one common shell.
To run any command inside the container we need at least one shell and one terminal.

To see the list of files or directories regarding to container

To see the hostname of the container

To see the OS information of the container

Exit from the container

If you run exit command you will go back to”/home/ex2-user”

To see all images inside your machine

To see only running containers

To see all the containers inside your machine

3. Search images in in online docker registry (Docker Hub)

Search jenkins

Search chef

4. Download images from online docker registry (Docker Hub)

Download chef/chefdk

Verify Images

To see all images inside your machine

5. Create container from image “chef/chefdk”

To see all OS related files of the container

To see the OS information of the container

Exit from the container

To see the running containers

Verify Containers

To see all the containers inside your machine

Note
If you don’t give any name to the container while creating the container in the docker run command,the docker will be created some funny random names for the container.
6. Create container “ktexperts-container” (give container name while creating a container)
We need to give extra flag for the container name (–name ktexperts-container).

To see OS related files of the container

To see the OS information of the container

Exit from the container

If you exit from the exit from the container,the container will be stopped automatically.

Verify Containers

To see all the containers inside your machine

We can see the container name which has taken whatever we have given.

To see the running containers

To see all the images inside your machine

7. Rename the containers

To see all the containers

Rename the container “competent_wright” to “newname “ktexperts1-container”

Rename the container “pedantic_jennings” to “newname “ktexperts2-container”

Verify the containers

To see all the containers

8. Go inside the existing container “ktexperts-container”

We need to do 2 things for going to inside the container
Start container
Attach container
Start Container

To see the running containers
Now ,the container”ktexperts-container” is running.

Attach Container

Exit from the container 

9. Delete Containers
To see all the containers

Delete the container “ktexperts-container”

Verify containers
To see all the containers

Note
If it is a running container we can’t delete directly by using above command.
we need to stop and delete the container.
For testing purpose i will start the container and stop then delete the container.

Start the containers

To see the running containers

Trying to use normal delete command

Note
We can see the error ” You cannot remove a running container 4b3f72c3038d5117ff22b80ba967828f42c909e084b9137b69479268a97b9938. Stop the container before attempting removal or force remove”
we need to stop the container then it will allow to delete the container.

Stop the containers

To see all the running containers

Delete multiple containers

10. Delete Images
Delete image “ubuntu”

using below command to delete image “ubuntu”

Verify images

Delete multiple images at a time

Verify Images

To see all the running containers

To see all the containers inside your machine

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

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment