martes, 3 de octubre de 2017

Docker Container

How to Get Shell Access to Running Docker Container

Rahul K.
In our previous docker tutorials your learned about how to Install Docker on CentOS or Install Docker on Debian based system. Also, write tutorials about how to create or delete docker containers. This tutorial will help you to get shell access to your running docker container.

#1. Using Docker Attach

You can get bash shell access in your docker container with attach command. But your docker container must be started with /bin/bash.
Use below syntax to get shell access of docker container.
$ sudo docker attach <CONTAINER ID/NAME>
For example, your docker container is running with id 76debad837d2 and name happy_tecadmin. Use one of the following commands with attach method.
Using Container ID:
$ sudo docker attach 76debad837d2
Using Container Name:
$ sudo docker attach happy_tecadmin

#2. Using Docker Exec

If docker container was not started with /bin/bash command. Then you can’t use attach command. Now, you need to use exec command to create bash in your container. Make sure you are using Docker version >= 1.3.
Use below syntax to get shell access of docker container.
$ sudo docker exec -it <CONTAINER ID/NAME> bash
For example, your docker container is running with id 76debad837d2 and name happy_tecadmin. Use one of the following commands with exec method.
Using Container ID:
$ sudo docker exec -it 76debad837d2  bash
Using Container Name:
$ sudo docker exec -it happy_tecadmin bash
 

How to Create, List & Delete Docker Containers on Linux

Rahul K.
In our previous tutorials you have learned about installation of Docker engine on CentOS/RHEL and Ubuntu operating system and pulled images from Docker hub. After that created containers with images. This tutorial will help you to create, list & delete docker containers on Linux machine.

#1. Launch Docker Container

To launch a new Docker container using below command. This will start a new container and provide you access to that container with /bin/bash shell.
# docker run [OPTIONS] <IMAGE NAME> [COMMAND] [ARG...] For example below command will create new docker container using the image named “ubuntu”. To list all available images use docker images command.
# docker run -i -t ubuntu /bin/bash To exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console. If you used exit command, it will stop the current container. Click here to read for more options about docker run command.

#2. List Docker Containers

After existing from Docker container, execute below command to list all running containers.
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours first_ubuntu By default Above command will list only running containers. To list all containers (including stopped container) use following command.
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours first_ubuntu 6b5b5a969241 centos "/bin/bash" 2 days ago Exited (0) 24 hours ago ubuntu-web

#3. Start/Stop/Attach Container

You can start, stop or attach to any containers with following commands. To start container use following command.
# docker start <CONTAINER ID|NAME> To stop container use following command.
# docker stop <CONTAINER ID|NAME> To attach to currently running container use following command.
# docker attach <CONTAINER ID|NAME>

#4. Drop Docker Container

Before deleting any container make sure that container is stopped. You can use ‘docker ps -a’ command to list status of containers. If container is still running first stop that container using given commands in above step.
Now use the following command to delete single or multiple containers.
# docker rm <CONTAINER ID|NAME> <CONTAINER ID|NAME> You can also delete all stopped containers at once using the following command.
# docker rm $(docker ps -a -q)

How to Search, Pull, List & Delete Docker Images on Linux

Rahul K.
In our previous tutorials you have learned about installation of Docker engine on CentOS/RHEL and Ubuntu operating system and pulled images from Docker hub. After that created containers with images. This tutorial will help you to search, pull, list and delete Docker images from your host system.

Search Docker Images

First of all search Docker container images from Docker hub. For example, below command will search all images with Ubuntu and list as output
# docker search ubuntu
The result will look like below
NAME                              DESCRIPTION          STARS     OFFICIAL   AUTOMATED

ubuntu                            Ubuntu is a Deb...   3318      [OK]
ubuntu-upstart                    Upstart is an e...   60        [OK]
torusware/speedus-ubuntu          Always updated ...   25                   [OK]
ubuntu-debootstrap                debootstrap --v...   24        [OK]
rastasheep/ubuntu-sshd            Dockerized SSH ...   22                   [OK]
neurodebian                       NeuroDebian pro...   17        [OK]
nuagebec/ubuntu                   Simple always u...   4                    [OK]
nickistre/ubuntu-lamp-wordpress   LAMP on Ubuntu ...   4                    [OK]
nimmis/ubuntu                     This is a docke...   3                    [OK]

