Tuesday, December 17, 2019

Docker Tutorials

Install Portainer for docker GUI or Docker Management Portal

https://portainer.io/install.html

Before installation of docker we need to disable Firewall and Selinux

docker overview
1) docker containers
2) docker image
3) docker compose
4) docker swarm
5) Networking in docker

There are two types of docker edition

1) docker community edition is free
2) docker Enterprise edition

For docker community edition installation refer below links and install docker

URL:
1) https://docs.docker.com/v17.09/engine/installation/linux/docker-ce/centos/#install-docker-ce-1
2) https://docs.docker.com/install/linux/docker-ce/centos/
3) https://docs.docker.com/v17.09/engine/installation/linux/centos/
4) https://www.vultr.com/docs/installing-docker-ce-on-centos-7

Once docker is installed below are the start and stop commands

systemctl start docker
systemctl status docker
systemctl stop docker

Please find below Docker commands and descriptions
Commands:
attach          Attach to a running container
build           Build an image from a Dockerfile
commit       Create a new image from a container's changes
cp               Copy files/folders between a container and the local filesystem
create         Create a new container
diff             Inspect changes on a container's filesystem
events        Get real time events from the server
exec           Run a command in a running container
export        Export a container's filesystem as a tar archive
history       Show the history of an image
images       List images
import       Import the contents from a tarball to create a filesystem image
info           Display system-wide information
inspect      Return low-level information on a container, image or task
kill            Kill one or more running containers
load          Load an image from a tar archive or STDIN
login        Log in to a Docker registry.
logout      Log out from a Docker registry.
logs          Fetch the logs of a container
network   Manage Docker networks
node        Manage Docker Swarm nodes
pause      Pause all processes within one or more containers
port        List port mappings or a specific mapping for the container
ps           List containers
pull        Pull an image or a repository from a registry
push      Push an image or a repository to a registry
rename  Rename a container
restart    Restart a container
rm         Remove one or more containers
rmi        Remove one or more images
run        Run a command in a new container
save       Save one or more images to a tar archive (streamed to STDOUT by default)
search   Search the Docker Hub for images
service  Manage Docker services
start      Start one or more stopped containers
stats      Display a live stream of container(s) resource usage statistics
stop      Stop one or more running containers
swarm  Manage Docker Swarm
tag       Tag an image into a repository
top       Display the running processes of a container
unpause  Unpause all processes within one or more containers
update    Update configuration of one or more containers
version   Show the Docker version information
volume   Manage Docker volumes
wait       Block until a container stops, then print its exit code

1) run --- start a container --- docker run nginx:latest
2) ps --- List containers or running container-- ps -a
3) stop ---- stop a container
4) rm ----- remove or stop a container
5) images --- list images (downloaded images show)
6) rmi ---- remove images
7) pull ---- download an image
8) append a command --- docker run ubuntu sleep 10000
9) exec --- execute a command

Related commands
Command Description
docker image build Build an image from a Dockerfile
docker image history Show the history of an image
docker image import Import the contents from a tarball to create a filesystem image
docker image inspect Display detailed information on one or more images
docker image load Load an image from a tar archive or STDIN
docker image ls List images
docker image prune Remove unused images
docker image pull Pull an image or a repository from a registry
docker image push Push an image or a repository to a registry
docker image rm Remove one or more images
docker image save Save one or more images to a tar archive

docker image tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

docker attach web_server ---- try this command

docker run -i web --- i for input

docker run -v /opt/db:/var/lib/mysql db_server ---mapping volume

docker run ubuntu sleep 15 ---- sleep of 15 second

docker run -d ubuntu sleep 15 --- detached mode in back ground

docker attach web_server ---- before this use command , use run command ^ start container

docker version ---- check version

docker info ----- cehck more information

How to search images
docker search centos
docker search centos7
docker search busybox

How to Download docker image
docker pull centos:centos7 ------ centos7 image pull in local repository

To display the list of locally available images, type;
docker images

Check downloaded images working or not --- To test your new image; type.
docker run centos:centos7 /bin/ping google.com -c 5
docker run centos:centos7 uname -a
id : 45e4a866a944 --- images id

Create web container
docker run -itd --privileged --name apache_web -v /opt/project_name/data:/var/www/html --restart=always -p 8070:80 apache /usr/sbin/init

--name         : conatiner name
-p                 : expose port
apache         : image name
--privileged : provode privileged to /usr/sbin/init
-v                : map volume (Mapped local path to container path)

List docker container
docker ps -a

Checking docker networking
docker network ls
docker network inspect (Network type)
docker network inspect bridge

Check Resource consumption by running container
docker stats

Full container capabilities (–privileged)
docker run -t -i --privileged ubuntu bash

Set working directory (-w)
$ docker run -w /path/to/dir/ -i -t ubuntu pwd

Will give 60% to the db container (614 is 60% of 1024) and 40% to the web container.sudo docker run -c 614 -dit --name db postgres /postgres.sh

sudo docker run -c 410 -dit --name web nginx /nginx.sh


To allow execution only on the first core:
docker run -it --rm --cpuset=0 stress --cpu 1
To allow execution only on the first two cores:

docker run -it --rm --cpuset=0,1 stress --cpu 2
You can of course mix the option --cpuset with -c.

