Sunday, November 12, 2017

9. Docker container networking

                               Docker container networking

         Docker takes care of the networking aspects so that the containers can communicate with other containers and also with the Docker Host. If you do an ifconfig on the Docker Host, you will see the Docker Ethernet adapter. This adapter is created when Docker is installed on the Docker Host.
Default networks
When you install Docker, it creates three networks automatically.
You can list these networks using the docker network ls command:

# docker network ls

NETWORK ID          NAME                DRIVER
7fca4eb8c647        bridge              bridge
9f904ee27bf5        none                null
cf03ee007fb4        host                host


#ip addr show

docker0   Link encap:Ethernet  HWaddr 02:42:47:bc:3a:eb
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:47ff:febc:3aeb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:9001  Metric:1
          RX packets:17 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1100 (1.1 KB)  TX bytes:648 (648.0 B)

Inspecting a Docker network

If you want to see more details on the network associated with Docker, you can use the Docker network inspect command.
#docker network inspect networkname

networkname – This is the name of the network you need to inspect.

Example
#sudo docker network inspect bridge

Now let’s run a container and see what happens when we inspect the network again. Let’s spin up an Ubuntu container with the following command:

#docker run –it ubuntu:latest /bin/bash

Now if we inspect our network name via the following command, you will now see that the container is attached to the bridge.

#docker network inspect bridge

[
   {
       "Name": "bridge",
       "Id": "f7ab26d71dbd6f557852c7156ae0574bbf62c42f539b50c8ebde0f728a253b6f",
       "Scope": "local",
       "Driver": "bridge",
       "IPAM": {
           "Driver": "default",
           "Config": [
               {
                   "Subnet": "172.17.0.1/16",
                   "Gateway": "172.17.0.1"
               }
           ]
       },
       "Containers": {},
       "Options": {
           "com.docker.network.bridge.default_bridge": "true",
           "com.docker.network.bridge.enable_icc": "true",
           "com.docker.network.bridge.enable_ip_masquerade": "true",
           "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
           "com.docker.network.bridge.name": "docker0",
           "com.docker.network.driver.mtu": "9001"
       },
       "Labels": {}
   }
]

Creating Your Own New Network

One can create a network in Docker before launching containers. This can be done with the following command:

Syntax

#docker network create –-driver drivername name

Options
drivername – This is the name used for the network driver.

name – This is the name given to the network.

Return Value
The command will output the long ID for the new network.




Example

#docker network create --driver bridge isolated_nw

1196a4c5af43a21ae38ef34515b6af19236a3fc48122cf585e3f3054d509679b

$ docker network inspect isolated_nw

