i51Bo

                    Incrementally committing changes
                    is not "the docker way"

                    https://stackoverflow.com/questions/19585028
                    /i-lose-my-data-when-the-container-exits

DOCKER FOR UBUNTU
~~~~~~~~~~~~~~~~~

Install:
........

  https://docs.docker.com/install/linux/docker-ce/ubuntu/

  $ sudo apt-get remove docker docker-engine docker.io
  $ sudo apt-get update
  $ sudo apt-get upgrade
  $ echo $(lsb_release -cs)
  bionic
  $ cd ~/Downloads
  $ URL="https://download.docker.com/linux/ubuntu/dists" && \
    URL="$URL/artful/pool/stable/amd64" && \
    URL="$URL/docker-ce_18.03.1~ce-0~ubuntu_amd64.deb" && \
  $ wget -c $URL
  $ sudo dpkg -i docker-ce_18.03.1~ce-0~ubuntu_amd64.deb
  $ sudo apt-get -f install

Terminology:
............

Docker Container -- A running instance of an image is called a container*
docker run -i -t ..
-i : Starts the container in interactive mode.
-t : allocates a pseudo-tty and attaches it to the standard input

------
*) https://www.thegeekdiary.com
         /how-to-list-start-stop-delete-docker-containers/
------

Managing Docker daemon:

  # systemctl enable docker
  # systemctl start docker
  # systemctl stop docker

First test

  $ sudo docker run hello-world

Post-install

  $ sudo usermod -aG docker $USER

Reboot..

How Docker Works?
.................

It helps a lot to be familiar with OOP concept of inheritance and layered Data approach. Read this:

  http://merrigrove.blogspot.com.ee/2015/10
  /visualizing-docker-containers-and-images.html

We'll use following approach:

  0. Docker CLI looks like

    $ docker ..

  1. Find out object's ID

    $ docker ps -a
    $ docker image ls

  2. Deal with object using it's ID

    $ docker start <CONTAINER_ID>
    $ docker attach <CONTAINER_ID>.. exit
    $ docker commit <CONTAINER_ID>
    $ docker export CONTAINER_ID > my_container.tar
    $ cat my_container.tar | docker import -
    $ docker rm <CONTAINER_ID>

    $ docker save -o <save image to path> <IMAGE_ID>
    $ docker load -i <path to image tar file>
    $ docker rmi <IMAGE_ID>

Browsing Docker Hub
...................

  https://hub.docker.com/explore/
  https://docs.docker.com/engine/reference/commandline/pull/

  Search: ubuntu
  Search: hello-world

Fedora
......

  Search: fedora (tags latest, rawhide,..)

  $ docker pull fedora:rawhide

  $ docker run -it fedora:latest bash
  [root@5823c5fc01bc /]# cat /etc/fedora-release
  Fedora release 27 (Twenty Seven)
  [root@5823c5fc01bc /]# exit
  $ docker run -it fedora:rawhide bash
  [root@a07e6349f6b5 /]# cat /etc/fedora-release
  Fedora release 29 (Rawhide)
  [root@a07e6349f6b5 /]# exit

Fedora tips:
............

  # dnf provides ip  # Which package provides file named ip?
  iproute-4.15.0-1.fc28.x86_64
  # dnf install iproute
  # rpm -ql iproute | grep "/ip$"
  /usr/sbin/ip
  # rpm -qf /usr/sbin/ip
  iproute-4.15.0-1.fc28.x86_64

Testing inside container:

  ip:   172.17.0.2/16
  mask: 172.17.255.255
  nic:  eth0
  gw:   172.17.0.1
  dns:  8.8.8.8

  # free
         total    used    free     shared  buff/cache  available
  Mem:   4039180  691072  2030020  13376   1318088     3075696
  Swap:   969960       0   969960

  # df
  Filesystem     1K-blocks    Used Available Use% Mounted on
  overlay         20509264 7727960  11716448  40% /
  tmpfs              65536       0     65536   0% /dev
  tmpfs            2019588       0   2019588   0% /sys/fs/cgroup
  /dev/sda1       20509264 7727960  11716448  40% /etc/hosts