Pull Docker Images

Now pull required docker image from docker hub on your local system using following commands. Below command will download image named “ubuntu”.
# docker pull ubuntu

latest: Pulling from library/ubuntu

fa5be2806d4c: Pull complete
b4af4261cb15: Downloading [==>               ] 3.779 MB/70.55 MB
5d358abc5d9c: Download complete
2933d50b9f77: Download complete
We are also downloading centos images from docker hub.
# docker pull centos

List Docker Images

Now make sure that above images has been downloaded successfully on your system. Below command list all images.
# docker images

REPOSITORY  TAG         IMAGE ID          CREATED           VIRTUAL SIZE
ubuntu      latest      36248ae4a9ac      2 hours ago       188 MB
centos      latest      2933d50b9f77      2 hours ago       196.6 MB

Remove Docker Images

To remove an images, Docker provides rmi option. Using this we can delete any docker images from our local system. For example use below command with changing IMAGE ID with your Docker image id.
# docker rmi  <IMAGE ID>
or you can simply remove images using repository name (image name)
# docker rmi ubuntu
In case you have two images with same name, add tag name while deletion
# docker rmi  ubuntu:latest

 

How to Move Docker Images between Hosts

Rahul K.
In our previous tutorials you have learned about installation of Docker engine on CentOS/RHEL and Ubuntu operating system and pulled images from Docker hub. After that created containers with images. This tutorial will help you to export and import Docker images and move them between hosts.

List Docker Images

Fist list docker images on your system using below command.
root@tecadmin:~# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos latest 2933d50b9f77 11 days ago 196.6 MB ubuntu latest 36248ae4a9ac 11 days ago 188 MB

Save or Archive Image

Now use the following command to save image repository named ubuntu (image id: 36248ae4a9ac) and make a zipped archive named ubuntu-latest.tar.gz. Remember that save is used for making backup of docker images (not containers).
# docker save ubuntu | gzip > ubuntu-latest.tar.gz

Import Image

After saving docker image in archive format on your system move it to remote system using scp or ftp. After that use below command on remote system to import Docker image with name ubuntu and tag name latest.
# zcat ubuntu-latest.gz | docker import - ubuntu:latest The above command will create a docker image with name ubuntu and tag name latest on your system. You can now launch containers using this image like below.

# docker run -i -t ubuntu /bin/bash


How to Export and Import Docker Containers

Rahul K.
In our previous tutorials you have learned about installation of Docker engine on CentOS/RHEL and Ubuntu operating system and pulled images from Docker hub. After that created containers with images. This tutorial will help you to export and import Docker containers and move them between hosts.

List Containers

Fist list all containers on your system using below command. Using ps -a will list all containers (running and stopped) from your system.
root@tecadmin:~# docker ps -a

CONTAINER ID   IMAGE    COMMAND      CREATED       STATUS      PORTS  NAMES
f2582758af13   ubuntu   "/bin/bash"  2 hours ago   Up 2 hours         ubuntu-web
2b199b9976c4   centos   "/bin/bash"  3 days ago    Up 3 hours         centos-mysql

Export Container

Finally use the following command to export container named ubuntu-web (container id: f2582758af13) and make a zipped archive named ubuntu-web.tar.gz. Remember that export is used for making backup of docker containers (not images) in image format.
# docker export ubuntu-web | gzip > ubuntu-web.tar.gz

Import Container

After exporting docker container on your system move it to remote server using scp or ftp. After that use below command on remote server to import Docker container on remote server with name ubuntu-web.
# zcat ubuntu-web.gz | docker import - ubuntu-web
The above command will create a docker image on your system. You can now launch a container from this image using below command.
# docker run -i -t ubuntu-web /bin/bash 
 