[
    {
        "Name": "isolated_nw",
        "Id": "1196a4c5af43a21ae38ef34515b6af19236a3fc48122cf585e3f3054d509679b",
        "Scope": "local",
        "Driver": "bridge",
        "IPAM": {
            "Driver": "default",
            "Config": [
                {
                    "Subnet": "172.21.0.0/16",
                    "Gateway": "172.21.0.1/16"
                }
            ]
        },
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

$ docker network ls

NETWORK ID          NAME                DRIVER
9f904ee27bf5        none                null
cf03ee007fb4        host                host
7fca4eb8c647        bridge              bridge
c5ee82f76de3        isolated_nw         bridge

The output of the above command is shown below:
You can now attach the new network when launching the container. So let’s spin up an

Ubuntu container with the following command:


#docker run –it –network=new_nw ubuntu:latest /bin/bash

And now when you inspect the network via the following command, you will see the container attached to the network.

#docker network inspect new_nw

Tuesday, November 7, 2017

Docker Real Time Interview Questions

                       Docker Real Time Interview Questions

            
           Docker is one of the fastest growing software in technology world.


        Engineers with Docker knowledge may get more salary than others with similar qualifications without Docker knowledge. Here i tried to cover all the Interview questions without answers try to test you knowledge.

Q1. What is Docker?
Q2. What are the differences between Docker and Hypervisors?
Q3.What is Docker image?
Q4.What’s the benefit of “Dockerizing?”
Q5. How does Docker help manage my infrastructure? Do I containerize all my infrastructure                     or something?

Q6.Tell us how you have used Docker in your past position?
Q7. What is Docker container?
Q8 What is Docker hub?
Q9. How is Docker different from other container technologies?
Q10. What is Docker Swarm?

Q11. What is Dockerfile used for?
Q12. Can I use json instead of yaml for my compose file in Docker?
Q13. Tell us how you have used Docker in your past position?
Q14. How to create Docker container?
Q15. How to stop and restart the Docker container?

Q16. What platforms does Docker run on?
Q17 How far do Docker containers scale?
Q18. Do I lose my data when the Docker container exits?
Q19. Mention some commonly used Docker command?
Q20. How far do Docker containers scale?

Q21. Can I use json instead of yaml for my compose file in Docker?
Q22. Do I lose my data when the Docker container exits?
Q23. Is Container technology new?
Q24. How is Docker different from other container technologies?
Q25 .Difference between Docker Image and container?

Q26. How exactly containers (Docker in our case) are different from hypervisor virtualization                       (vSphere)? What are the benefits.?
Q27. How did you become involved with the Docker project?
Q28. Docker is the new craze in virtualization and cloud computing. Why are people so excited                   about  it?
Q29. How do you think Docker will change virtualization and cloud environments? Do you think                 cloud technology has a set trajectory, or is there still room for significant change?
Q30. What’s the difference between up,run, and start?

Q31. What is a Docker container and how is it different than a VM?  Does containerization replace               my virtualization infrastructure?
Q32. How many containers can run per host?

Q33. What do I have to do to begin the “Dockerization process”?
Q34. How do I run multiple copies of a Compose file on the same host?
Q35. Will cloud automation overtake containerization any sooner?

  Guys for answer of all these Questions plz go through my previous blogs. 
                      Reference- Qura.com , Tekslate.com, edureka.co ...

Friday, November 3, 2017

8. Top Important Docker Commands



List of Docker Commands-

Google Image

  • How Do You Use a Docker?

The biggest advantage of VMs is that they create snapshots which can be revisited instantly later.
Docker containers further enhance the lightweight process virtualization by being OS independent and using the Linux Kernel’s functionality. They are created from Docker images – like snapshots. Docker images are created using a Docker file which can be customized or used as is. The default execution driver for creating a docker container is ‘libcontainer’.  Docker Hub can be used for serching docker images and seeing the way they have been built.


CommandDescription
docker attachAttach local standard input, output, and error streams to a running container
docker buildBuild an image from a Dockerfile
docker checkpointManage checkpoints
docker commitCreate a new image from a container’s changes
docker configManage Docker configs
docker containerManage containers
docker cpCopy files/folders between a container and the local filesystem
docker createCreate a new container
docker deployDeploy a new stack or update an existing stack
docker diffInspect changes to files or directories on a container’s filesystem
docker eventsGet real time events from the server
docker execRun a command in a running container
docker exportExport a container’s filesystem as a tar archive
docker historyShow the history of an image
docker imageManage images
docker imagesList images
docker importImport the contents from a tarball to create a filesystem image
docker infoDisplay system-wide information
docker inspectReturn low-level information on Docker objects
docker killKill one or more running containers
docker loadLoad an image from a tar archive or STDIN
docker loginLog in to a Docker registry
docker logoutLog out from a Docker registry
docker logsFetch the logs of a container
docker networkManage networks
docker nodeManage Swarm nodes
docker pausePause all processes within one or more containers
docker pluginManage plugins
docker portList port mappings or a specific mapping for the container
docker psList containers
docker pullPull an image or a repository from a registry
docker pushPush an image or a repository to a registry
docker renameRename a container
docker restartRestart one or more containers
docker rmRemove one or more containers
docker rmiRemove one or more images
docker runRun a command in a new container
docker saveSave one or more images to a tar archive (streamed to STDOUT by default)
docker searchSearch the Docker Hub for images
docker secretManage Docker secrets
docker serviceManage services
docker stackManage Docker stacks
docker startStart one or more stopped containers
docker statsDisplay a live stream of container(s) resource usage statistics
docker stopStop one or more running containers
docker swarmManage Swarm
docker systemManage Docker
docker tagCreate a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker topDisplay the running processes of a container
docker unpauseUnpause all processes within one or more containers
docker updateUpdate configuration of one or more containers
docker versionShow the Docker version information
docker volumeManage volumes
docker waitBlock until one or more containers stop, then print their exit codes
Note- These all above commands are basic commands. According to your Permissions try to use. 

For More Details-

Wednesday, November 1, 2017

7. Docker Container

Docker Container 


       Containers are instances of Docker images that can be run using the Docker run command. The basic purpose of Docker is to run containers. Let’s discuss how to work with containers.

Run a Container-

Running of containers is managed with the Docker run command. To run a container in an interactive mode, first launch the Docker container.

    #sudo docker run –it centos /bin/bash

Then hit Crtl+p and you will return to your OS shell.

Listing of Containers-

One can list all of the containers on the machine via the docker ps command. This command is used to return the currently running containers.

#docker ps

docker ps -a-
This command is used to list all of the containers on the system

#docker ps -a

  ─a It tells the docker ps command to list all of the containers on the system.

docker history-
With this command, you can see all the commands that were run with an image via a container.

#docker history ImageID

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

docker top-
With this command, you can see the top processes within a container.

#docker top ContainerID

ContainerID – This is the Container ID for which you want to see the top processes

docker stop-
This command is used to stop a running container.

#docker stop ContainerID

ContainerID This is the Container ID which needs to be stopped.

docker rm-
This command is used to delete a container.

#docker rm ContainerID

ContainerID This is the Container ID which needs to be removed.

Return Value

The output will give the ID of the removed container.

docker stats-
This command is used to provide the statistics of a running container.

#docker stats ContainerID

ContainerID This is the Container ID for which the stats need to be provided.

Return Value

The output will show the CPU and Memory utilization of the Container.

docker attach-
This command is used to attach to a running container.

#docker attach ContainerID

Once you have attached to the Docker container, you can run the above command to see the process utilization in that Docker container.


docker pause
This command is used to pause the processes in a running container.

#docker pause ContainerID


docker unpause-
This command is used to unpause the processes in a running container.

#docker unpause ContainerID

The Docker pause command is used to pause an existing Docker container

docker kill-
This command is used to kill the processes in a running container.

#docker kill ContainerID

Docker – Container Lifecycle-
The following illustration explains the entire lifecycle of a Docker container.

             
                 Initially, the Docker container will be in the created state.

             Then the Docker container goes into the running state when the Docker      run command is used.

            The Docker kill command is used to kill an existing Docker container.
            The Docker stop command is used to pause an existing Docker container. 

      The Docker run command is used to put a container back from a stopped state to a running state




6. Docker Image

Docker Image 

        In Docker, everything is based on Images. An image is a combination of a file system and parameters. Let’s take an example of the following command in Docker.





   #docker run hello-world

1. The Docker command is specific and tells the Docker program on the Operating System that something needs to be done.

2.The run command is used to mention that we want to create an instance of an image, which is then called a container.

3. Finally, "hello-world" represents the image from which the container is made.

    Now let’s look at how we can use the CentOS image available in Docker Hub to run CentOS on our Ubuntu machine. We can do this by executing the following command on our Ubuntu machine.

     #sudo docker run centos –it /bin/bash

       Note the following points about the above sudo command:

We are using the sudo command to ensure that it runs with root access.

*Here, centos is the name of the image we want to download from Docker Hub and install on our Ubuntu machine.

*    ─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.

Displaying Docker Images:-
To see the list of Docker images on the system, you can issue the following command.

   #docker images

This command is used to display all the images currently installed on the system.

Return Value
This command is used to display all the images currently installed on the system.


From the above output, you can see that the server has three images: centos, newcentos, and jenkins. Each image has the following attributes:

·     TAG This is used to logically tag images.

·     Image ID This is used to uniquely identify the image.

·     Created The number of days since the image was created.

·     Virtual Size The size of the image.

Downloading Docker Images:- 

Images can be downloaded from Docker Hub using the Docker run command. Let’s see in detail how we can do this.

Syntax

The following syntax is used to run a command in a Docker container.

#docker run imageName

Return Value

The output will run the command in the desired container.


You will now see the CentOS Docker image downloaded. Now, if we run the Docker images command to see the list of images on the system, we should be able to see the centos image as well.


Removing Docker Images:-
The Docker images on the system can be removed via the docker rmi command. Let’s look at this command in more detail.

#docker rmi ImageID

Docker Images ID :-
This command is used to return only the Image ID’s of the images

#docker images -q

Docker Inspect-

This command is used see the details of an image or container.
#docker inspect Repository

Repository This is the name of the Image

Example-
#sudo docker inspect jenkins

When we run the above command, it will produce the following result:


For More Details..

9. Docker container networking

                                Docker container networking          Docker takes care of the networking aspects so that the containers c...