Example: managing the CPU shares of a container
As I mentioned before you can use the -c switch to manage the value of shares assigned to all processes running inside of a Docker container.

Since I have 4 cores on my machine available, I’ll tell stress to use all 4:

$ docker run -it --rm stress --cpu 4
stress: info: [1] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
If we start two containers the same way, both will use around 50% of the CPU. But what happens if we modify the CPU shares for one container?

$ docker run -it --rm -c 512 stress --cpu 4
stress: info: [1] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd

Use a restart policy
To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following:

Flag Description
no Do not automatically restart the container. (the default)
on-failure Restart the container if it exits due to an error, which manifests as a non-zero exit code.
unless-stopped Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted.
always Always restart the container if it stops.

The following example starts a Redis container and configures it to always restart unless it is explicitly stopped or Docker is restarted.

$ docker run -dit --restart unless-stopped redis

Start , Restart and stop container
docker restart 45e4a866a944

Create customize image
docker commit 45e4a866a944 (container ID)

Tag your customize OS
(IMAGE ID) (REPOSITORY)-(OS TAG)
Docker tag abd1af3cbe01 mytest/image:centos07.demo

Remove container
docker rm 45e4a866a944
docker rm 45e4a866a944 -f ( -f - force fully removed)

Check container logs with login
docker logs fb327defcdfa (container ID)

The two containers can be reached by each other
# docker inspect --format '{{.NetworkSettings.IPAddress}}' test1
172.17.0.2

# docker inspect --format '{{.NetworkSettings.IPAddress}}' test2
172.17.0.3

# docker exec test1 bash -c 'ping 172.17.0.3'

Delete docekr images
docker image prune -a
docker images --format 'table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}\t{{.Size}}'
docker image prune -a --force --filter "until=2018-01-10T00:00:00"
docker image prune -a --force --filter "until=240h"

Download base images
docker pull hello-world
docker run hello-world:latest
docker pull docker/whalesay
docker run docker/whalesay cowsay test_massege

Run in background
docker run -d nginx:latest

Check running container : docekr ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

18129edaca30 nginx:latest "nginx -g 'daemon of…" 17 seconds ago Up 14 seconds 80/tcp nervous_bardeen

check insite the container
docker inspect nervous_bardeen
stop containers
docker stop nervous_bardeen
docker stop angry_benz

Tag to the container
docker run -d --name=web_server nginx:latest
docker ps --- check running container and see name will changed

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
548490a909d8 nginx:latest "nginx -g 'daemon of…" 18 seconds ago Up 16 seconds 80/tcp web_server

loging into running container
docker exec -it web_server /bin/bash

Remove all containers
docker ps -a -q ---- this command for container ids

Remove all containers
docker rm `docker ps -a -q`

Remove images forcefully
docker rmi -f nginx:letest

check container ip add
docker inspect web_server |grep -i IPAddr

Assigning random port
docker run -d --name=webtest -P nginx:latest

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

da78d0419c65 nginx:latest "nginx -g 'daemon of…" 5 seconds ago Up 3 seconds 0.0.0.0:32768->80/tcp webtest

docker inspect webtest |grep -i IPAddr
lynx 10.10.10.2
lynx localhost:32768

Check container used port
docker port webtest $CONTAINERPORT

Assign port to container
docker run -d -p 8880:80 --name=web_server nginx:latest

define root directory & port
docker run -d -p 8800:80 --name=web_server1 -v /mnt/data nginx:latest

copy local drive data to root dir, set port, set root dir and tag server
docker run -d -p 60050:80 --name=web -v /var/www/html/marketplace:/var/www/html/marketplace nginx:latest

docker run -d -p 60052:80 --name=myweb -v /var/www/html/marketplace:/var/www/html nginx:stable-alpine

docker run -d -p 60050:80 --name=webserver -v /var/www/html/:/var/www/html camw/centos-nginx-php:latest

docker run --privileged -ti -e "container=docker" -v /sys/fs/cgroup:/sys/fs/cgroup trinitronx/ansible-base:stable-centos7 /usr/sbin/init

After using Docker for a while, you'll have many active (running) and inactive containers on your computer.

To view the active ones, use:
docker ps

To view all containers — active and inactive, pass it the -a switch:
docker ps -a

To view the latest container you created, pass it the -l switch:
docker ps -l

Stopping a running or active container is as simple as typing:
docker stop container-id

Remove multiple images forcefully
docker rmi -f mysql:latest solr:latest

To stop all the running docker containers
docker stop $(docker ps -a -q)

To remove all the stopped docker containers
docker rm $(docker ps -q -f status=exited)
------------------------------------------------------------------
Docker Network Part

create bridge network
docker network create -d bridge --subnet 10.0.0.1/24 my-network
list etwork
docker network ls

check network config
docker network inspect my-network
Install bridge-utils

check bridge network and device driver
cmd : brctl show
ip link show

create container with network
docker run -it -d --name web1 --network my-network -p 8080:80 nginx:stable-alpine bash
docker run -it -d --name web2 --network my-network -p 5000:80 nginx:stable-alpine bash
docker network inspect my-network
-----------------------------------------------------------------------------
The cp command can be used to copy files. One specific file can be copied like:

docker cp foo.txt mycontainer:/foo.txt

docker cp mycontainer:/foo.txt foo.txt

docker cp src/. mycontainer:/target

docker cp mycontainer:/src/. target