# DevOps/Docker

Docker is a software available since 2013 that among other features performs operating-system-level virtualization also known as containerization

## Features

Docker offer the following features:
* Package an application and its dependencies in a virtual container that can run on any Linux server (docker images)
* Pull or push docker images to a container repository/container registry (docker pull, docker push). By default docker hub but you can specify any other registry.

## Installing Docker

You can read official Docker documentation about installing Docker on your system: https://docs.docker.com/install/.
On macOS you can follow official documentation https://docs.docker.com/docker-for-mac/install/ which requires to create and account to download installer. You can also try: brew cask install docker or try to follow https://pilsniak.com/how-to-install-docker-on-mac-os-using-brew/ instructions:
    brew install docker docker-compose docker-machine(to install docker and docker-compose)
    brew cask install virtualbox
    docker-machine create --driver virtualbox default
    docker-machine ls
    docker-machine env default
    eval $(docker-machine env default)
    docker run hello-world (will pull hello-world image and run it on docker)
On Ubuntu: snap install docker

### Binaries

Following binaries will be installed: docker-init, docker-proxy, docker and dockerd

### Verifying Docker installation

Once installed the Docker daemon, called "dockerd" should be running. You can also run docker run hello-world to verify docker correct installation.

### Configuration files

* Ubuntu: /etc/docker/daemon.json

## Docker Releases (Docker Engine release notes)

* https://docs.docker.com/engine/release-notes/
* https://github.com/docker/docker-ce/releases
Use: docker version to check your version. You can download Docker CE source code from GitHub.

## Docker Command Line

You can read official docker command line documentation in https://docs.docker.com/engine/reference/commandline/docker/. Before being able to run docker commands you will have to install Docker on your machine.
Some typical task using containers:

### Docker verification commands

* Verify correct installation: docker run hello-world
You will see some message similar to this one:

### Docker image and container creation

* Create a new docker image base on alpine linux distribution and login into the new container: docker run -it alpine sh
* Create a new docker image: docker build
** Create a new docker image by creating a new text file with instructions, usually called Dockerfile: docker build -f /path/to/a/Dockerfile .
* Create a container: docker create
* Generate a mediawiki:1.27 image: docker build --tag mediawiki:1.27 . (you can use -t or --tag)
* Execute or run a container based on mediawiki:1.27 image: docker run --name wikiFGA -p 0.0.0.0:9090:80 -d mediawiki:1.27

### Docker Container Operation

* Start an existing container: docker start CONTAINER_ID OR CONTAINER_NAME. See also docker run and differences.
* Stop an running container: docker stop container_id
* Show only running containers:
** docker container ls or docker ps. (docker container ls) introduced in 2017 in Docker 1.13 (There is no difference between both commands)
* Show containers (running or not):
** docker ps -a : https://docs.docker.com/engine/reference/commandline/ps/
** docker container ls -a . See also docker-compose ps
* Login/Connect into a running container:
** docker exec -it <my_container_name> bash
** docker exec -it <my_container_name> sh
* Start stopped containers: docker start $(docker ps -a -q -f status=exited)
* Restart all containers: docker restart $(docker ps -aq) previously docker restart $(docker ps -q)-a all -q quiet
* Start configuration behavior and restart policy
See also: docker compose and docker stack

### Docker images management

* List created images: docker images, docker image ls (both commands seems to perform the same action, note it is image instead of images)
    Images are stored in docker info | grep "Docker Root Dir"
* docker image rm. See also: docker rm CONTAINER

### Docker Network command line commands

* docker network ls
* docker network inspect
* Deprecated: docker --link

### Docker resource limitation

* Limit container CPU usage to 0.5 cpus: docker run -it --cpus=".5" docker_image /bin/bash
* Limit memory requires your kernel to have functionality activated, you can check it executingdocker info. See https://docs.docker.com/install/linux/linux-postinstall/ and docker-compose resource limitation example: DevOps/Docker/docker compose/Version 3 resouces

### Docker: Working with remote repositories/registries

You will be using mainly the following commands docker login, docker logout, docker pull and docker push. Docker registry allow to configure notifications.. Docker has a public repository called Docker Hub and cloud providers offer repositories services such as AWS Elastic Container Registry (ECR).

### Docker Information

* docker version https://docs.docker.com/engine/reference/commandline/version/
* docker info https://docs.docker.com/engine/reference/commandline/info/
* docker stats
* docker system info https://docs.docker.com/v17.12/edge/engine/reference/commandline/system_info/
* docker system events https://docs.docker.com/v17.12/edge/engine/reference/commandline/system_events/
* docker system df
* docker top [CONTAINER_NAME or CONTAINER_ID] https://docs.docker.com/engine/reference/commandline/top/
* docker inspect CONTAINER_ID|IMAGE_ID https://docs.docker.com/engine/reference/commandline/inspect/ also available for: container, image, volume, network, node, service, or task
* docker images, docker images -q
* docker image inspect
* docker image ls or docker image ls --no-trunc

### Docker Operation

* docker run IMAGE|IMAGE_ID : https://docs.docker.com/engine/reference/run/
* docker start CONTAINER_ID|CONTAINER_NAME: https://docs.docker.com/engine/reference/commandline/start/
* docker stop CONTAINER_ID|CONTAINER_NAME: https://docs.docker.com/engine/reference/commandline/stop/
* docker restart CONTAINER_ID|CONTAINER_NAME: https://docs.docker.com/engine/reference/commandline/restart/

### Docker/cgroup Performance information

* systemd-cgtop

### Docker Swarm

* docker swarm
* docker nodes

### Maintenance

* docker system prune -a remove unused and dangling images. Therefore any images being used in a container, whether they have been exited or currently running, will NOT be affected.

### Volumes

* docker volume: https://docs.docker.com/engine/reference/commandline/volume/
* docker volume ls: https://docs.docker.com/engine/reference/commandline/volume_ls/
* docker inspect -f 'Template:.Mounts' CONTAINER_NAME
* docker volumes prune

## Docker logging

Docker support logging to format or different platforms, such as, json-file, syslog, journald, gelf, fluentd, awslogs, splunk, etwlogs, gcplogs and logentries.
* Configuration is done at docker container start time with command docker run -it --log-drive LOG_METHOD CONTAINER_ID
* Check method configured in a running container:
** docker inspect -f 'Template:.HostConfig.LogConfig.Type' CONTAINER_ID
** docker inspect -f 'Template:.HostConfig.LogConfig' CONTAINER_ID
* View logs
    docker logs CONTAINER_NAME_OR_ID (Docker Community Engine only support: local, json-file and journald)
    docker logs CONTAINER_NAME_OR_ID 2>&1 | grep "STRING_TO_SEARCH" (You will need to redirect outputs to be able to grep output)
    See https://docs.docker.com/config/containers/logging/ for more information

## Misc

* docker tag

## Activities

* Read docker blog: https://blog.docker.com/
* Read Docker Engine release notes (2017) and Docker CE or Docker EE releases.
* Understand the difference between an image and a container, Docker Images vs. Containers: https://stackoverflow.com/a/26960888
* Understand the difference between docker start and docker run: https://stackoverflow.com/questions/34782678/difference-between-running-and-starting-a-docker-container. See also runC
* Read Stackoverflow questions related to docker: https://stackoverflow.com/questions/tagged/docker?tab=Votes