Docker

Udemy Course Material

$ git clone https://github.com/BretFisher/udemy-docker-mastery.git

// later on to have an update: go in the project repo
$ git pull

Community Links:

Setup Docker

Setup Docker on Rasperry Pi

To install docker on Raspberry pi: https://phoenixnap.com/kb/docker-on-raspberry-pi

!!! Docker image on compatible with Raspberry pi4 must be ARM64 in the Docker hub site (check on left side the parameters arm64)

Opposed to most other Linux distributions, Raspberry Pi is based on ARM architecture. Hence, not all Docker images will work on your Raspberry Pi.

In fact, all Docker images that are not specifically designed for ARM devices will not work. Running docker-apps designed for x86/x64 and i386 architecture on your Raspberry Pi will return an error.

$ sudo apt-get update && sudo apt-get upgrade
$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
$ sudo usermod -aG docker Pi

// Verify ok
$ docker info

// Run a first app:
$ docker run hello-world
// Run on the pi CLI
$ docker run -d -p 80:80 hypriot/rpi-busybox-httpd
// To upgrade Docker on Raspberry
$ sudo apt-get upgrade
  • Install docker compose on pi

$ sudo pip3 install docker-compose
// test it
$ docker-compose --version

Setup Docker on Linux

Docker was made to run on Linux native (unbuntu)

// See if docker is installed
$ docker --version

// if old version of docker, deinstall it
$ sudo apt-get remove docker docker-engine docker.io containerd runc

// Update the apt package index and install packages to allow apt to use a repository over HTTPS:
$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
    
// Add Docker’s official GPG key: 
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

// Verify that you now have the key with the fingerprint ending with 8 caract
$ sudo apt-key fingerprint 0EBFCD88


// set up the stable repository
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

// Update the apt package index, and install the latest version of Docker Engine and containerd
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

// Verify that Docker Engine is installed correctly by running the hello-world image.
$ sudo docker run hello-world

Instruction to install docker on Linux: https://docs.docker.com/engine/install/ubuntu/

! if you are on Linux REHL (Redhat enterprise edition), it supports only the docker enterprise version

But Linux Centos Version support Docker CE

There is an all in one script to install docker from script doing all, check OS type

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sh get-docker.sh

// if you need to add the sudo write each time, add your account to the docker group
$ sudo usermod -aG docker "UserName"

!! Docker-machine and docker-compose are not installed by default with the command here above (which is the case with MAC, so you will need to install them manually

// Install docker-machine - https://docs.docker.com/machine/

$ base=https://github.com/docker/machine/releases/download/v0.16.0 &&
  curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
  sudo mv /tmp/docker-machine /usr/local/bin/docker-machine &&
  chmod +x /usr/local/bin/docker-machine
  
  // verify install is ok
$ docker-machine version
  
// Install docker-compose - https://docs.docker.com/compose/install/
  // but there is an easy way: https://github.com/docker/compose
  
  // first check if you have and older version of compose and uninstall it
  // https://phoenixnap.com/kb/install-docker-compose-on-ubuntu-20-04

$ sudo rm /usr/local/bin/docker-compose
$ sudo apt remove docker-compose
$ sudo apt autoremove

// Install it
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
$ docker-compose --version


Setup docker on MAC

Setup on Mac: https://www.docker.com/products/docker-desktop

You can also download the edge version on : https://docs.docker.com/docker-for-mac/edge-release-notes/

On Mac docker app, they support both stable and edge version, via settings it is easy to jump from one to another

Docker doesn't live natively on Mac Kernel, so we need to have a Linux VM running on MAC.

A nice bash terminal for Mac is: https://iterm2.com

Install docker bash auto completion: https://docs.docker.com/docker-for-mac/#install-shell-completion

// copy this in terminal

etc=/Applications/Docker.app/Contents/Resources/etc
ln -s $etc/docker.bash-completion $(brew --prefix)/etc/bash_completion.d/docker
ln -s $etc/docker-compose.bash-completion $(brew --prefix)/etc/bash_completion.d/docker-compose
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

You can make nice color on tuned termina: https://www.bretfisher.com/shell/

Docker Theory Concepts

Docker Software type

Docker CE = Community Edition = free version

Docker EE = Enterprise Edition = paid, support included and ony available on certified machine

3 different types of installation:

  • Direct

  • Win/Mac => not supported so there is a some VM install where docker run inside of it = legacy docker toolbox (you have a GUI, app)

  • Cloud: Azure, AWS, Google

Version available:

  • Edge = beta version, once every month

  • Stable, once per quarter

Images vs container:

  • Image: application we want to run -> so just code

  • container: instance of the image running as a process

Registry = docker hub = repo of all the docker prebuild docker images (like the repo on github) -> https://hub.docker.com

docker container run "ContainerName:Version" --> what behind the wood

  • look locally in image cash if there is a container with that name

  • if no image locally, then go automatically to Docker Hub

  • If you don't specify a version, it takes as default, the last one: nginx:latest

  • Then create a new container based on that image

  • Prepare start:

    • give container a virtual IP on private network inside the docker engine

    • Then open port mapping specified in the --publish options. If that option is not defined, no port will be opened between hosting PC and docker engine

  • Then Start the container using the commands defined in the DockerFile

// Syntax
$ docker container run --publish 8080:80 --name test -d nginx:1.11 nginx -T
// at the end after the version those can be change CMD run on start

Docker Commands

COmmands to know: https://towardsdatascience.com/15-docker-commands-you-should-know-970ea5203421

// To launch a docker image and interact with it on terminal
$ docker run -ti alpine /bin/sh
// - t for interacting with image in terminal and -i for interactive session
// /bin/sh is beacause bash was not present on alpine initially but can work now without it

Learning Docker by steps

Check Setup

$ docker version
// you need to see client and server side running.
// check that docker client is able talking to your docker server
$ docker info
// can see the images install, running
$ docker (press enter)
// see the list of all commands

$ docker "management command" -> use that new style
// docker <command> <subcommand> (options)
// old way docker <command> (options)

Start a NGINX Web Server

// Start a new container on terminal CLI
$ docker container run --publish 80:80 nginx

// then go to browser and enter: localhost you should see page if setup succeed
// publish option means: you can specify after the port mapping PC -> Docker container internal process
// to kill it CTRL + C

// Start a new container on detached mode in the background
$ docker container run --publish 80:80 --detach nginx
// we get back the unique id of running container: 7886d73b04a7d45bf637cd3ab70c85ff6a7eaf53bf38060d8e5bda7148a1ae61

// List our container
$ docker container ls

// Stop the container
// docker container stop "a few first Digit of the id is ennough"
$ docker container stop 788

$ docker container ls -a
// we have the list of all container that have been created
// the name field is auto generated by docker if not specified by us
// names come from a list of known hacker of scientist, so this is a funny part

// Start a container with a specified name
$ docker container run --publish 80:80 --detach --name HelooName nginx

// To see the log of the detach container running
$ docker container logs HelooName
// full dump

// Display the processes running inside a container
$ docker container top HelooName

// Help command
$ docker container --help

// Stop and remove container
$ docker container rm -f "list of containerIDFirstLetters separated by space"
$ docker container ls -a (to verify we don't have any container anymore)

Last updated