Menu Search Sign up

Docker

Overview

https://docs.docker.com/get-started/
https://stackoverflow.com/questions/16047306/how-is-docker-different-from-a-normal-virtual-machine 

Install Docker on Windows

1. Download dockerToolbox.exe from https://docs.docker.com/toolbox/toolbox_install_windows/ which will include a 64-bit oracle VM Virtual Box
2. Click downloaded DockerToolBox.exe to install
3. Click "Docker QuickStart Terminal" to start the terminal, so you can execute docker-machine and docker commands

4. Open "Oracle VM VirtualBox Manager" to configure your "default" docker engine.

https://docs.docker.com/toolbox/toolbox_install_windows/

Docker Engine

When people say “Docker” they typically mean Docker Engine, the client-server application made up of the Docker daemon, a REST API that specifies interfaces for interacting with the daemon, and a command line interface (CLI) client that talks to the daemon (through the REST API wrapper). Docker Engine accepts docker commands from the CLI, such as docker run <image>, docker ps to list running containers, docker image ls to list images, and so on.

https://docs.docker.com/machine/overview/#whats-the-difference-between-docker-engine-and-docker-machine

Docker Machine

Docker Machine is a tool for provisioning and managing your Dockerized hosts (hosts with Docker Engine on them). Typically, you install Docker Machine on your local system.

Docker Machine has its own command line client docker-machine and the Docker Engine client, docker. You can use Machine to install Docker Engine on one or more virtual systems. These virtual systems can be local (as when you use Machine to install and run Docker Engine in VirtualBox on Mac or Windows) or remote (as when you use Machine to provision Dockerized hosts on cloud providers). The Dockerized hosts themselves can be thought of, and are sometimes referred to as, managed “machines”.

https://docs.docker.com/machine/overview/#whats-the-difference-between-docker-engine-and-docker-machine

Docker Machine commands refernce: https://docs.docker.com/machine/reference/

On Windows, You can also check if a Docker Engine is running or not by going to "Oracle VM VirtualBox Manager", and see if the "default" Docker Engine or other is running, stopped, or in other status.

Docker Machine CLI

List all configured dockerized hosts (docker engines) in running or timeout (not responding so may not be running) state.

docker-machine ls

Log into a dockerized host

docker-machine ssh default (the dockerized host named "default")

Start a dockerized host (ie, the docker engine daemon on a host)

docker-machine start default

Stop a dockerized host

docker-machine stop default

https://docs.docker.com/machine/reference/

How to access host folders after logging into a dockerized host?
In "Oracle VM VirtualBox Manager", go to Settings->Shared Folders, and map the host's folders

Docker CLI Reference

https://docs.docker.com/engine/reference/commandline/run/#description

Image Commands

pull images from remote registry
docker image pull <image name> (for example, alpine, fedora, ubuntu, etc. Note alpine is a linux OS)

list local images
docker image ls

Container Commands

list running containers
docker container ls
list running or recently running but currently exited containers
docker container ls -a
run a container (to execute 1 command and then exit)
docker container run alpine ls -l
docker container run alpine echo "echo from alpine"
docker container run alpine /bin/sh

NOTE: specify <image name>:<tag name> to run the exact image you want
run a container interactively (and let the container stay)
docker container run -it alpine /bin/sh
docker run -it fedora bash

NOTE: specify <image name>:<tag name> to run the exact image you want
pull images from remote registry and then run it interactively
docker container run -ti ubuntu bash
run an image as container, make some changes in it, save it as different image, run it again
docker container run -ti ubuntu bash
In ubuntu:
mkdir test
touch ./test/test.txt
exit
docker container ls -a //to get a list of the recently run container IDs
docker container commit <the container ID of the ubunto container>
docker image ls //to see a list of the saved images, including the new ubunto image
docker container run -it <new ubunto image id> //to run the new ubunto container with your saved changes.

Build a docker image for your spring boot application

Step 1 prepare a Dockerfile like below:

FROM openjdk:8-jre-alpine

MAINTAINER you name

LABEL name="Suspend Micro Service (jar)"
LABEL description="Suspend Micro Service for OMC"
LABEL url="http://localhost:8090/suspend"
LABEL vendor="@myname"

EXPOSE 8090

MKDIR /tmp
COPY ./target/suspend-0.0.2-SNAPSHOT.jar /suspend-0.0.2-SNAPSHOT.jar
ENTRYPOINT ["/usr/bin/java", "-Djava.security.egd=file:/dev/./urandom", "-Xmx256m", "-jar", "/suspend-0.0.2-SNAPSHOT.jar"]


Step 2 run the following docker build command in the same folder where you have the above Dockerfile:

docker build -t myname/eho-omc-spndsvc:0.0.2  .

Build a docker image for your angular application

Step 1 prepare a Dockerfile like below:

#Final nginx image ======================================
FROM nginx:alpine

MAINTAINER My Name

# Copy custom nginx config
COPY ./.docker/nginx.conf /etc/nginx/nginx.conf
COPY ./nginx-startup.sh /
RUN chmod a+x /nginx-startup.sh

