Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 B

In the previous tutorial Part 3 A, we learned 11 Command to get familiar with Docker terms and their terminology. Here we will learn 12 more commands used in Docker and its implementation

Just in case, if  you did miss Previous two tutorial regarding Docker can be read here:

Part 1: What Is Docker And Concept Of Containers With Virtualization?
Part 2: How To Install Docker On CentOS/RHEL 6/7? And Learn Docker HUB Registration
Part 3A: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 A
Part 4: How To Perform Task Like Install, Run And Delete Applications Inside Docker Containers

Let’s Start

Command 13: Stop A Docker

Now if our container is working/running and we got its ID by issuing the docker ‘ps’ command previously. Now to stop that container issue following command

Syntex: docker stop container ID or auto-generated name.

[root@localhost ~]# docker stop hungry_mayer



Command 14: Add Name To Docker Container On Local

consider a situation when you have good numbers of containers and you can’t remember so you don’t have to remember the container ID. You can give a unique name for every container using the –name option as the following example shows:

Syntex: docker run –name unique_name Image Command

[root@localhost ~]# docker run --name unique_name centos cat /etc/redhat-release

Command 15: Use of Name To (start, stop, remove, top, stats)

Now as we have given a unique name to the container so we can do a lot of work with container unique name like the following.

[root@localhost ~]# docker start unique_name 
[root@localhost ~]# docker stats unique_name 
[root@localhost ~]# docker top unique_name

Note: Sometimes there is no output because the command was in execution which was used to create the container.

Command 16: Start Docker Container Interactive Shell

To start an interactive session into a container shell, and want to run commands as you do on any other Linux session, please run the following command

[root@localhost ~]# docker run -it centos /bin/sh
sh-4.2# bash
[root@3e278670bbc1 /]#

Note: Run bash command to get a bash shell

Here, Centos is the name of the image we download from Docker Hub
-it is used to mention that we want to run in interactive mode.
/bin/bash is used to run the bash shell once CentOS is up and running.

Command 17: Quit Docker Container Interactive Shell

Now if you want to run quit this interactive session from the container with the host. You can do this by typing exit in an interactive shell. The exit command terminates all the container processes and stops the container.

[root@3e278670bbc1 /]# exit
exit
sh-4.2# exit
exit
[root@localhost ~]#

Command 18: Keep Docker Shell Session Active

As the previous command kill all the running process of a container after exiting interactive shell mode. Now if you don’t want to kill the running container process and want to keep the container in the running state. Also, want to exit the interactive shell session and return to the Host Terminal, you can do this by pressing Ctrl+p and Ctrl+q keys.

[root@d15866d0dc29 /]#   "<Press Ctrl+p> and then <Press  Ctrl+q>"
[root@localhost ~]#

Command 19: To Reconnect With Running Container

Well with the previous command we kept a docker running even after exiting the interactive shell now to reconnect that interactive shell we need the container ID or name of that container. Use the ‘docker ps’ command to get the ConatinerID or name and use the following command to get reconnected.

[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d15866d0dc29 centos "/bin/sh" 10 minutes ago Up 10 minutes naughty_montalcini

Now Use this ContainerID or Name to Reconnect Container.

Syntex: docker attach <container id>

[root@localhost ~]# docker attach d15866d0dc29
[root@d15866d0dc29 /]#



Command 20: To Stop A Running Container

To stop and kill a docker from the host, use the following command
Syntax: docker kill <container id>

[root@localhost home]# docker kill 9ac0ec22a0a2
9ac0ec22a0a2

Command 21: Inspect A Docker

This command is used to see the details of an image or container. This Command provides lots of information about the Docker Image.
Syntax: docker inspect Repository

[root@localhost ~]# docker inspect centos
[
 {
 "Id": "sha256:3bee3060bfc81c061ce7069df35ce090593bda584d4ef464bc0f38086c11371d",
 "RepoTags": [
 "docker.io/centos:latest"
 ],
 "RepoDigests": [

…………………


……………………

 },
 "RootFS": {
 "Type": "layers",
 "Layers": [
 "sha256:dc1e2dcdc7b6ff86d785fa16cf97464d263d04346a191c57b5ca8a66b4155861"
 ]
 }
 }
]

Repository − This is the name of the Image.

Command 22: Docker Logs

When there is a production, we always need logs for data analysis and get logs of the container from the host machine we need to run the following command but to do this we require a ContainerID

Syntex: docker logs ContainerID

[root@localhost ~]# docker logs 2b8b51d81869

Command 23: Get Image ID’s Only

If you require image ID only for any purpose you may issue the following command to get IDs

[root@localhost home]# docker images -q
1815c82652c0
3bee3060bfc8

As you can see this command is used to return only the Image IDs of the images.

q− It tells the Docker command to return the Image ID’s only

Command 24: Docker History

docker history is the command to check all the commands which were fired/ran with an image via a container.

Syntax: docker history ImageID

This command will show all the commands that were run with the CentOS image.

[root@localhost home]# docker history centos
IMAGE CREATED CREATED BY SIZE COMMENT
3bee3060bfc8 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B 
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL name=CentOS Base Ima 0 B 
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:d22a9c627d1d1f32a8 192.5 MB

ImageID − This is the Image ID for which you want to see all the commands that were run against it.



Read More:

Part 1: What Is Docker And Concept Of Containers With Virtualization?
Part 2: How To Install Docker On CentOS/RHEL 6/7? And Learn Docker HUB Registration
Part 3A: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 A
Part 3B: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 B
Part 4: How To Perform Task Like Install, Run And Delete Applications Inside Docker Containers