Working with Docker Containers – Command Line Interface

ADVERTISEMENT

Working with Docker Containers - Command Line Interface
Working with Docker Containers – Command Line Interface

In this post, we will be working with Docker container CLI, to interact with Docker containers. This guide focuses on important commands that we use generally in Docker environment.

Before going ahead, take a look at the installation of docker on famous Linux distributions.

ADVERTISEMENT
Article will continue after the ad

1. Installing Docker on CentOS 7 / RHEL 7 / Fedora 24

2. Installing Docker on Ubuntu 16.04

Docker information:

Let’s check Docker version installed on the machine.

# docker -v

Docker version 1.5.0, build a8a31ef/1.5.0

You can also use version command to get to know more about components and versions in use.

# docker version

Client version: 1.5.0
Client API version: 1.17
Go version (client): go1.3.3
Git commit (client): a8a31ef/1.5.0
OS/Arch (client): linux/amd64
Server version: 1.5.0
Server API version: 1.17
Go version (server): go1.3.3
Git commit (server): a8a31ef/1.5.0

The info command prints the location of different components, shows how many containers and images are there, as well as it gives the information about the operating system, kernel version, CPU, Memory, and hostname.

# docker info

Containers: 0
Images: 4
Storage Driver: devicemapper
Pool Name: docker-253:1-369-pool
Pool Blocksize: 65.54 kB
Backing Filesystem: extfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 564.9 MB
Data Space Total: 107.4 GB
Metadata Space Used: 942.1 kB
Metadata Space Total: 2.147 GB
Udev Sync Supported: true
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.90 (2014-09-01)
Execution Driver: native-0.2
Kernel Version: 3.17.4-301.fc21.x86_64
Operating System: Fedora 21 (Twenty One)
CPUs: 1
Total Memory: 978.3 MiB
Name: server.itzgeek.com
ID: 6DOI:SVVM:GP3C:4O7D:L5OG:VQBB:B4EO:C7GJ:4WNH:XR7L:U7BZ:LQFU

Working with Docker containers:

The search command allows you to search for Docker images in Docker registry, lets search images related to WordPress.

# docker search wordpress

The pull command allows you download Docker images from the registry . By default, it downloads from Docker public registry. Also, you can download images from your registry.

Here is the tutorial on how to push your Docker images to Docker Hub.

# docker pull centos

List the available Docker images on the system.

# docker images

You can remove downloaded images using rmi command; below command removes Ubuntu image from the local system.

# docker rmi ubuntu

The following command is widely used to create containers, uses the “centos” docker image to create a container.

# docker run -dit --name docker-centos --hostname="centos" centos /bin/bash

-d = Running a docker container in the background

-i = Running a docker container in interactive mode.

-t = Allocates tty terminal wich is required to attach to the container.

–name = Name of a docker container

–hostname = Set a host to container

Check the running containers using ps command.

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
1f99133e0387        centos:latest       "/bin/bash"         About a minute ago   Up About a minute                       docker-centos

The attach command lets you to attach to running container (docker-centos), you can see the hostname is set to centos, also run some test commands like “df -h” to see the mount points details.

[root@server ~]# docker attach docker-centos

[root@centos /]# df -h
Filesystem                                                                                     Size  Used Avail Use% Mounted on
/dev/mapper/docker-253:1-369-14d43fd37613411218528b599dd1c39a7b19ae2041a26a2cb170f52e8bc591e8  9.8G  254M  9.0G   3% /
tmpfs                                                                                          490M     0  490M   0% /dev
shm                                                                                             64M     0   64M   0% /dev/shm
/dev/mapper/fedora--server-root                                                                 50G  1.9G   45G   4% /etc/hosts
tmpfs                                                                                          490M     0  490M   0% /proc/kcore

The docker run command allows you to run a command in a container. For example, let’s get an information of mount points within a container.

–rm = removes the container when the process exits.

# docker run --rm  centos /usr/bin/df -h

Filesystem                                                                                     Size  Used Avail Use% Mounted on
/dev/mapper/docker-253:1-369-8c3daee9969e4096047fa7b6802cccefe2b78ac176347d5b0feb9d4df4156c6d  9.8G  254M  9.0G   3% /
tmpfs                                                                                          490M     0  490M   0% /dev
shm                                                                                             64M     0   64M   0% /dev/shm
/dev/mapper/fedora--server-root                                                                 50G  1.9G   45G   4% /etc/hosts
tmpfs                                                                                          490M     0  490M   0% /proc/kcore

The top command shows running process and their details.

# docker top docker-centos

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3442                2121                0                   21:44               pts/2               00:00:00            /bin/bash

The stats command does live stream of resource usage statistics, the output of this command will look like a normal top command.

# docker stats docker-centos

The cp command will help you to copy files/folders from containers to a host system; the following command will copy “to be copied” to /root of a host machine.

# docker cp docker-centos:/tobecopied /root/

The kill command sends the SIGTERM to kill a running container.

# docker kill docker-centos

The start command lets you start a stopped container; let’s start the docker-centos.

# docker start docker-centos

The restart command helps you to restart a container.

# docker restart docker-centos

The stop command lets you stop a container gracefully

# docker stop docker-centos

The rename command allows you to change the name of the container, following command rename the docker-centos to MyCentOS.

# docker rename docker-centos MyCentOS

The rm command will allow you to remove a container.

# docker rm MyCentOS

Hope you are now able to work with Docker containers.

Reference: Docker

ADVERTISEMENT

POSTS YOU MAY LIKE -:)

Share This Post

Shares