Docker Overview
Docker
  The word "Docker" refers to several thing.
   ●    The IT software "Docker” is containerization technology.
   ●    The open source Docker community works to improve these technologies to
        benefit all users—freely.
   ●    The company, Docker Inc., builds on the work of the Docker community,
        makes it more secure, and shares those advancements back to the greater
        community.
Docker
https://www.redhat.com/es/topics/containers/what-is-docker
Containers
Containers
https://www.redhat.com/es/topics/containers
What is Docker?
    ●   Docker is a platform designed to make it easier to create, deploy and run
        applications by using containers.
    ●   Docker provides the ability to package and run an application in an isolated
        environment called a container.
    ●   The isolation and security allow you to run many containers simultaneously
        on a given host
    ●   Containers allow a developer to package up an application with all of the
        parts it needs, such as libraries and other dependencies and ship it all out as
        one package.
What is Docker?
Docker works with something called “Linux containers”, these are a set of technologies that
together form (forman) a container (Docker), this set of technologies are called:
  ●     Namespaces: Permite a la aplicación que corre en un contenedor de Docker tener
        una vista de los recursos del sistema operativo.
  ●     Cgroups: Permite limitar y medir los recursos que se encuentran disponibles en el
        sistema operativo.
  ●     Chroot: Permite tener en el contenedor una vista de un sistema “falso” para el mismo,
        es decir, crea su propio entorno de ejecución con su propio root y home.
Thanks to the container, the developer can rest assured (puede estar seguro) that the
application will run on any other Linux machine regardless (sin importar) of any customized
settings that machine might have that could differ from the machine used for writing and
testing the code.
Containers, or otherwise known as (virtualización a nivel de sistema operativo)
operating-system-level virtualization , are a lightweight (ligero) approach to virtualization that
only provides the (minimo necesario) bare minimum that an application requires to run
https://opensource.com/resources/what-docker
https://docs.docker.com/engine/docker-overview/
Containers & Virtual Machines
Containers & Virtual Machines
A container runs natively on Linux and shares the kernel of the host machine with
other containers. It runs a discrete process, taking no more memory than any other
executable, making it lightweight.
By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with
virtual access to host resources through a hypervisor. In general, VMs incur a lot of
overhead beyond what is being consumed by your application logic.
Una hipervisor (hypervisor) o monitor de máquina virtual (virtual machine monitor) es
una plataforma que permite aplicar diversas técnicas de control de virtualización para
utilizar, al mismo tiempo, diferentes sistemas operativos en una misma computadora
https://docs.docker.com/get-started/
Docker Engine
Docker Engine
Docker is a Client - Server Application (un cliente realiza peticiones a otro programa, el
servidor, quien le da respuesta)
  A server runs one or more server programs, which share their resources with clients.
  Clients request content or service from a server.
 - Docker CLI - cliente   (command line interface, main way to interact with Docker)
 - Rest API               (API is the means of communication between client and docker
server)
 - Docker Daemon - Server (presta el servicio de docker)
