Docker Commands

In Docker basic command section we deal with 3 components, and they are:

1. Docker Daemon
2. Docker Client
3. Docker Hub




Now, when we work with the docker client, commands are being sent to the Docker Daemon, which then interprets the command and executes it for you.

Docker Daemon - The background service running on the host that manages the building, running and distributing Docker containers. 

Docker Client - The command line tool that allows the user to interact with the daemon.

Docker Hub - A registry of Docker images.


Docker Client Help

To get help at any point in time try out the following command:

$docker help



This will give you a full listing of commands that the docker client can understand. Take some time to go through this. Most of the commands are self-explanatory and are typical ones that you will use while dealing with containers.

Initial series of commands:

To know about the version of docker installed on your machine, type:
$docker version
By default, this will render all version information in an easy to read layout.

Default Output

$ docker version

Client:
Version:      1.8.0
API version:  1.20
Go version:   go1.4.2
Git commit:   f5bae0a
Built:        Tue Jun 23 17:56:00 UTC 2015
OS/Arch:      linux/amd64

Server:
Version:      1.8.0
API version:  1.20
Go version:   go1.4.2
Git commit:   f5bae0a
Built:        Tue Jun 23 17:56:00 UTC 2015
OS/Arch:      linux/amd64

To know about the system-wide information regarding the Docker installation. Information displayed includes the kernel version, number of containers and images. The number of images shown is the number of unique images. The same image tagged under different names is counted only once.

$docker info

The example below shows the output for a daemon running on Red Hat Enterprise Linux, using the devicemapper storage driver. As can be seen in the output, additional information about the devicemapper storage driver is shown:

$ docker info

Containers: 14
 Running: 3
 Paused: 1
 Stopped: 10
Images: 52
Server Version: 1.10.3
Storage Driver: devicemapper
 Pool Name: docker-202:2-25583803-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 10.74 GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 1.68 GB
 Data Space Total: 107.4 GB
 Data Space Available: 7.548 GB
 Metadata Space Used: 2.322 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.145 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Deferred Deletion Enabled: false
 Deferred Deleted Device Count: 0
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2015-12-01)
Execution Driver: native-0.2
Logging Driver: json-file
Plugins:
 Volume: local
 Network: null host bridge
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: Red Hat Enterprise Linux Server 7.2 (Maipo)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 991.7 MiB
Name: ip-172-30-0-91.ec2.internal
ID: I54V:OLXT:HVMM:TPKO:JPHQ:CQCD:JNLC:O3BZ:4ZVJ:43XJ:PFHZ:6N2S
Docker Root Dir: /var/lib/docker
Debug mode (client): false
Debug mode (server): false
Username: gordontheturtle
Registry: https://index.docker.io/v1/
Insecure registries:
 myinsecurehost:5000
 127.0.0.0/8
If I want to know, how many containers are available:

$docker search busybox

This command will search the online Docker registry for an Image named busybox. 

$ docker search busybox

NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
busybox                          Busybox base image.                             316       [OK]       
progrium/busybox                                                                 50                   [OK]
radial/busyboxplus               Full-chain, Internet enabled, busybox made...   8                    [OK]
odise/busybox-python                                                             2                    [OK]
azukiapp/busybox                 This image is meant to be used as the base...   2                    [OK]
ofayau/busybox-jvm               Prepare busybox to install a 32 bits JVM.       1                    [OK]
shingonoide/archlinux-busybox    Arch Linux, a lightweight and flexible Lin...   1                    [OK]
odise/busybox-curl                                                               1                    [OK]
ofayau/busybox-libc32            Busybox with 32 bits (and 64 bits) libs         1                    [OK]
peelsky/zulu-openjdk-busybox                                                     1                    [OK]
skomma/busybox-data              Docker image suitable for data volume cont...   1                    [OK]
elektritter/busybox-teamspeak    Lightweight teamspeak3 container based on...    1                    [OK]
socketplane/busybox                                                              1                    [OK]
oveits/docker-nginx-busybox      This is a tiny NginX docker image based on...   0                    [OK]
ggtools/busybox-ubuntu           Busybox ubuntu version with extra goodies       0                    [OK]
nikfoundas/busybox-confd         Minimal busybox based distribution of confd     0                    [OK]
openshift/busybox-http-app                                                       0                    [OK]
jllopis/busybox                                                                  0                    [OK]
swyckoff/busybox                                                                 0                    [OK]
powellquiring/busybox                                                            0                    [OK]
williamyeh/busybox-sh            Docker image for BusyBox's sh                   0                    [OK]
simplexsys/busybox-cli-powered   Docker busybox images, with a few often us...   0                    [OK]
fhisamoto/busybox-java           Busybox java                                    0                    [OK]
scottabernethy/busybox                                                           0                    [OK]
marclop/busybox-solr