More commands
.............

  https://docs.docker.com/get-started/#recap-and-cheat-sheet

  ## List Docker CLI commands
  docker
  docker container --help

  ## Display Docker version and info
  $ docker --version
  $ docker version
  $ docker info

  ## Execute Docker image
  $ docker run hello-world
  $ docker run -it ubuntu bash

  ## List Docker images
  $ docker image ls
  REPOSITORY     TAG       IMAGE ID        CREATED        SIZE
  ubuntu         latest    452a96d81c30    3 days ago     79.6MB
  hello-world    latest    e38bc07ac18e    2 weeks ago    1.85kB

  ## List Docker containers (running, all, all in quiet mode)
  $ docker container ls --all
  CONTAINER ID  IMAGE        COMMAND   CREATED         STATUS                    PORTS  NAMES
  a08c033f67fb  ubuntu       "bash"    5 minutes ago   Exited (0) 2 minutes ago         pedantic_ritchie
  7425fa6383cf  hello-world  "/hello"  6 minutes ago   Exited (0) 6 minutes ago         zen_elion
  72822f10a8a0  hello-world  "/hello"  12 minutes ago  Exited (0) 12 minutes ago        focused_babbage
  a8a97a2ea2be  hello-world  "/hello"  39 minutes ago  Exited (0) 39 minutes ago        thirsty_montalcini
  $ docker container ls -aq
  a08c033f67fb
  7425fa6383cf
  72822f10a8a0
  a8a97a2ea2be

  ## Remove Docker containers*

  DESTROY EVERYTHING: $ docker system prune -a

  1. list containers:  docker ps -qa
  2. stop container:   docker stop a08c033f67fb
  3. remove container: docker rm a08c033f67fb
  4. list images:      docker images -a
  5. remove image:     docker rmi 452a96d81c30

  Images

  List:              $ docker images -a
  Remove:            $ docker rmi Image Image
  Remove all images: $ docker rmi $(docker images -a -q)

------
*) https://www.digitalocean.com/community/tutorials
     /how-to-remove-docker-images-containers-and-volumes
------

Attach and detach running container:
....................................

  $ docker run -dti fedora:rawhide bash
  $ docker ps -a
  CONTAINER_ID  STATUS
  dc85e320cc5b  up 11 seconds
  $ docker exec -ti dc85e320cc5b bash
  [root@dc85e320cc5b /]# ctrl+p ctrl+q
  read escape sequence
  $ docker ps -a
  CONTAINER_ID  STATUS
  dc85e320cc5b  up 4 minutes

I lose my data when the container exits
.......................................

  https://stackoverflow.com/questions/19585028
    /i-lose-my-data-when-the-container-exits

When you use docker run to start a container, it actually creates a new container based on the image you have specified.

  $ docker run -ti fedora:rawhide bash
  # dnf update
  # exit
  $ docker ps -a
  0c216093306e dnf update successful
  $ docker run -ti fedora:rawhide bash
  # exit
  $ docker ps -a
  0c216093306e dnf update successful
  20cd9493d919 new container created from image

  docker start 0c216093306e   # restart it in the background
  docker attach 0c216093306e  # reattach the terminal & stdin

  File             Provides
  ping             iputils
  nslookup         bind-utils
  ip               iproute
  route, ifconfig  net-tools

Changing default DNS for the containers:
........................................

  https://opsech.io/posts/2017/May/23
    /docker-dns-with-firewalld-on-fedora.html

  ### please create this if it does not exist: ###

  # cat /etc/docker/daemon.json
  {
      "dns": [
          "192.168.1.1"
      ]
  }

  # systemctl restart docker.service

Container vs Host (with Docker daemon stopped).
...............................................

Compare performance for the following tests:

  $ time git clone https://github.com/jpoirier/picoc
  $ time tar cv allikas | bzip2 >  tulemus
  $ time tar xf
  $ time make

          ---===<DAY III>===---

Simple exercises: Fedora <-> Ubuntu
...................................

1. FTP

  https://www.liquidweb.com/kb
    /how-to-install-and-configure-vsftpd-on-centos-7/
  https://linuxconfig.org
   /how-to-install-ftp-client-for-ubuntu-18-04-bionic-beaver-linux