When writing docker in a terminal we are communicating through the API to docker daemon
The daemon creates and manages Docker objects, such as images, containers, networks, and
volumes
Docker Image
   ●   Docker image is a template with instructions for creating containers.
   ●   It is a package that includes everything needed to run an application
       (the code, libraries, environment variables and configuration files).
   ●   The official Docker Images are stored in Docker Hub (https://hub.docker.com/)
   ●   Docker can build images automatically by reading the instructions from a
       Dockerfile
   ●   The layers of the image are created through a file called Dockerfile.
Docker Image
Docker Hub is a public registry that anyone can use, and Docker is configured to look for
images on Docker Hub by default.
The number of layers that can contain an Image is n-layers.
https://docs.docker.com/engine/docker-overview/
https://docs.docker.com/get-started/
Dockerfile
   ●   Dockerfile use a simple syntax for defining the steps needed to create the
       image and run it.
   ●   Each instruction in a Dockerfile creates a layer in the image.
   ●   When you change the Dockerfile and rebuild the image, only those layers
       which have changed are rebuilt.
   ●   The instructions are not case-sensitive
                                                  # OS base
                                                  FROM centOS
          # comment
          INSTRUCTION arguments
                                                  # OS base
                                                  from centOS
Dockerfile
The convention is to be UPPERCASE to distinguish them from arguments more easily
https://docs.docker.com/engine/reference/builder/
                Dockerfile - container                                     Image
                                                                      Base Image
                      FROM centos:7
                      RUN yum -y install httpd                    Server HTTP Apache                RO
                                                                      installation                LAYERS
                      CMD
                      ["apachectl","-DFOREGROUND"]
                                  Dockerfile                        Running Apache as
                                                                      Foreground
Container Creation
                                                                      “Cool_gates”                  RW
                     $ docker run apache-centos:apache-cmd
                                                                        Container                  LAYER
              Dockerfile - container
              CMD : Start the service in foreground, if the cmd process dies the container dies
              Apache HTTPD is an HTTP server daemon produced by the Apache Foundation. It is a piece
              of software that listens for network requests (which are expressed using the Hypertext
              Transfer Protocol) and responds to them.
              https://geeks.ms/etomas/2019/03/05/curiosidad-esos-nombres-de-contenedores-en-docker/
              https://github.com/docker/engine/blob/master/pkg/namesgenerator/names-generator.go
              https://docs.docker.com/get-started/
Pull Images
       To download an image you use the following command:
                      docker pull <image>:<version>
            If the <version> is omitted Docker use the default latest tag.
                  Tag is a label that says the image version <version>.
                  docker pull centos
                  docker pull centos:6.7
                                                                             10
Pull Images
Images have tags that are using to versioning the images.
How Can I know what is the name of the image that I need?
  -   Google : docker centos
Create Images
     To create Docker images you only need to type:
           docker build -t <image_name>:<tag> <dockerfile_path>
           ➤    ‘-t ’ Name and optionally a tag in the ‘image_name:tag’ format.
           ➤    dockerfile_path by default is ‘CURRENT_PATH/Dockerfile’.
                If you want to specify a different dockerfile anywhere use
                 ‘-f <PATH/Dockerfilename>’.
                      docker build -t centos:2 -f Dockerfile.debug .
                                                                                  11
Create Images
FROM centos:7
RUN yum -y install httpd
CMD ["apachectl","-DFOREGROUND"]
$ docker build -t apache-centos .
Layers’ Image
   See the layouts of an image
         docker history -H <image_name>:<tag> --no-trunc
     $ docker history -H apache-centos:1.0
     $ docker history -H apache-centos:1.0 --no-trunc
                                                           12
Layers’ Image
       Use of the <history> command:
$ docker history apache-centos:1.0
IMAGE                 CREATED            CREATED BY                                  LAYERS
a5344bf00ac6   19   seconds ago      /bin/sh -c #(nop) CMD ["apachectl" "-DFOREG…     CMD
9e688069c3d1    5   weeks ago        /bin/sh -c yum install httpd -y                 Apache
67fa590cfc1c    8   weeks ago        /bin/sh -c #(nop) CMD ["/bin/bash"]
<missing>       8   weeks ago        /bin/sh -c #(nop) LABEL org.label-schema.sc…    CentOS
<missing>       8   weeks ago        /bin/sh -c #(nop) ADD file:4e7247c06de9ad117…
                                                                                              13
Create Containers
 Docker container creation:
                    docker run -d --name <container_name> -p
                  <local_port>:<container_port> <docker_image>
             ➤    ‘-d’ Run container in background and returns the
                  container_id.
             ➤    ‘--name’ Optional parameter. If it is not specified Docker will
                  create a random container_name .
             ➤    ‘-p’ Optional parameter. Expose ports
                                                                                   14
Create Containers
       docker run --name apache-centos -p 81:80 apache-centos
       localhost:81
Isolating Docker Containers
                       Output Of: docker ps -a
                                                                                                15
Create Containers
To understand the isolation we are going to do an example:
        docker   run   alpine ls -l
        docker   run   alpine /bin/sh
        docker   run   alpine echo “hello world!”
        docker   run   hello-world
Even though each docker container run command used the same alpine image, each
execution was a separate, isolated container.
Each container has a separate filesystem and runs in a different namespace; by default a
container has no way of interacting with other containers, even those from the same image
Docker users take advantage of this feature not only for security, but to test the effects of
making application changes.
Isolation allows users to quickly create separate, isolated test copies of an application or
service and have them run side-by-side (lado a lado) without interfering with one another.
https://training.play-with-docker.com/ops-s1-hello/                        (terminal online)
Docker Container Instances
                                                                                          16
Docker Container Instances
Example of how to the isolation works. If I create a file ‘hello.txt’ this file is only
belongs to the container where I run the instruction.
        docker run -it alpine /bin/sh
        # echo “hello world” > hello.txt
        # ls ; exit
https://training.play-with-docker.com/ops-s1-hello/
Docker Exec
              17
1.
Basic
Command for
Docker
Basic Commands       ▸   Download CentOS image
 ●   docker pull
                           docker pull centos
 ●   docker images
 ●   docker rmi
                     ▸   Download CentOS image version 6.7
                           docker pull centos:6.7
                     ▸   List the images downloaded
                           docker images
                     ▸   List only the CentOS images
                           docker images | grep centos
                     ▸   Remove the CentOS:6.7 image
                           docker rmi <Image ID> | <Repository:Tag>
                           docker rmi centos:6.7
                           docker rmi 9f1de3c6ad53
                                                                      19
Basic Commands             ▸   Dockerfile
 ●   docker build
 ●   docker build --help       FROM centos
                               RUN yum install httpd -y
                               CMD apachectl -DFOREGROUND
                           ▸   Create image
                                 $ docker build -t apache-centos .
                                     Successfully tagged apache-centos:latest
                           ▸   Create image with a different tag
                                 $ docker build -t apache-centos:1.0 .
                           ▸   List only the CentOS images
                                 $ docker images | grep apache-centos
                                                                                20
Basic Commands           ▸   Create container
 ●   docker run
                               $ docker run --name apache-centos -p
 ●   docker run --help       81:80 apache-centos
 ●   docker ps
                         ▸   List containers
                               $ docker ps        (running)
                               $ docker ps -a     (all)
                                                                      21
EXAMPLES OF
USE THE
DOCKERFILE
INSTRUCTIONS
Dockerfile Instructions
       FROM:      Specify the OS or Image base
       RUN:       Instructions that can be executed in a terminal such as add users,
                  create directories, install programs, etc. )
       COPY/ADD: Copy files from localhost to the image
       ARG:       Known as ‘build-time variables’, available until image creation
       ENV:       Define environment variables
       WORKDIR: Path from where the RUN instructions are executed (cd /path/)
       EXPOSE:    Expose ports of the container
       LABEL:     Give Metadata
       USER:      Change the user that execute the sentences
       VOLUMEN: Share directories between Docker and our machine
       CMD:       Process in FOREGROUND that keeps life the container
                                                                                       23
Dockerfile is a file where you define the steps to configure an image.
Basic Commands                            COPY <local_folder> <destination_path>
 ●
 ●
      FROM
      RUN
                                      ▸   Relative path
 ●    COPY
 ●    CMD                                 FROM centos
                                          RUN yum install httpd -y
                                          COPY sb-admin /var/www/html
                                          CMD apachectl -DFOREGROUND
                                                docker build -t apache-centos .
                                                docker run -d -p 81:80 apache-centos
                                                localhost:81
                                      ▸   Absolute path (windows error)
                                          FROM centos
                                          RUN yum install httpd -y
  To avoid this error, the ideal is       COPY /C/docker/apache-php/sb-admin /var/www/html
                                          CMD apachectl -DFOREGROUND
  to have the Dockerfile, folders
  and files that you want to                     docker build -t apache-centos .
  copy in the same directory                     COPY failed: stat
                                          /var/lib/docker/tmp/docker-builder107384051/c/docker
                                          /apache-php/sb-admin: no such file or directory
                                                                                                 24
 If you use COPY /home/yourname/file1, Docker build interprets it as ${docker build working
 directory}/home/yourname/file1, if no file with same name here, no file or directory error is
 thrown.
 URL to download examples from bootstrap: https://startbootstrap.com/themes/
Basic Commands                 ENV <variable> <value>
 ●   FROM
 ●   RUN                       FROM centos
 ●   COPY                      RUN yum install httpd -y
 ●   ENV                       COPY sb-admin /var/www/html
 ●   CMD                       ENV foo “hello world!”
                               RUN echo “$foo” > /var/www/html/test.html
                               CMD apachectl -DFOREGROUND
                                     docker build -t apache-centos .
                                     docker run -d -p 81:80 apache-centos
                                     http://localhost:81/test.html
                                                                            25
 http://www.sigt.net/general/el-significado-de-foo-bar-y-foobar.html
Basic Commands   WORKDIR <path>   (cd /path)
 ●   FROM
 ●   RUN         FROM centos
 ●   COPY        RUN yum install httpd -y
 ●   ENV         WORKDIR /var/www/
 ●   WORKDIR     COPY sb-admin html/
 ●   CMD         ENV foo “hello world!”
                 RUN echo “$foo” > html/test.html
                 CMD apachectl -DFOREGROUND
                       docker build -t apache-centos .
                       docker run -d -p 81:80 apache-centos
                       http://localhost:81/test.html
                                                              26
Basic Commands   EXPOSE <port>
 ●   FROM
 ●   RUN         FROM centos
 ●   COPY        RUN yum install httpd -y
 ●   EXPOSE      COPY sb-admin /var/www/html/
 ●   CMD         EXPOSE 8080
                 CMD apachectl -DFOREGROUND
                       docker build -t apache-centos .
                       docker run -d -p 81:80 apache-centos
                                                              27
Basic Commands   CMD <command>
                 run.sh
 ●   FROM
 ●   RUN         #!/bin/bash
 ●   COPY        echo "Iniciando container..."
 ●   CMD         apachectl -DFOREGROUND
                 FROM centos
                 RUN yum install httpd -y
                 COPY sb-admin /var/www/html/
                 COPY run.sh /run.sh
                 CMD sh /run.sh
                          docker build -t apache-centos .
                          docker run -d -p 81:80 apache-centos
                          http://localhost:81
                                                                 28
Build Context
  Is the set of files at a specified location PATH or URL.
   ●    The PATH is a directory on your local filesystem. The URL is a Git repository
        location.
              docker build -t apache-centos .
              Sending build context to Docker daemon 20 MB
   ●    To increase the performance and avoid to send unused files to Docker
        daemon use a .dockerignore file.
   ●    Warning: Do not use your root directory ‘/’ as the PATH as it causes the
        build to transfer the entire contents of your hard drive to the Docker
        daemon.
                                                                                           29
Build Context
When you issue a docker build command, the current working directory is called the build
context
https://docs.docker.com/engine/reference/builder/
.dockerignore File
   ●   It file allows to ignore some files or folders.
   ●   This file is hidden ( ‘.’ prefix)
       .dockerignore   (Ignore zip file)
       startbootstrap-sb-admin-2-gh-pages.zip
                                                                                          30
.dockerignore File
Inadvertently including files that are not necessary for building an image results in a
larger build context and larger image size. This can increase the time to build the
image, time to pull and push it, and the container runtime size
Best practices for writing Dockerfiles
   ●   Decouple applications: Each container should have only one concern.
       Decoupling applications into multiple containers makes it easier to scale
       horizontally and reuse containers.
   ●   Exclude with .dockerignore: Keep the build context clean.
   ●   Minimize the number of layers: Use the (&&) or (\) command.
        ○    Only the instructions RUN, COPY, ADD create layers. Other
             instructions create temporary intermediate images, and do not
             increase the size of the build
                                                                                   31
Best practices for writing Dockerfiles
1. (Decouple applications) One service by image, i.e. do not have a Apache and
MySQL installed in the same image because this was difficult to maintain
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
Multi-line sentences
     RUN echo "1" >> /usr/share/nginx/html/test.txt
     RUN echo "2" >> /usr/share/nginx/html/test.txt               Create 3 different
     RUN echo "3" >> /usr/share/nginx/html/test.txt                    layers
     Using ‘&&’ operator
     RUN echo "1" >> /usr/share/nginx/html/test.txt && echo "2"
     >> /usr/share/nginx/html/test.txt && echo "3" >>
     /usr/share/nginx/html/test.txt
     Using ‘&&’ and ‘\’ operator
     RUN echo "1" >> /usr/share/nginx/html/test.txt && \
         echo "2" >> /usr/share/nginx/html/test.txt && \           Create 1 layer
         echo "3" >> /usr/share/nginx/html/test.txt
                                                                                       32
Pipe Dockerfile Through stdin
  Docker allow us to build images by piping through stdin.
    ●   Creation of an image using ‘|’ and stdin:
           $ echo -e ‘FROM busybox\nRUN echo ”hello world”’ | docker build -
           $ docker build -<<EOF
           FROM busybox
           RUN echo “hello world”
           EOF
           $ docker build -t myimage:latest -<<EOF
           FROM busybox
           RUN echo “hello world”
           EOF
           The hyphen (-) takes the position of the PATH, and instructs Docker to read the
           build context (which only contains a Dockerfile) from stdin instead of a directory
                                                                                                33
Pipe Dockerfile Through stdin
  ●     standard input (stdin) / standard output (stdout) / standard error (stderr)
  ●     A pipe (|) is a form of redirection (transfer of standard output to some other
        destination)
BusyBox originally aimed to put a complete bootable system on a single floppy disk that
would serve both as a rescue disk and as an installer for the Debian distribution (Wikipedia)
       echo -e ‘FROM busybox\nRUN echo ”hello world”’ | docker build -t
busybox-stdin -
Pipe Dockerfile Through stdin
 ●   Using (-) to avoid path error when copying file
     $ touch somefile.txt
     # Build an image using the current directory as context, and
     # a Dockerfile passed through stdin
     $ docker build -t myimage:latest -f- . <<EOF
     FROM busybox
     COPY somefile.txt .
     RUN cat /somefile.txt
     EOF
                                                                    34
Dangling Images
 It is a image that doesn’t have name <repository> nor tag <none> but they take up
 space.
       This happen when you create an image with the same name and tag but with
       different content.
          # List only dangling images
          $ docker images -f dangling=true
          # Show only the IMAGE_ID for dangling images
          $ docker images -f dangling=true -q
          # Remove only dangling images
          $ docker images -f dangling=true -q | xargs docker rmi
                                                                                     35
Multi-stage-build
   ●   Allows to use several FROM within the same Dockerfile to create multiples
       images with dependencies.
   ●   Each FROM instruction can use a different base image, you can copy artifacts
       from one stage to another, leaving behind you don’t want in the final image.
                                                                                      36
Multi-stage-build
https://docs.docker.com/develop/develop-images/multistage-build/
Multi-stage-build
  Create a jar from Maven image and use this jar in a Java image
           # Create a folder
           mkdir multi-stage && cd multi-stage
           # Download a Maven project (google: maven sample app)
           $ git clone https://github.com/jenkins-docs/simple-java-maven-app.git
           # Dockerfile
           FROM maven:3.5-alpine as builder
           COPY simple-java-maven-app/ /app
           RUN cd /app && mvn package
              docker build -t java .
                                                                                             37
Multi-stage-build
Docker images tags with alpine the make reference tha using Alpine Linux distribution and
the reason is because this distribution is minimalist based on Busybox and Musl-libc, what
makes that the final size is much less than an image with Ubuntu or Debian.
        docker build --no-cache -t java .
- 'mvn package' build the initial jar in the path
(/app/target/my-app-1.0-SNAPSHOT.jar)
Multi-stage-build
   ●   Add a second FROM command
         # Dockerfile
         FROM maven:3.5-alpine as builder
         COPY simple-java-maven-app/ /app
         RUN cd /app && mvn package
         FROM openjdk:8-alpine
         COPY --from=builder /app/target/my-app-1.0-SNAPSHOT.jar /opt/app.jar
         CMD java -jar /opt/app.jar
            docker build -t my-app .
         # Consult the size of the previous images
         $ docker images | grep -e “java” -e “my-app” -e “openjdk”
                                                                                       38
Multi-stage-build
El peso de la imagen resultante va dado por el tamaño del ultimo FROM definido en el
Dockerfile.
Docker asume que las sentencias previas al FROM son sentencias temporales que no se
requieren en su totalidad en la imagen resultante.
Multi-stage-build
 ●   Create a container and see the logs
        # Container creation
        $ docker run -d --name c-my-app my-app
        # See the logs
        $ docker logs c-my-app
           The simple-java-maven-app app only print a “Hello World” and the process
           Finish. For this reason the container not appears in the running containers.
                                                                                          39
Containers
             40
Docker Container
   ●    A container is an additional layer that contains an execution in real-time of an
        image.
   ●    The container will execute CentOS that has installed Apache and it will run
        the Apache service.
         docker run -d --name <container_name> <image>:<version>
              ‘-d’ Run container in background
              ‘--name’ Assign a name to the container
                                                                                           41
. son una instancia de ejecución de una imagen
. son temporales
. capa de RW (lectura y escritura)
. se pueden crear varios contenedores a partir de una misma imagen
. cuando no se especifica un nombre al contenedor, docker asigna un nombre por
defecto
. si la imagen a usar no es encontrada, docker procederá a descargarla
 docker run hello-world
Test de docker
        $ docker run --name mysql -e MYSQL_ROOT_PASSWORD=12345678 -d
