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

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://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar -o glassfish/lib/mysql-connector-java-5.1.40.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