Category Archives: Docker

Kubernetes useful commands

!!This is a draft document!!

Minikube Setup Commands

Linux: curl -LO$(curl -s
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version
Linux: curl -Lo minikube && chmod +x minikube && sudo mv minikube /usr/local/bin/
minikube start
kubectl run hello-minikube --port=8080
kubectl expose deployment hello-minikube  --type=NodePort
kubectl get pod
curl $(minikube service hello-minikube --url)
kubectl delete deployment hello-minikube
minikube stop

Basic Kubectl Commands

kubectl get pods
kubectl get pods [pod name]
kubectl expose  <identifier/name> [—port=external port] [—target-port=container-port [—type=service-type]
kubectl port-forward  [LOCAL_PORT:]REMOTE_PORT]
kubectl attach  -c 
kubectl exec  [-it]  [-c CONTAINER] — COMMAND [args…]
kubectl label [—overwrite]  KEY_1=VAL_1 ….
kubectl run  —image=image

Scaling Commands

kubectl scale —replicas=4 deployment/tomcat-deployment
kubectl expose deployment tomcat-deployment --type=NodePort
kubectl expose deployment tomcat-deployment —type=LoadBalancer —port=8080 —target-port=8080 —name tomcat-load-balancer
kubectl describe services tomcat-load-balancer
kubectl describe services tomcat-load-balancer

Deployments Commands

kubectl get deployments kubectl rollout status kubectl set image kubectl rollout history

Secret Commands

kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
kubectl create secret generic mysql-pass --from-literal=password=YOUR_PASSWORD

How to create a Docker with Jenkins, CaspesJS, PhantomJS and ChromeDriver

Create a Dockerfile with this content:

FROM jenkins

RUN wget -q -O - | apt-key add - && echo "deb stable main" > /etc/apt/sources.list.d/google.list && apt-get update && apt-get install -y google-chrome-stable

RUN /usr/local/bin/ ace-editor  ant antisamy-markup-formatter branch-api build-timeout cloudbees-folder credentials credentials-binding chromedriver  durable-task email-ext external-monitor-job git git-client github github-api github-branch-source github-organization-folder git-server gradle handlebars icon-shim javadoc jquery-detached junit ldap mailer mapdb-api matrix-auth matrix-project momentjs pam-auth pipeline-build-step pipeline-input-step pipeline-rest-api pipeline-stage-step pipeline-stage-view plain-credentials scm-api script-security ssh-credentials ssh-slaves structs subversion timestamper token-macro windows-slaves workflow-aggregator workflow-api workflow-basic-steps workflow-cps workflow-cps-global-lib workflow-durable-task-step workflow-job workflow-multibranch workflow-scm-step workflow-step-api workflow-support ws-cleanup

USER root
 RUN apt-get clean && apt-get update && apt-get install -y npm build-essential chrpath libssl-dev libxft-dev libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev

RUN cd ~ && wget && tar xvfj phantomjs-2.1.1-linux-x86_64.tar.bz2 && rm phantomjs-2.1.1-linux-x86_64.tar.bz2 && mv phantomjs-2.1.1-linux-x86_64 /usr/local/share && ln -sf /usr/local/share/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin && npm install -g casperjs


Dockerfile first RUN

On the first RUN we’ll install the google-chrome for use with the ChromeDriver plugin on Jenkins.

Dockerfile second RUN

Here we’ll installed all the plugins we use on Jenkins.

Dockerfile next RUN

The third RUN we’ll install all packages we’ll need for the last version of phantomjs.

Dockerfile last RUN

In this RUN we download the last version of phantom and install it, then install casperjs using npm.

Build and run the new Jenkins Docker

docker build -t lcjenkins .
docker run --name lcjenkins -p 8888:8080 -p 50000:50000 -v $jhome:/var/jenkins_home tgjenkins

* Change the $jhome for the path you want to have the jenkins home.


Kubernetes (is an open source container cluster manager by Google)
maestro-ng (simple cluster manager)
Fig (Old cluster manager)





# How to backup a running docker:
# First commit the running docker
# The -p option will PAUSE the running machine
docker commit -p container_id newnamebkp
# Now generate a tar archive from the image
docker save -o /var/backups/name.tar newnamebkp
# Committing an existing container
#docker commit $CONTAINERID diretorio/nome:1.0 (versioning, create image)
# Ex.
docker commit $CONTAINERID containers/nix-apache:1.0
docker run -i -t -p 8080:80 containers/nix-apache:1.0 /bin/bash

# How to access docker container using bash

docker exec -i -t $CONTAINERID  /bin/bash

# Useful commands

docker exec $CONTAINERID ls (run command at guest)
docker inspect $CONTAINERID (show all data about containers, ip, image…)
docker stats (show usage stats)
docker stop $CONTAINERID (Stop and save)
docker start $CONTAINERID
docker rm $CONTAINERID (remove)
docker run --name laladb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
ctrl + p q (Exit an leave container open)
docker ps (List containers)
docker ps -a (list all containers)
docker attach $CONTAINERID (join container )
docker diff (Show diff files from the begining of container)
docker run -i -t ubuntu:15.10 /bin/bash (Run)
docker run -i -t -p 8080:80 ubuntu:14.10 /bin/bash (map 8080 host to 80 guest)
docker run -d -p x.x.x.x:hostPort:containerPort registry/image
# Autostart existing container
docker update --restart=always containername

Docker Cookbook

5.3 pg 135
8.10 Using Kubernetes in the Cloud via GCE pg. 254
10.2 Jenkins Continous Delivery pg. 304

Nice videos

Docker Compose (official from docker)