This Blog is to share our knowledge and expertise on Linux System Administration and VMware Administration

Showing posts with label Docker. Show all posts
Showing posts with label Docker. Show all posts

Thursday, February 15, 2018

Working with Docker Container - Labeling and filtering containers

Thursday, February 15, 2018 0
Labeling and filtering containers

With Docker 1.6, a feature has been added to label containers and images, through which we can attach arbitrary key-value metadata to them. You can think of them as environment variables, which are not available to running applications inside containers but they are available to programs (Docker CLI) that are managing images and containers. Labels attached to images also get applied to containers started via them. We can also attach labels to containers while starting them.

Docker also provides filters to containers, images, and events, which we can use in conjunction with labels to narrow down our searches.

For this document, let's assume that we have an image with the label, distro=centos.

root@Docker:~# docker images
REPOSITORY                 TAG                 IMAGE ID                CREATED            SIZE
nskselvan/nsk                   latest              b2f0c17eed23        2 months ago        197MB
centos-latest                     latest              2083898799b1       2 months ago        197MB
wordpress                         latest              224b7eef6944        3 months ago        408MB
mysql                                 5.7                 b4e78b89bcf3        3 months ago        412MB
registry                               2                    28525f9a6e46       3 months ago        33.2MB
localhost:5000/reg            latest               28525f9a6e46       3 months ago        33.2MB
centos                               latest              196e0ce0c9fb        3 months ago        197MB
localhost:5000/centos-ka  latest              196e0ce0c9fb        3 months ago        197MB

As you can see from the preceding screenshot, if we use filters with the docker images command, we only get an image where the corresponding label is found in the image's metadata.

To start the container with the --label/-l option, run the following command:
root@Docker:~# docker run --label environment=test centos date
Thu Feb 15 06:21:09 UTC 2018

Let's start a container without a label and start two others with the same label:
root@Docker:~# docker run --name container1 centos date
Thu Feb 15 06:22:13 UTC 2018
root@Docker:~# docker run --name tcontainer1 --label environment=test centos date
Thu Feb 15 06:23:01 UTC 2018
root@Docker:~# docker run --name tcontainer2 --label environment=test centos date
Thu Feb 15 06:23:20 UTC 2018

If we list all the containers without a label, we will see all the containers, but if we use label, then we get only containers, which matches the label.




Docker attaches label metadata to containers while starting them and matches the label while listing them or other related operations
We can list all the labels attached to a container through the inspect command

root@Docker:~# docker inspect -f '{{.Config.Labels}}' tcontainer1
map[build-date:20170911 environment:test license:GPLv2 name:CentOS Base Image vendor:CentOS]
root@Docker:~# docker inspect -f '{{.Config.Labels}}' tcontainer2
map[license:GPLv2 name:CentOS Base Image vendor:CentOS build-date:20170911 environment:test]

Wednesday, February 14, 2018

Working with Docker Container - Docker info Command

Wednesday, February 14, 2018 0
Docker info Command

As we know, images are required to run containers. You can either use existing images or create new custom images. You will need to create custom images to suit your development and deployment environment. Once you create an image, you can share it through the public or private registry. Before we explore more about Docker images, let's look at the output of the docker info command:

root@Docker:~# docker info
Containers: 26
 Running: 5
 Paused: 0
 Stopped: 21
Images: 6
Server Version: 17.09.0-ce
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 87
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 06b9cb35161009dcb7123345749fef02f7cea8e0
runc version: 3f2f8b84a77f73d38244dd690525642a72156c64
init version: 949e6fa
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.4.0-96-generic
Operating System: Ubuntu 16.04.3 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 3.859GiB
Name: Docker
ID: S3LP:G73H:NJWO:SVEG:I7U5:UB5M:B3LC:WAQV:AINP:7YYE:I7EN:MRFF
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Username: nskselvan
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support
root@Docker:~#

The preceding command gives the current system-wide info as follows:

It has 26 containers and 6 images.


The current storage driver, devicemapper, and its related information, such as thin pool name, data, metadata file, and so on. Other types of storage drivers are aufs, btrfs, overlayfs, vfs, and so on. Devicemapper, btrfs, and overlayfs have native support in the Linux kernel. AUFS support needs a patched kernel. 