Fedora container

  $ docker -a
  CONTAINER ID        IMAGE..
  3efdc1ea8e8a        ubuntu              "bash"seconds ago..
  0c216093306e        fedora:rawhide      "bash"minutes ago..
  $ docker start 0c216093306e
  $ docker attach 0c216093306e
  [root@0c216093306e /]# cat /etc/vsftpd/vsftpd.conf  | tail -n 8
  guest_enable=NO
  anonymous_enable=NO
  local_enable=YES
  write_enable=YES
  chroot_local_user=YES
  allow_writeable_chroot=YES
  local_root=/home/virtu
  [root@0c216093306e /]# vsftpd
  [root@0c216093306e /]# ps -A
    PID TTY          TIME CMD
      1 pts/0    00:00:00 bash
     15 ?        00:00:00 vsftpd
     16 pts/0    00:00:00 ps

Ubuntu container

  $ docker start 3efdc1ea8e8a
  $ docker attach 3efdc1ea8e8a
  root@3efdc1ea8e8a:/# history
      1  adduser virtu
      2  su virtu
      3  apt-get update
      4  apt-get install ftp
      5  ftp 172.17.0.2

2. APACHE

  https://access.redhat.com/articles/1328953
  https://linoxide.com/linux-how-to
    /configure-apache-containers-docker-fedora-22/

Fedora container

  # dnf install httpd
  # httpd -D FOREGROUND &

ubuntu container

  # elinks 172.17.0.2

3. SSH

Fedora Container

  https://gist.github.com/pokle/10808069
  https://stackoverflow.com/questions/40801417
    /installing-ssh-in-the-docker-containers

  # cat /etc/ssh/sshd_config | tail
  ..
  AllowUsers virtu

  # adduser virtu
  # passwd
  # dnf install -y openssh-server openssh-clients
  # ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
  # ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
  # ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
  # ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
  # /usr/sbin/sshd -D &

Ubuntu Container

  ssh -l virtu 172.17.0.2
  bash: ssh: command not found
  root@3efdc1ea8e8a:/# apt-get install openssh-client

What package provides command?
..............................

Fedora:

  # dnf provides ps                 ## 1. Locate package
  procps-ng-3.3.14-1.fc29.x86_64
  # dnf install procps-ng           ## 2. Install package
  # ps -A                           ## 3. Use command from package

Ubuntu

  # sudo apt-get install apt-file
  # sudo apt-file update
  # apt-file search ping | grep '/ping$'
  ..
  iputils-ping: /bin/ping
  # apt-get install iputils-ping
  # ping google.ee

Backed up to:

  BIONIC-20GB-i51.7z  7043072KB

Annex A. picoc log under WSL Ubuntu 16.04:
..........................................

  $ sudo apt-get install libreadline-dev subversion xzip
  $ time git clone https://github.com/jpoirier/picoc
  Cloning into 'picoc'...
  remote: Counting objects: 4072, done.
  remote: Total 4072 (delta 0), reused 0 (delta 0), pack-reused 4072
  Receiving objects: 100% (4072/4072), 1.51 MiB | 940.00 KiB/s, done.
  Resolving deltas: 100% (2945/2945), done.
  Checking connectivity... done.

  real    0m3.197s
  user    0m0.250s
  sys     0m0.938s

  $ time tar cJf picoc.tar.xz picoc

  real    0m1.745s
  user    0m1.688s
  sys     0m0.234s

  $ rm -rf picoc && time tar xf picoc.tar.xz

  real    0m0.344s
  user    0m0.156s
  sys     0m0.281s

  $ cd picoc && time make
  ..

  $ cd .. && rm -rf picoc && time tar xf picoc.tar.xz

Annex B. Cleaning Docker machine
................................

  $ df | grep sda1
  /dev/sda1       20509264   8053480  11390928  42% /
  $ docker ps -aq
  20cd9493d919
  0c216093306e
  0c853a60e392
  ee678bc6763b
  b7e0b3236e53
  dc85e320cc5b
  $ docker rm 20cd9493d919 0c853a60e392 ee678bc6763b b7e0b3236e53
  $ docker rm dc85e320cc5b
  20cd9493d919
  0c853a60e392
  ee678bc6763b
  b7e0b3236e53
  dc85e320cc5b
  $ docker ps -aq
  0c216093306e
  virtu@UBOX:~$ df | grep sda1
  /dev/sda1       20509264   7727164  11717244  40% /

