What is Docker?

Installation of docker

$ brew cask list
$ brew cask install docker
$ brew cask install docker-toolbox

Some docker commands

search images
$ docker search elasticsearch
list images
$ docker images
list containers
$ docker ps
$ docker ps -a | grep 60afe4036d97
list all running containers
$ docker ps -a -f status=running
remove container
$ docker rm <containerid>
remove multiple container
$ docker rm 305297d7a235 ff0a5c3750b9
$ docker rm $(docker ps -a -q -f status=exited)
remove image
$ docker rmi <imageid>
remove all images
$ docker rmi $(docker images -q)
start container
$ docker start zen_leavitt
$ docker start -ai zen_leavitt
stop container
$ docker stop zen_leavitt

create machine
$ docker-machine create --driver virtualbox default
rename container
$ docker rename CONTAINER NEW_NAME
get container
$ docker pull glassfish
run container
$ docker run glassfish
get and run container
$ docker run -it glassfish bash
$ docker run -it glassfish sh

login to Docker Hub
$ docker login

$ docker run -d -p 8484:8080 --name horinca imworld/horinca
$ docker exec -it glassfish /bin/bash
$ mvn clean install && docker build -t imworld/horinca .
$ docker kill horinca

Hello App

build java repo after creating Dockerfile

FROM centos:7
LABEL maintainer="dev@aripd.com"
RUN yum update -y \
  && yum -y install unzip \
  && yum -y install java-1.8.0-openjdk-devel \
  && yum clean all
ENV JAVA_HOME /usr/lib/jvm/java-1.8.0
ENV PATH "$PATH":/${JAVA_HOME}/bin:.:
$ docker build -t aripd/java .
$ docker push aripd/java

build glassfish5 repo after creating Dockerfile

FROM aripd/java
LABEL maintainer="dev@aripd.com"
ENV GLASSFISH_ARCHIVE glassfish5
ENV DOMAIN_NAME domain1
ENV INSTALL_DIR /
RUN curl -o ${INSTALL_DIR}/${GLASSFISH_ARCHIVE}.zip -L http://download.oracle.com/glassfish/5.0/nightly/latest-glassfish.zip \
    && unzip ${INSTALL_DIR}/${GLASSFISH_ARCHIVE}.zip -d ${INSTALL_DIR} \
    && rm ${INSTALL_DIR}/${GLASSFISH_ARCHIVE}.zip
ENV GLASSFISH_HOME ${INSTALL_DIR}/${GLASSFISH_ARCHIVE}/glassfish
ENV DEPLOYMENT_DIR ${GLASSFISH_HOME}/domains/domain1/autodeploy
WORKDIR ${GLASSFISH_HOME}/bin
ENTRYPOINT asadmin start-domain --verbose ${DOMAIN_NAME}
EXPOSE 4848 8009 8080 8181
$ docker build -t aripd/glassfish5 .
$ docker push aripd/glassfish5

build hello app after creating Dockerfile

FROM aripd/glassfish5
LABEL maintainer="dev@aripd.com"
COPY ./target/hello.war ${DEPLOYMENT_DIR}
$ mvn clean install && docker build -t aripd/hello .
run container
$ docker run -d -p 8080:8080 -p 4848:4848 --name hello aripd/hello
stop container
$ docker stop hello
remove container
$ docker rm hello
push container
$ docker push aripd/hello

to check jax-rs using curl via terminal

curl -H'Accept:application/json' http://localhost:8080/hello/resources/hello

build mysql-data-container repo after creating Dockerfile

FROM mysql:latest
LABEL maintainer="dev@aripd.com"

VOLUME /var/lib/mysql

CMD ["true"]
$ docker build -t aripd/mysql-data-container .
$ docker push aripd/mysql-data-container
# Start the data-only container as
$ docker create --name mysql_data aripd/mysql-data-container
# Start the MySQL container as
$ docker run --volumes-from mysql_data --name mysqldb -e MYSQL_USER=mysql -e MYSQL_PASSWORD=mysql -e MYSQL_DATABASE=sample -e MYSQL_ROOT_PASSWORD=supersecret -p 3306:3306 -d mysql

Updating and committing an image