COPY --from=node-angular-app /app/dist /usr/share/nginx/html

WORKDIR /usr/share/nginx/html

EXPOSE 80 443

# ENTRYPOINT ["nginx", "-g", "daemon off;"]
ENTRYPOINT ["/nginx-startup.sh"]

Step 2 run the following docker build command in the folder that is the parent folder of your Dockerfile folder (docker)

docker build -f .docker/Dockerfile.prod -t myname/eho-omc-spndui:0.0.1  .

Portainer

Reference

http://portainer.readthedocs.io/en/stable/index.html

How to deploy and run

If portainer is already deployed and running:

docker stop <container id> && docker rm <container id>

Do the following to deploy and run:

docker run -d --name portainer -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer

How to access on Windows locally

http://192.168.99.100:9000

You can choose a username / password like admin / welcome1

Nginx

Overview

Nginx is a small footprint http server, like apache.

Where does nginx store html files in a nginx container?

/usr/share/nginx/html

Where does nginx store its configuratin file(s)?

/etc/nginx/nginx.conf and /etc/nginx/conf.d/*.conf

How to start nginx?

#!/bin/sh
find /usr/share/nginx/html -name "main.*" -type f | xargs /bin/sed -i "s|http://localhost|${BASE_URL}|g"
exec nginx -g 'daemon off;'
exec "$@"

Example nginx configuration file

worker_processes 4;

events { worker_connections 1024; }

http {
        ssl_session_cache   shared:SSL:10m;
        ssl_session_timeout 30m;

        #See http://blog.argteam.com/coding/hardening-node-js-for-production-part-2-using-nginx-to-avoid-node-js-load
        proxy_cache_path        /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
        proxy_temp_path         /var/tmp;
        include                 mime.types;
        default_type            application/octet-stream;
        sendfile                on;
        keepalive_timeout       65;

        gzip                    on;
        gzip_comp_level         6;
        gzip_vary               on;
        gzip_min_length         1000;
        gzip_proxied            any;
        gzip_types              text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_buffers            16 8k;

    upstream auth_server {
        server auditsvc:8090;
    }

    server {
      listen 80;
      sendfile on;
      default_type application/octet-stream;

      gzip on;
      gzip_http_version 1.1;
      gzip_disable      "MSIE [1-6]\.";
      gzip_min_length   256;
      gzip_vary         on;
      gzip_proxied      expired no-cache no-store private auth;
      gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
      gzip_comp_level   9;

      root /usr/share/nginx/html;

      location / {
        try_files $uri $uri/ /index.html =404;
      }

      location /audit {
        proxy_set_header    Host $host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto $scheme;

        keepalive_timeout   60;
        proxy_read_timeout  90;
        proxy_redirect      off;
        proxy_pass http://auditsvc:8090/audit/;
      }

      location /suspend {
        proxy_set_header    Host $host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto $scheme;

        keepalive_timeout   60;
        proxy_read_timeout  90;
        proxy_redirect      off;
        proxy_pass http://spndsvc:8090/suspend/;
      }

      location /auth {
        proxy_set_header    Host $host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto $scheme;

        keepalive_timeout   60;
        proxy_read_timeout  90;
        proxy_redirect      off;
        proxy_pass http://auditsvc:8090/auth/;
      }
    }
}

Docker-Compose

https://docs.docker.com/compose/overview/

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Here is a sample yaml file (docker-compose.yaml):

version: "3.3"
services:
  spndui:
    container_name: spndui
    image: myname/eho-omc-ui:0.0.1
    # networks:
    #   - spnduinet
    ports:
      - "80:80"
      - "443:443"
    # links:
    #   - spndsvc
    #   - auditsvc
    # volumes:
    #   - "./nginx-startup.sh:/usr/local/bin/nginx-startup.sh"
    depends_on:
      - spndsvc
      - auditsvc
    # deploy:
    #   replicas: 1
    #   restart_policy:
    #     condition: on-failure
    #     max_attempts: 1
    environment:
      - BASE_URL=http://localhost
    labels:
      - ENV=development

  spndsvc:
    container_name: spndsvc
    image: myname/eho-omc-spndsvc:0.0.2
    # networks:
    #   - spnduinet
    # ports:
    #   - "8091:8090"
    # deploy:
    #   replicas: 1
    #   restart_policy:
    #     condition: on-failure
    #     max_attempts: 1

  auditsvc:
    container_name: auditsvc
    image: myname/eho-omc-auditsvc:0.0.2
    # networks:
    #   - spnduinet
    # ports:
    #   - "8090:8090"
    # deploy:
    #   replicas: 1
    #   restart_policy:
    #     condition: on-failure
    #     max_attempts: 1

# networks:
#   spnduinet:
#     driver: overlay
#     ipam:
#       driver: default
#       config:
#       - subnet: 172.10.0.0/16

Place the above yaml file in the same folder where you want to run the docker-compose command:

docker-compose up -d