Quick Guide To Docker

What is docker?

Docker provides a virtualization environment to run containers. In more practical terms it means we can define docker images that run on top of the OS virtualization to provide services. A container is in principle the instance of the image.

Docker Terminology Definitions

Docker image

A docker image is a set of services defined to run on top of the Virtual Host OS.

A file containing a docker image definition ex.
  1. # define the base image to pull  
  2. FROM docker.elastic.co/elasticsearch/elasticsearch:7.10.2  
  3. # make port 9200 and 9300 available to the world outside this container  
  4. EXPOSE 9200  
  5. EXPOSE 9300  
Add the above to a file named "dockerfile" in the root of a folder called my_elastic_image and we can build the image with;
  1. # build the image file    
  2. $ docker build ./my_elastic_image --tag=mbister/elastic_cust    
  3. # run the custom image file    
  4. $ docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" mbister/elastic_cust 
This is how simple it is to pull the base of of ubuntu and start building a custom image
  1. FROM ubuntu:latest    
  2. # Update the image to the latest packages    
  3. RUN apt-get update && apt-get upgrade -y 
Docker compose
With docker-compose and a docker-compose.yml file we can define and run multiple container services. For example, we can fire up Kibana and Elastic in one composition.
  1. version: '3'  
  2. services:  
  3.   elasticsearch:  
  4.     image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2  
  5.     environment:  
  6.       - cluster.name=docker-cluster  
  7.       - bootstrap.memory_lock=true  
  8.       - "ES_JAVA_OPTS=-Xms512m -Xmx512m"  
  9.     ulimits:  
  10.       memlock:  
  11.         soft: -1  
  12.         hard: -1  
  13.     ports:  
  14.       - "9200:9200"  
  15.   kibana:  
  16.     image: docker.elastic.co/kibana/kibana:7.10.2  
  17.     ports:  
  18.       - "5601:5601" 
We can run the above file by;
  1. # run the docker-compose file in the background (-d)  
  2. $ docker-compose up -d  
  3. # specifying the docker-compose file  
  4. $ docker-compose -f <compose_file> up -d 
We can now access Kibana on http://localhost:5601/ and ES on http://localhost:9200/. To shutdown we use the down option;
  1. $ docker-compose down 
If you just want to fire up a container with out much fuzz;
  1. $ sudo docker pull docker.elastic.co/elasticsearch/elasticsearch:7.10.2   
  2. $ sudo docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --name "es_demo_app" docker.elastic.co/elasticsearch/elasticsearch:7.10.2 
The 'p' flag sets the port forwarding to the container, from local to container.
The 'e' flag sets elastic to a single node mode.
The --name flag is used to give it a handy name and it will ensure we start the same container with data persistency.
Start by using the name.
  1. #start
  2. $ docker container run es_demo_app
Stop by using the name.
  1. #stop  
  2. $ docker container stop es_demo_app 
List containers
  1. $ docker container ls 
List running containers
  1. $ docker container ps 
Tricks and treats.
A handy way of removing all containers
  1. #-a all -q ids
  2. $ docker container rm $(docker container ls -a -q) 
A super handy little trick if your container crashes or causes other issues, you can access the terminal on the docker image:
  1. $ docker exec -it <containerid> /bin/bash