$ docker run -t -i aripd/ecommerce /bin/bash
root@0b2616b0e5a8:/# asadmin change-admin-password
root@0b2616b0e5a8:/# asadmin enable-secure-admin
... etc.
$ docker commit -m "Made some changes" -a "aripddev" 0b2616b0e5a8 aripd/ecommerce:v2
$ docker images
$ docker run -t -i aripd/ecommerce:v2 /bin/bash
$ docker history aripd/ecommerce:v2

commit and push docker images to dockerhub

docker ps
docker commit c3f279d17e0a  aripd/testimage:version3
docker images
docker login
docker push aripd/testimage

Log into the Docker Image using the root user

You can log into the Docker Image using the root user (ID = 0) instead of the provided default user when you use the -u option.

docker exec -u 0 -it mycontainer bash

getting logs

docker logs [container-name]

creating and connecting to a network

$ docker network create [network-name]
$ docker network inspect [network-name]
$ docker network connect [network-name] [container-name]
$ docker network inspect [network-name]

Running container on network

docker run --net [network-name] -d -p 8282:8080 --name [container-name] aripd/app-name

isolated network

Dockerfile

FROM        glassfish:latest
LABEL       maintainer="dev@aripd.com"
RUN         apt-get update
RUN         curl http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar -O glassfish/lib/mysql-connector-java-5.1.45.jar
COPY        domain.xml glassfish/domains/domain1/config/domain.xml
COPY        admin-keyfile glassfish/domains/domain1/config/admin-keyfile
COPY        target/ecommerce-3.0-SNAPSHOT.war glassfish/domains/domain1/autodeploy/ecommerce-3.0-SNAPSHOT.war
EXPOSE      8080 4848 8181

jdbc url is jdbc:mysql://mydatabase:3306/Database_Name

$ docker network create my-network
$ docker run -d --net=my-network --name=mydatabase -e MYSQL_ROOT_PASSWORD='supersecret' mysql
$ unset GEM_PATH
$ mvn clean install && docker build -t aripd/ecommerce .
$ docker run -d --net=my-network --name=myapp aripd/ecommerce

Running datascience image

$ docker run -d -p 8888:8888 --name datascience aripd/datascience

Setting Up Database Servers

MySQL

Download and start MySQL container:

$ docker run --name=mysql1 -d -p 3306:3306 -e MYSQL_USER=dev -e MYSQL_PASSWORD=dev123 -e MYSQL_DATABASE=dev mysql/mysql-server

Install MySQL Shell and connect to the database - for more details.

$ brew cask install mysql-shell
$ mysqlsh dev@localhost:3306

Microsoft SQL Server

Download and start Microsoft SQL Server container:

$ docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=SqlServer2017' -p 1433:1433 -d microsoft/mssql-server-linux:2017-latest

Install and connect using sqlcmdfor more details.

$ brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
$ brew install --no-sandbox mssql-tools

Connect to SQL Server:

$ sqlcmd -S localhost,1433 -U SA -P SqlServer2017

PostgreSQL

Download and run PostgreSQL:

$ docker run --name mypostgresdb -e POSTGRES_PASSWORD=psql123 -p 5432:5432 -d postgres

Install and connect using PSQL:

$ brew install psqlodbc
$ psql -h localhost -U postgres

Docker Compose

Create docker-compose.yml file and run

$ docker-compose up

Below is the example for graylog

version: '2'
services:
  # MongoDB: https://hub.docker.com/_/mongo/
  mongodb:
    image: mongo:3
  # Elasticsearch: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docker.html
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:5.6.2
    environment:
      - http.host=0.0.0.0
      - transport.host=localhost
      - network.host=0.0.0.0
      # Disable X-Pack security: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/security-settings.html#general-security-settings
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 1g
  # Graylog: https://hub.docker.com/r/graylog/graylog/
  graylog:
    image: graylog/graylog:2.4.3-1
    environment:
      # CHANGE ME!
      - GRAYLOG_PASSWORD_SECRET=somepasswordpepper
      # Password: admin
      - GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
      - GRAYLOG_WEB_ENDPOINT_URI=http://127.0.0.1:9000/api
    links:
      - mongodb:mongo
      - elasticsearch
    depends_on:
      - mongodb
      - elasticsearch
    ports:
      # Graylog web interface and REST API
      - 9000:9000
      # Syslog TCP
      - 514:514
      # Syslog UDP
      - 514:514/udp
      # GELF TCP
      - 12201:12201
      # GELF UDP
      - 12201:12201/udp