Let us understand the output here, by paying attention to the columns:

  1. The first column is NAME and it gives you the name of the Docker image.
  2. The second column is DESCRIPTION and it is obvious what that means.
  3. The next column is STARS and if you noticed the list of images that matched the search term Docker has been listed in the descending order of the number of people who have starred the project. This is a very useful indicator of the popularity/correctness of the Image.


So, let’s say that we are fine with the busybox image name and now want to create an instance (Container) of this image. To do that, all we need to do is use the docker run command as given below:

$docker run busybox

The run command does something interesting and this will help you understand the Docker architecture, which we have seen earlier. The run command does the following:

  1. It checks if you already have a busybox image in your local repository.
  2. If it does not find that (which will be the case the first time), it will pull the image from the Docker Hub. Pulling the image is similar to downloading it and it could take a while to do that depending on your internet connection.
  3. Once it is pulled successfully, it is present in your local repository and hence it is then able to create a container based on this image.
Note: If the image was present locally, it would have directly run the container for you.


So, here we are in the busybox container and tried some of the Linux commands

Note from the prompt that you are now inside the container. You can exit the container by simply giving the exit command.

When you give the exit command, the container has stopped running. To verify that, you can give another command as given below:

Docker Containers

When you executed the docker ps command, you noticed that no container was running. This was because you exited out of the container. 
$docker ps


This will let you know that no container is running.
If we want to know about all the container state other then running, then:

$docker ps -all


Notice the following columns:
  1. CONTAINER_ID: A Unique ID for the container that was launched.
  2. IMAGE: This was the IMAGE that you launched i.e. busybox
  3. COMMAND: Important stuff here. This was the default command that was executed when the container was launched. If you recollect, when the container based on busybox image was launched, it led you to the Unix Prompt i.e. the Shell was launched. And that is exactly what the program in /bin/sh does.

To Relaunch a container

$docker start my_container

Docker Images

Get List of Docker Images
The default docker images will show all top level images, their repository and tags, and their size.

$docker images

$ docker images

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
postgres                  9                   746b819f315e        4 days ago          213.4 MB
postgres                  9.3                 746b819f315e        4 days ago          213.4 MB
postgres                  9.3.5               746b819f315e        4 days ago          213.4 MB
postgres                  latest              746b819f315e        4 days ago          213.4 MB
Note the columns that the output gives (2 important ones are given below):
  1. REPOSITORY
  2. TAG
The REPOSITORY column is obvious since it is the name of the Image itself. The TAG is important, you will find that the TAG value is mentioned as the latest. But there was no indication given to us about that.

Attach to a running Container

Now, it's time for something interesting to help us understand some more commands. We will continue with our example around busybox Image.

First up, we will relaunch our container without the -i (interactive) mode.
Give the following command:

$docker start busybox

Output : It will return you the container_id of busybox.

Use docker attach to attach to a running container using the container’s ID or name, either to view its ongoing output or to control it interactively. You can attach to the same contained process multiple times simultaneously, screen sharing style, or quickly view the progress of your detached process.

$docker attach busybox

And you are in the busybox terminal.Enjoy!!

Hi Guys, apologies for skipping the installation part but will share soon with you. Share your feedback about the blog post and its effectiveness.

Image source :  https://docs.docker.com/


Comments

Post a Comment

Popular posts from this blog

Docker Commands Continue...