How to Remove Docker Images and Containers

Rahul K.
In our previous tutorials you have learned about installation of Docker engine on CentOS/RHEL and Ubuntu operating system and pulled images from Docker hub. After that created containers with images. This tutorial will help you to remove unnecessary Docker images and containers from your host machine.

Remove Docker Images

To remove an images, Docker provides rmi option. Using this we can delete any docker images from our local system. For example use below command with changing <IMAGE ID> with your Docker image id.
# docker rmi <IMAGE ID> To find all images on your system use following command. This will help you to find ID of images.
root@tecadmin:~# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos latest 2933d50b9f77 11 days ago 196.6 MB ubuntu latest 36248ae4a9ac 11 days ago 188 MB

Remove Docker Containers

To remove a containers, Docker provides rm option. Using this we can delete any docker containers from our local system. For example use below command with changing <CONTAINER ID> with your Docker container id.
# docker rm <CONTAINER ID> To list all containers on your system using ps option, but ps will show only running containers. So to view all containers use -a parameter with ps.
root@tecadmin:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours first_ubuntu 2b199b9976c4 centos "/bin/bash" 3 days ago Up 3 hours thirsty_yalow

Stop & Remove All Docker Containers

If you want to remove all docker containers. You can use simply following commands. The first command will stop all running docker containers and the second command will delete them.
Stop All Containers
# docker stop $(docker ps -a -q) Remove All Containers
# docker rm $(docker ps -a -q)

How to Install and Manage Docker Engine on CentOS/RHEL & Fedora

Rahul K.
Docker is container-based application framework, which wrap of a specific application with all its dependencies in a container. Docker containers can easily to ship to remote location on start there without making entire application setup. This tutorial will help you to install and manage Docker on CentOS/RHEL 7/6 operating system.

#1 – Verify Requirements

For standard installation Docker required 64 bit operating system having Kernel >= 3.10 version. Older versions of Kernel have some missing requirements to run all features of Docker.
$ uname -r

3.19.0-49-generic
Also install the following packages on your system.
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

#2 – Add Docker Yum Repository

Let’s add the official Docker yum repository on your system.
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

#3 – Install Docker

First update the yum chache for local system.
$ sudo yum makecache fast
Now install docker community edition package to install docker on your system. This is install many of required decencies on your system.
$ sudo yum install docker-ce
After successful installation of Docker engine, Let’s start the docker service.
$ sudo service docker start

4. Manage Docker Container

Search Docker Images

First of all search Docker container images from Docker hub. For example, below command will search all images with Ubuntu and list as output
# docker search centos

Download Docker Images

Now download the Docker container with name Ubuntu on your local system using following commands.
# docker pull ubuntu

latest: Pulling from library/ubuntu

fa5be2806d4c: Pull complete
b4af4261cb15: Downloading [==>               ] 3.779 MB/70.55 MB
5d358abc5d9c: Download complete
2933d50b9f77: Download complete
Now make sure that above images has been downloaded successfully on your system. Below command list all images.
# docker images

REPOSITORY  TAG            IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu      latest         36248ae4a9ac        2 days ago          188 MB
centos      latest         2933d50b9f77        2 days ago          196.6 MB

Launch New Container with Image

Finally launch a Docker container using above downloaded image on your system. Below command will start a new container and provide you access of that container with /bin/bash shell.
# docker run -i -t ubuntu /bin/bash
To exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console. If you used exit command, it will stop the current container.
After existing from Docker container, execute below command to list all running containers.
# docker ps

CONTAINER ID     IMAGE     COMMAND        CREATED        STATUS        PORTS    NAMES
f2582758af13     ubuntu    "/bin/bash"    2 hours ago    Up 2 hours             first_ubuntu
By default Above command will list only running containers. To list all containers (including stopped container) use following command.
# docker ps -a

Start/Stop/Attach Container

You can start, stop or attach to any containers with following commands. To start container use following command.
# docker start <CONTAINER ID>
To stop container use following command.
# docker stop <CONTAINER ID>
To attach to currently running container use following command.
# docker attach <CONTAINER ID>


