Monday, October 12, 2015

What is Docker?

According to industry analyst firm 451 Research, "Docker is a tool that can package an application and its dependencies in a virtual container that can run on any Linux server."

Docker works upon the resource isolation feature of Linux kernel, which allows independent “containers” to run within a single Linux instance. This functionality avoids starting and managing multiple virtual machines for the application.

Key Docker components:


Docker has main three key components Docker Images, Docker Registries and Docker Containers.

1. Docker Images:

Docker image is a kind of template that uses to create containers. User can create, update and use the docker image.

2. Docker Registries:

It is a public or private store that stores docker images. Docker Hub is a public store, which has large collection of images. Users can pull/download images from Docker Hub.

3. Docker Containers:

Docker container is a kind of directory, which holds everything that is needed for an application to run. Each container is isolated and secured application platform. Each container is created from a docker image. User can start, run, stop, move or delete container.

Installation

Mac

1) Install docker-toolbox.
Docker Machine for running the docker-machine binary
Docker Engine for running the docker binary
Docker Compose for running the docker-compose binary
Kitematic, the Docker GUI
A shell preconfigured for a Docker command-line environment
Oracle VM VirtualBox

2) Download docker-toolbox from https://www.docker.com/toolbox .

3) And follow installation steps. https://docs.docker.com/mac/step_one/

4) Verify installation.


Verify installation:

1. Open ‘docker quickstart terminal’ from the Launchpad. It provides docker command line environment. By default docker shell will connect to the virtual machine named ‘default’.

2. Run command:

$ docker run hello-world

Expected output:

Unable to find image ‘hello-world:latest’ locally latest: Pulling from library/hello-world 535020c3e8ad: Pull complete af340544ed62: Pull complete Digest: sha256:a68868bfe696c00866942e8f5ca39e3e31b79c1e50feaee4ce5e28df2f051d5c Status: Downloaded newer image for hello-world:latest

Hello from Docker. This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

The Docker client contacted the Docker daemon.
The Docker daemon pulled the “hello-world” image from the Docker Hub.
The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

Troubleshoot:


1) Not able to connect with ‘default’ docker machine?

[Error log on docker quick start terminal]

Reason:
Installing new virtual machine disturbs ‘Default’ docker machine port.

Description:
In mac docker works on docker machine, which already gets install with Docker Toolbox installation.
Docker machine is kind of virtual machine on which docker host exists. Docker machine has docker daemon, which accesses containers.
Docker client can manage different docker machines.
By default when user opens ‘quick start terminal’, docker client connects to the ‘default’ docker machine on particular port. It can be viewed with command:

$ docker-machine ls

Output to this command will give list of docker machines created on system with its status and running port.
If user try to install new virtual machine on system, then there are chances that newly created virtual machine disturbs the ‘default’ docker-machine’s running port.

Solution:
1. Open ‘docker quickstart terminal’.
2. Verify status of default docker-machine with:

$ docker-machine ls

3. Kill virtual machine named ‘default’. If status is Ruuning but not active then first of all stop machine and then kill it.

$ docker-machine stop default
$ docker-machine kill default

4. Verify deletion of the default docker machine with $ docker-machine ls.
5. Create new docker-machine with name ‘default’

$ docker-machine create --driver virtualbox default

6. Update docker machine environment if required with command:

$ eval "$(docker-machine env default)"

7. Verify the new docker machine with $ docker-machine ls.
8. Restart ‘docker quickstart terminal’.


Ubuntu 14.04 (LTS)

Requirements:
There are no pre requisites for this version

Installation steps:

1) Open ubuntu terminal. Log on to the user with ‘sudo’ privileges for installation.
2) Verify that curl is installed or not with:

$ which curl

If curl is not installed, install it with:

$ sudo apt-get update
$ sudo apt-get install curl

3) Get latest Docker package.

$ curl -sSL https://get.docker.com/ | sh

It downloads and then installs docker. Provide sudo password if system prompts.