mysql
        Unable to find image 'mysql:latest' locally
        latest: Pulling from library/mysql
Ports mapping
   ●   With docker ps, we can see the mapping ports
       docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=12345678 mysql
       docker ps
       As in the previous container we don’t expose a port, we can not be
       able to do a request to the 3306 port. To solve this we need to use
       the ‘-p’ option.
       docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=12345678 -p 3306:3306 mysql
       docker ps
                                                                      localhost:3306
                                                                                             42
this environment, which is isolated from the rest of your system, so you need to map ports
to the outside world
Shell’s container
   ●   When we create a container we can enter to the shell terminal of that
       container.
       docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=12345678 -p 3306:3306 mysql
       docker ps
                   docker exec -it <container_name> bash
             ‘-i’ interactive (Keep STDIN open even if not attached)
             ‘-t’ terminal
                                                                                       43
execute an interactive bash shell on the container.
Docker exec
   ●   Enter to the terminal container.
       docker exec -it mysql bash
             The input device is not a TTY.   Add ‘winpty’ at the beginning
       root@53e0e38db15c:/# whoami
       root
       root@53e0e38db15c:/# hostname
       53e0e38db15c                              Inside the terminal
       root@53e0e38db15c:/# exit                      container
                                                                              44
What is a TTY? It's a terminal interface that supports color output, escape
sequences, moving the cursor around, etc,
Docker exec
 ●   Enter to the terminal with a specific user.
     docker exec -u root -it mysql bash
     root@53e0e38db15c:/# whoami
     root
     root@53e0e38db15c:/# hostname
     53e0e38db15c
     root@53e0e38db15c:/# exit
 ●   To know the user list in Linux see the file /etc/passwd
                                                               45
                                 MySQL Database with
                                       Docker
 Pull Image
                            Check DB
                             status
               Create
              Container                     Use the DB
                                                                       46
MySQL Database with Docker
 1.    docker run -d --name mysql -e “MYSQL_ROOT_PASSWORD=12345678”
       mysql:5.7
 2.    docker logs -f mysql
       -----------------------------
 3.    docker run --rm -ti centos bash
              # yum install mysql -y
              # mysql (ERROR 2002)
              # mysql -u root -p12345678 -> (ERROR 2002) “xq?, en la
              creación del contenedor docker no se especificó prt”
 5.    docker inspect mysql
         -    Check the Networks, IPAddress (172.17.0.2)
 6.    En el contenedor centOS
              # mysql -u root -h 172.17.0.2 -p12345678
              MYSQL > show databases;
                          Host
IP 172.17.0.2
mysql-db                                         centos
-   3306 (MySQL)                             -    MySQL (client)
                    show databases;
                Information_schema, mysql,
                  performance_schema, sys
                                                           172.17.0.2:3306
                                                           127.0.0.1:3306
                                                                             47
                        Host (127.0.0.1)
                         -   3305 (MySQL-DB)
     IP 172.17.0.2
      mysql-db                                     centos
     -   3306 (MySQL)                          -    MySQL (client)
                                                            172.17.0.2:3306
                                                            127.0.0.1:3305
                                                                              48
1.   docker run -d --name mysql-db -p 3305:3306 -e
     “MYSQL_ROOT_PASSWORD=root” -e “MYSQL_DATABASE=docker-db” -e
     MYSQL_USER=docker-user" -e "MYSQL_PASSWORD=12345678" mysql:5.7
2.   docker logs -f mysql-db
3.   $ mysql -u root -p12345678 -h 127.0.0.1 --port 3305 (First root user)
     MYSQL > show databases;
     $ mysql -u docker-user -p12345678 -h 127.0.0.1 --port 3305 (First
     root user)
     MYSQL > show databases;
                                   Commit PostgreSQL
  Create
                                    Database Image
 Container
                            Generate
                            an image
             Create a                      Tagging the
             database                         image
                                                                               49
  ●    docker run -d --name postgres -e "POSTGRES_PASSWORD=12345678" -e
       "POSTGRES_USER=docker" -e "POSTGRES_DB=docker-db" -p 5432:5432
       postgres
  ●    docker exec -ti postgres bash
              # psql -d docker-db -U docker    // ingresar a la db
              # create table MCT(connection_type varchar(10) CONSTRAINT x101