How to Install and Manage Docker CE on Ubuntu 16.04 & 14.04 LTS

Rahul K.
Docker is container-based application framework, which wrap of a specific application with all its dependencies in a container. Docker containers can easily to ship to remote location on start there without making entire application setup. This tutorial will help you to install and manage Docker on Ubuntu 17.04, 16.04 LTS & 14.04 LTS operating system.

#1 – Install Requirements

First of all install the required packages for docker installation.
$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
For standard installation Docker required 64 bit operating system having Kernel >= 3.10 version. Older versions of Kernel have some missing requirements to run all features of Docker.
$ uname -r

4.4.0-21-generic
Ubuntu 14.04 LTS users recommended to install the linux-image-extra kernel package.
$ sudo apt-get install linux-image-extra-$(uname -r)

#2 – Add Docker Apt Repository

Import dockers official GPG key to verify packages signature before installing them with apt-get.
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add 
Run the below comamnd to enable the docker official apt-get repository on your system. This will add entry in systems /etc/apt/sources.list file.
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

#3 – Install Docker on Ubuntu

Remove the old packages installed on your system and purge them using below command.
$ sudo apt-get purge docker lxc-docker docker-engine
Now use the following commands to upgrade apt index and install docker engine on your system.
$ sudo apt-get update
$ sudo apt-get install docker-ce
After successful installation of Docker ce, the service will start automatically, Use below command to verify service status.
$ sudo systemctl status docker
Use our Docker Tutorial for Beginners to working with Docker.

#4 – Manage Docker Container

Search Docker Images

First of all search Docker container images from Docker hub. For example, below command will search all images with Ubuntu and list as output.
# docker search ubuntu

Download Docker Images

Now download the Docker container with name Ubuntu on your local system using following commands.
# docker pull ubuntu

latest: Pulling from library/ubuntu

fa5be2806d4c: Pull complete
b4af4261cb15: Downloading [==>               ] 3.779 MB/70.55 MB
5d358abc5d9c: Download complete
2933d50b9f77: Download complete
Now make sure that above images has been downloaded successfully on your system. Below command list all images.
# docker images

REPOSITORY  TAG            IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu      latest         36248ae4a9ac        7 days ago          188 MB

Launch New Container with Image

Finally launch a Docker container using above downloaded image on your system. Below command will start a new container and provide you access of that container with /bin/bash shell.
# docker run -i -t ubuntu /bin/bash
To exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console. If you used exit command, it will stop the current container.
After existing from Docker container, execute below command to list all running containers.
# docker ps

CONTAINER ID     IMAGE     COMMAND        CREATED        STATUS        PORTS    NAMES
f2582758af13     ubuntu    "/bin/bash"    2 hours ago    Up 2 hours             first_ubuntu
By default Above command will list only running containers. To list all containers (including stopped container) use following command.
# docker ps -a

Start/Stop/Attach Container

You can start, stop or attach to any containers with following commands. To start container use following command.
# docker start <CONTAINER_ID>
To stop container use following command.
# docker stop <CONTAINER_ID>
To attach to currently running container use following command.
# docker attach <CONTAINER_ID>

 

martes, 7 de febrero de 2017

Linux KVM: Disable virbr0 NAT Interface

The virtual network (virbr0) used for Network address translation (NAT) which allows guests to access to network services. However, NAT slows down things and only recommended for desktop installations. To disable Network address translation (NAT) forwarding type the following commands:

Display Current Setup

Type the following command:
# ifconfig
Sample outputs:
virbr0    Link encap:Ethernet  HWaddr 00:00:00:00:00:00  
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:39 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:7921 (7.7 KiB)
Or use the following command:
# virsh net-list
Sample outputs:
Name                 State      Autostart
-----------------------------------------
default              active     yes       
To disable virbr0, enter:
# virsh net-destroy default
# virsh net-undefine default
# service libvirtd restart
# ifconfig