Note: If company is behind a filtering proxy, you may find that the apt-key command fails for the Docker repo during installation. To work around this, add the key directly using the following:

$ curl -sSL https://get.docker.com/gpg | sudo apt-key add -

4) Verify installation.

Verify installation:

1. Open ubuntu terminal.
2. [Optional] Give root access with command:

$ sudo –su root

Provide root access password when system prompts.

3. Create or download new image from docker hub with command:

$ docker run hello-world
This command downloads a test image and runs it in a container.

Expected Output:

Hello from Docker.
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.


Getting started with Docker:

1) How to create new container with image?

Open docker bash.
Create new container with image with command:

$ docker run –t -i <image-name>:[<tag-name>]
example: $ docker create ubuntu
It will create new container with random container name and container ID. It will also create docker image with name ‘ubuntu’, tag name ‘latest’ and image ID. By default <tag-name> will peek latest. In command, -i provides interactive access to the created container image.

2) How to tag image?

There are two ways to tag image. First is at the time of the creation and second one is after creating image editing the tag name and create new image with customized tag name.

Create new image with specific tag name:

$ docker create <image-name>:<tag-name>
example: $ docker create ubuntu:14.04

It will create new container with random container name and container ID. It will also create docker image with name ‘ubuntu’, tag name ’14.04’ and image ID.

Customize image tag-name:

$ docker tag <image-ID> <image-name>:<new-tag name>
example: $ docker tag 7d9495d03763 ubuntu:14.04

It will peek image having image ID 7d9495d03763 and create new copy of docker image with name ‘ubuntu’ and tag name ’14.04’.

3) How to list all containers exists in docker host?

$ docker ps –a

4) How to list only running containers exists in docker host?

$ docker ps

5) How to list all images exists in docker host?

$ docker images

6) How to start stopped container?

$ docker start <container-ID> or <container-name>

7) How to stop running container?

$ docker stop <container ID> or <container-name>



8) How to stop running container?

$ docker exec -it <container ID or name> bash

9) How to remove container?

Container must not be in running mode while removing it. So if it is in running mode then first stop it and then remove it.

$ docker rm <container ID> or <container-name>

10) How to remove image?

$ docker rmi <image ID>

11) Create a Data volume container:

 If user has some persistent data that can be share between containers. In other words, if user wants to  use persistent data among non-persistent containers it should create a data volume.

 How to mount a data volume for the host resource path?

$ docker run –d –P –i –t –v <host-address>:<container-address> <image-name:tag> /bin/bash
Example: $ docker run –d –P –i –t –v /results/git/torrent-suite:/code ubuntu:14.04 /bin/bash

Command in example will create a new container having ‘/code’ directory in image ubuntu:14.04.
It will load data from the host machine adress ‘/results/git’ to the container address ‘/code’.
Inspect newly mounted data volume container with:

$ docker inspect <container name>

It should prompt output with the mount information as:

    "Mounts": [
        {
            "Source": "/results/git",
            "Destination": "/code",
            "Mode": "",
            "RW": true
        }
    ]

How to mount multiple data volumes for different the host resource path?

$ docker run –d –P –i –t –v <host-address1>:<container-address1> \
-v <host-address2>:<container-address2> \
<image-name:tag> /bin/bash


12) Create MySql container:

docker \
  run \
  --detach \
  --env MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} \
  --env MYSQL_USER=${MYSQL_USER} \
  --env MYSQL_PASSWORD=${MYSQL_PASSWORD} \
  --env MYSQL_DATABASE=${MYSQL_DATABASE} \
  --name ${MYSQL_CONTAINER_NAME} \
  --publish 3306:3306 \
  mysql:5.7;
docker run -d -e MYSQL_ROOT_PASSWORD='root' -e MYSQL_USER='root' -e MYSQL_PASSWORD='root' -e MYSQL_DATABASE='default' --name 'MY_SQL_SANKET' --publish 3306:3306 mysql

No comments:

Post a Comment