To leverage the kernel features that enable containerization, the Docker daemon has to talk to the Linux kernel. This is done through the execution driver. libconatiner or native is one of that type. The others are libvirt, lxc, and so on.


The kernel version on the host operating system.
The user account that is registered on the registry mentioned in the next section to pull/push images.

Sunday, February 4, 2018

Working with Docker Container - Creating an image from the container

Sunday, February 04, 2018 0

Creating an image from the container

There are a couple of ways to create images, one is by manually committing layers and the other way is through Dockerfiles.

As we start a new container, a read/write layer gets attached to it. This layer will get destroyed if we do not save it. In this recipe, we will see how to save that layer and make a new image from the running or stopped container using the docker commit command.

Syntax: docker commit -a|--author[=""] -m|--message[=""] CONTAINER [REPOSITORY[:TAG]]

root@Docker:~# docker run -i -t centos /bin/bash
[root@91fb5dbccd0b /]# yum install -y httpd
Loaded plugins: fastestmirror, ovl
base                                                                | 3.6 kB  00:00:00
extras                                                              | 3.4 kB  00:00:00
updates                                                           | 3.4 kB  00:00:00
(1/4): base/7/x86_64/group_gz                       | 156 kB  00:00:00
(2/4): extras/7/x86_64/primary_db                  | 166 kB  00:00:00
(3/4): base/7/x86_64/primary_db                    | 5.7 MB  00:00:11
(4/4): updates/7/x86_64/primary_db               | 6.0 MB  00:00:13
...........
...........



As you can see, the new image is now being committed to the local repository with  nskselvan/centos  as a name and httpd as a tag.

we saw that while starting a container, a read/write filesystem layer will be created on top of the existing image layers from which the container started, and with the installation of a package, some files would have been added/modified in that layer. All of those changes are currently in the ephemeral read/write filesystem layer, which is assigned to the container. If we stop and delete the container, then all of the earlier mentioned modifications would be lost.

Using commit, we create a new layer with the changes that have occurred since the container started, which get saved in the backend storage driver.


root@Docker:~# docker diff 91fb5dbccd0b
C /.wh..wh.plnk
A /.wh..wh.plnk/1010.142994
.....................
A /boot/grub/splash.xpm.gz
C /etc/group
...................
C /etc/gshadow-
A /etc/httpd
A /etc/httpd/conf
A /etc/httpd/conf/httpd.conf
A /etc/httpd/conf/magic
A /etc/httpd/conf.d
...................
A /etc/httpd/conf.modules.d

We can see a prefix before each entry of the output. The following is a list of those prefixes:

    A: This is for when a file/directory has been added
    C: This is for when a file/directory has been modified
    D: This is for when a file/directory has been deleted


For more details, please check docker commit --help 

Friday, February 2, 2018

Working with Docker Container - Injecting a new process to a running container

Friday, February 02, 2018 0

 Injecting a new process to a running container

While doing development and debugging, we might want to look inside the already running container. There are a few utilities, such as nsenter (https://github.com/jpetazzo/nsenter), that allow us to enter into the namespace of the container to inspect it. With the exec option, we can inject a new process inside a running container.

Syntax:
    docker exec [-d|--detach[=false]] [--help] [-i|--interactive[=false]] [-t|--tty[=false]] CONTAINER COMMAND [ARG...]

Let's start an centos container and then inject uptime into that


root@Docker:~# id='docker run -d mysql'
root@Docker:~# docker run -it $id bash
root@b4e78b89bcf3#

For help with the docker run use --help

Friday, January 26, 2018

Working with Docker Container - Creating an account with Docker Hub

Friday, January 26, 2018 0

Creating an account with Docker Hub

Docker Hub is like GitHub for images. It is a public registry on which you can host images both public and private, share them and collaborate with others. It has integration with GitHub, Bitbucket, and can trigger automated builds.

As of now, the creation of an account on Docker Hub is free. A repository can hold different versions of an image. You can create any number of public repositories for your images. By default, you will have one private repository, which will not be accessible to the public. You can buy more private repositories. You can create an account either through a web browser or from the command line.

To create an account through a web browser on Docker Hub, visit https://hub.docker.com/account/signup/ and create an account:
 

Give the required details & and get created docker login.

To create an account using the command line, run the following command and submit the required details:
root@Docker:~# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (nskselvan):
Password: