Showing posts with label docker. Show all posts
Showing posts with label docker. Show all posts

Wednesday, March 18, 2020

Basic Docker Commands


Docker – ‘A better way to build apps’, as stated on its website, is an open-source platform for building apps and microservices. The catch here is the automated deployment of your app in a container, by OS level virtualization provided by Dockers. Dockers are better than VMs as you can do away with the additional costs for maintaining and starting the latter. By deploying your app and its dependencies (i.e. the pre-requisite apps for its proper functioning) in a container, your app becomes portable during all the phases of development and testing. Moreover, the isolated apps eliminate conflicts, enable team collaboration, and reduce the time-to-market.

When Do You Need to Use a Docker?

  • For replicating the environment on your server, while running your code locally on your laptop
  • For Docker CI/CD during numerous development phases (dev/test/QA)
  • For distributing your app’s OS with a team, and as a version control system.

How Do You Setup a Docker Locally

  • Download a Docker edition and the Docker Toolbox
  • Make sure your BIOS has Virtualization Technologies, AMD-V, or KVM enabled
  • Install the Extension Pack in the Oracle VirtualBox.
  • Run the Setup

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 searching docker images and seeing the way they have been built.
  • To create a Docker container, download the ‘hello world’ image, by typing the following command in the terminal –
$ docker run hello world
  • For checking the number of images on your system, use the following command –
$ docker images
  • For searching an image in the Docker Hub –
$ docker search <image>

Here’s a List of Docker Commands

  • docker run – Runs a command in a new container.
  • docker start – Starts one or more stopped containers
  • docker stop – Stops one or more running containers
  • docker build – Builds an image form a Docker file
  • docker pull – Pulls an image or a repository from a registry
  • docker push – Pushes an image or a repository to a registry
  • docker export – Exports a container’s filesystem as a tar archive
  • docker exec – Runs a command in a run-time container
  • docker search – Searches the Docker Hub for images
  • docker attach – Attaches to a running container
  • docker commit – Creates a new image from a container’s changes

Examples of Using a Docker

  • You can run WordPress locally on your laptop by downloading Docker, without having to install Apache, PHP, MySQL etc. The Docker Toolbox creates a containerized version of Linux to run the Docker in a VM.
    • Download Docker Tool Box which will install the Oracle VirtualBox.
    • Install the Extension Pack in the VirtualBox.
    • Type $ docker run hello-world in the terminal to check if your installation has finished properly.
    • Search for a WordPress image on the Docker Hub to install WordPress locally.
  • Similarly, you can install DokuWiki using dockers.
  • Dockers can be used for testing SDN components
Here are a few samples to get your docker engine up and running.
*All the examples in this article are for installing Docker on Windows. You can always run it on a Linux VM.

Saturday, August 27, 2016

Docker installation on rhel7



How to install and setup Docker on RHEL 7



How to install and use Docker on RHEL 7 or CentOS 7 (method 1)
The procedure to install Docker is as follows:
  1. Open the terminal application or login to the remote box using ssh command:
    ssh user@remote-server-name
  2. Type the following command to install Docker via yum provided by Red Hat:
    sudo yum install docker
  3. Type the following command to install the latest version of Docker CE (community edition):
    sudo yum remove docker docker-common docker-selinux docker-engine
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sudo yum install docker-ce
Let us see all info in details along with examples.

How to install Docker on CentOS 7 / RHEL 7 using yum

Type the following yum command:
$ sudo yum install docker
Install Docker on RHEL 7 using yum command

How to install Docker CE on CentOS 7 (method 2)

First remove older version of docker (if any):
$ sudo yum remove docker docker-common docker-selinux docker-engine-selinux docker-engine docker-ce
Next install needed packages:
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Configure the docker-ce repo:
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
Finally install docker-ce:
$ sudo yum install docker-ce
Install Docker on CentOS 7 using yum command CE Version

How to enable docker service

$ sudo systemctl enable docker.service
Sample outputs:
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

How to start/stop/restart docker service on CentOS7/RHEL7

$ sudo systemctl start docker.service ## <-- Start docker ##
$ sudo systemctl stop docker.service ## <-- Stop docker ##
$ sudo systemctl restart docker.service ## <-- Restart docker ##
$ sudo systemctl status docker.service ## <-- Get status of docker ##

Sample outputs:
List status of Docker on CentOS RHEL server

How to find out info about Docker network bridge and IP addresses

Default network bridge named as docker0 and is assigned with an IP address. To find this info run the following ip command:
$ ip a
$ ip a list docker0

Sample outputs:
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:cd:c0:6d:4a brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever

How to run docker commands

The syntax is:
docker command
docker command arg
docker [options] command arg
docker help | more

Get system-wide information about Docker

docker info

Getting help

docker help | more
Sample outputs:
Docker help
Run 'docker COMMAND --help' for more information on a command:
docker ps --help
docker cp --help

How to test your docker installation

Docker images are pulled from docker cloud/hub such as docker.io or registry.access.redhat.com and so on. Type the following command to verify that your installation working:
docker run hello-world
Sample outputs:
Run docker hello world for testing

How to search for Docker images

Now you have working Docker setup. It is time to find out images. You can find images for all sort of open source projects and Linux distributions. To search the Docker Hub/cloud for nginx image run:
docker search nginx
Sample outputs:
Docker search image
Click to enlarge

How to install Docker nginx image

To pull an image named nginx from a registry, run:
docker pull nginx
Sample outputs:
Docker pull nginx image (gif)

How to run Docker nginx image

Now you pulled image, it is time to run it:
docker run --name my-nginx-c1 --detach nginx
Say you want to host simple static file hosted in /home/vivek/html/ using nginx container:
docker run --name my-nginx-c2 -p 80:80 -v /home/vivek/html/:/usr/share/nginx/html:ro -d nginx
Where,
  • --name my-nginx-c1 : Assign a name to the container
  • --detach : Run container in background and print container ID
  • -v /home/vivek/html/:/usr/share/nginx/html:ro : Bind mount a volume
  • -p 80:80 : Publish a container's port(s) to the host i.e redirect all traffic coming to port 80 to container traffic
Go ahead and create a file named index.html in /home/vivek/html/:
echo 'Welcome. I am Nginx server locked inside Docker' > /home/vivek/html/index.html
Test it:
curl http://your-host-ip-address/
curl 192.168.122.188

Sample outputs:
Welcome. I am Nginx server locked inside Docker

How to list running Docker containers

docker ps
docker ps -a

Sample outputs:
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                NAMES
bb9d85a56a92        nginx               "nginx -g 'daemon of…"   55 seconds ago       Up 54 seconds       0.0.0.0:80->80/tcp   my-nginx-c2
fe0cdbc0225a        nginx               "nginx -g 'daemon of…"   About a minute ago   Up About a minute   80/tcp               my-nginx-c1
You can use CONTAINER ID to stop, pause or login into the container.

How to run a command in a running container

Run ls /etc/nginx command for my-nginx-c1 container
docker exec fe0cdbc0225a ls /etc/nginx
OR
docker exec my-nginx-c1 ls /etc/nginx
Want to gain bash shell for a running container and make changes to nginx image?
docker exec -i -t fe0cdbc0225a bash
OR
docker exec -i -t my-nginx-c1 bash

How to stop running containers

docker stop my-nginx-c1
OR
docker stopfe0cdbc0225a

How to remove docker containers

docker rm my-nginx-c1
docker ps -a




Networking terms in basic level

Basics of Computer Networking Open system: A system which is connected to the network and is ready for communication. Closed system...