Docker Container: What is Docker ? How to Create it ? Basic Docker Commands?

What is Docker? Create Docker Container and Learn Basic Commands

What is Docker

What is Docker ?

Docker is one of the best container's platform providers in market these day.

What is a Container ?

In a development or testing environment where a team wants to test or use different operating systems or different configuration virtualization was the best answer, containers now provide a fast and a lightweight solution, imagine the need of testing your application in different environments while using maybe different middleware technologies or configuration, with containers you can start the required system fast and easy and yet stop it when you don't need it, you can also automate the container creation with predefined configuration.

Some people they ask why to use a container and not cloud image or a virtual snapshot.

Docker VS AWS EC2 Image VS Virtualization
  • Container is much faster to start and stop.
  • There is no need to pay extra cost for keeping the snapshots or images.
  • If you have hundreds of images they will be hard to manage compared to containers.
  • Whenever you make changes to the environment you can track it with Docker while you cannot with snapshots.
  • And more...

Install Docker

yum install -y docker

If you get below error:

No package docker available.

Try one of below methods:

#1

yum install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-extras

Or

#2 Find Docker epel repo and download it.

Start the Docker service

RHEL 7 / CentOS 7

systemctl start docker

RHEL/CentOS 5 and RHEL/CentOS 6

sudo service docker start

Add your username to the docker group, if you get an error claiming that group is not there then
create the group manually 'groupadd docker'

usermod -a -G docker username

You can use below docker commands to confirm installation and other info

service docker status "To confirm that it's running"
docker --version
docker info

Basic Docker Commands

#1 List available Docker images

docker images

This should return no images as you did not yet install any

Output:

REPOSITORY TAG IMAGE ID CREATED SIZE

#2 Search for a specific image inside docker index

docker search ubuntu

--filter "is-official=true" -- To show only official images.

docker search centos --filter "is-official=true"

#3 Download image by using 'docker pull' and add required version

docker pull centos:6

Confirm docker image download

docker images

Output:

REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos 6 30365b2e827c 2 weeks ago 194.7 MB

#4 Create docker container

Replace the bold/colored with the required image, in below example i am creating the CentOS 6.9 image

docker images

 

REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 14.04 302fa07d8117 2 weeks ago 188 MB
docker.io/centos 6 30365b2e827c 2 weeks ago 194.7 MB
docker run -i -t 30365b2e827c /bin/bash

Exit the docker container by typing 'exit'
#5 Run 'docker ps -a' to know the available containers
#6 Start your docker container/instance "replace bold/colored" to the container you want to start by
checking the container id 'docker ps -a'

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd20a433a852 30365b2e827c "/bin/bash" 2 minutes ago Exited (0) 10 seconds ago fervent_kirch

 

docker start fd20a433a852

 

#7 Connect to the docker container

First you have to start the container

docker attach fd20a433a852

Now you are using the CentOS 6.9 system, you can do any modification or update and either ignore it
or commit the changes to the image.
Ex.:

yum install httpd
After installing the httpd and having the configuration changes, you can commit and save all changes
to the image and have it as a new image to be used.

exit "Exit the docker container"

#8 Commit Docker Changes

docker commit fd20a433a852

You can also create that image under new name like "dotsway"

docker commit fd20a433a852 dotsway

By checking docker images you will find your new updated CentOS with the HTTPD installation
under dotsway name.

docker images

Output:
[root@ip-172-31-38-20 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dotsway latest 636507873058 3 seconds ago 384.7 MB
docker.io/ubuntu 14.04 302fa07d8117 2 weeks ago 188 MB
docker.io/centos 6 30365b2e827c 2 weeks ago 194.7 MB

#9 Remove/Delete an Image

First you have to be sure that the image has no dependent child or being used by stopped container.

docker rmi 30365b2e827c

#10 Remove a Docker Container

docker ps -a "to list all containers"

Output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64003d7540d1 e12d2e4ec2d7 "/bin/sh -c 'yum upda" 8 hours ago Exited (1) 8 hours ago stoic_swanson

docker rm 64003d7540d1

#11 Stop and remove all Docker's containers

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

 

Don't forget to Share and Like our page.

 


Subscribe to
for video tutorials updates