PRIMARY KEY, minimum_connection_time numeric(4));
              # \dt
              # insert into MCT values ('DOM_DOM', 20);
              # select * from MCT;
              # exit;
  ●    docker ps -a
  ●    docker diff <container ID> # inspect all the changes we made
  ●    docker commit <container ID>
  ●    docker images
       # REPOSITORY           TAG                 IMAGE ID           CREATED
       SIZE
       # <none>               <none>              ee60ba054526       7
       seconds ago       93MB
  ●   docker image tag <image id> <repository>
Creating Images
  Jenkins (https://hub.docker.com/r/jenkins/jenkins/)
     docker run -d -p 7070:8080 --name jenkins jenkins
     winpty docker exec -ti jenkins bash
     jenkins@9013d6d9b2a1:/$ cat /var/jenkins_home/secrets/initialAdminPassword
                                                                                          50
You can start and stop the container and all the data will remain inside the container,
but, if you need to update the image you will probably to lost the data because you
need to create a new container pointing to the newest image. To solve this problem
you are able to use volumenes.
Any change that you made inside the image you need to recreate the container
Docker users managements
In the Dockerfile you can specify the creation and use of users
      FROM   centos
      ENV    BAR foo
      RUN    useradd daniel
      USER   daniel
 docker build -t centos:users .
 docker run -d -ti --name centos-user centos:users
 docker exec -ti centos-user bash
                                                                       Nota: If the -ti
      # whoami                                                   parameter is not
      # echo $BAR                                                passed as parameter,
                                                                 the container will die
                                                                                          51
Docker users managements
Create a user but not leave it as main user
     FROM centos
     RUN useradd daniel
      docker build -t centos:users-2 -f Dockerfile.user .
      docker run -d -ti --name centos-user-v2 centos:users-2
      docker exec -ti centos-user-v2 bash
            # whoami
Log in with the user created in the Dockerfile (daniel)
      docker exec -ti -u daniel centos-user-v2 bash
           # whoami
                                                               52
Limit Container Resources
 Create a user but not leave it as main user
                                               53
# List images                                  # Show layers of an image
docker images                                  docker history -H <image>:<tag>
                                               --no-trunc
# Filter images based on conditions
docker images -f dangling=true                 # Build a images using a different
                                               Dockerfile
# Delete images                                docker build -t <image_name> -f
docker rmi <image id>                          </PATH/Dockerfile_name> .
docker rmi <repository>:<tag>
  If you have 2 o more image with different
tags and you don’t put the tag in the moment
of delete the image it will delete the
latest version
# Delete several images in the same line
docker rmi <image_id1> .. <image_idn>
# Delete all images
docker image prune -a -f
docker image prune --help
docker images -q | xargs docker rmi
                                                                                    54
# Create a container                             # Delete container
docker run -d <image>                            docker rm <container_name>
# Assign a name to the container                 # Delete all containers
docker run -d --name <cont_name> <image>         docker rm -fv $(docker ps -aq)
# List containers running                        # Port mapping
docker ps                                        docker run -d -p <local_p>:<docker_p>
                                                 <image>
# List all containers
                                                 # Create env variable
docker ps -a                                     docker run -d -e “<var=value>”   <image>
# Show the last container created                # Statistic resources used
docker ps -l                                     docker stats <container_name>
# Start container
docker start <container_name> | <container_id>
# Stop container
docker stop <container_name> | <container_id>
# Restart container
docker restart <container_name>|<container_id>
                                                                                            55
# Create a container and database                # Commit container
docker run -d --name postgres -e                 docker commit <container id>
"POSTGRES_PASSWORD=12345678" -e
"POSTGRES_USER=docker" -e                        # List images
"POSTGRES_DB=docker-db" -p 5432:5432             docker images
postgres
                                                 # Tagging the image
# Enter to the container                         docker image tag <image id> <repository
docker exec -ti postgres bash                    name>
# psql -d docker-db -U docker
# create table MCT(connection_type varchar(10)
CONSTRAINT x101 PRIMARY KEY,
minimum_connection_time numeric(4));
# \dt
# insert into MCT values ('DOM_DOM', 20);
# select * from MCT;
# exit;
# List all containers
docker ps -a
# Inspect the changes
docker diff <container id>
                                                                                           56
                                                    SlidesCarnival icons are editable shapes.
                                                    This means that you can:
                                                       ●    Resize them without losing quality.
                                                       ●    Change line color, width and style.
                                                    Isn’t that nice? :)
                                                    Examples:
                                                                                                  57
https://training.play-with-docker.com/ops-stage1/