Introduction to Docker and Kubernetes - madhusudana30/Kubernetes-Learning GitHub Wiki

Install Docker in Linux and Run the Daemon

sudo su

yum install docker

service docker start

Docker Fundamentals

  • Docker runs on Container based Management.

  • Docker Client -Server Architecture:

  • Docker Client - command line interacts with Docker Daemon.

  • Docker Client GUI- Kitematic

  • Run docker container in background:

  • docker run -d imagename

  • list all docker runs and background docker

  • docker ps -a

  • Name docker:

  • docker run -name hello image name

  • docker inspect dockerimageid (backgorund id)

Docker Port mapping and logs Command:

  • docker run -it -p 8888:8080 tomcat:8.0

  • ctl + c to exit

  • docker logs dockerId: To see the logs.

Docker Image:

docker history dockerimage:tag

Build Docker Images:

  • Docker File is a text document that contains instructions to assemble image.

Create docker file:

  • touch Dockerfile (Name should be Dockerfile without any extention)

if docker file is in different location provide -f option while building an image.

Build Docker Image:

  • docker build -t name/imageName . (. specifies Dockerfile is available in current directory)

  • Docker Images are persistent and read only

Ways to Build Docker Image:

  • Commit changes made in docker container

  • Write a Dockerfile.

  • Docker commit saves changes made in Docker Container and creates a new Image.

  • get docker container id

  • docker ps -a

  • docker commit containerId userName/imagename:version

  • creates a new image and returns the container id.

  • spin of container with new image.

  • Try with the Debian image to build openldap

Dockerfile in Depth:

  • Each run command will execute the command on the top writable layer of the conainer and then commit the container as a new image.

  • The New image is used as a next step in Dockerfile. So each run instruction creates a new image layer.

  • Its Recommended the chain the run instructions in Dockerfile to reduce the number of image layers it creates.

  • Aggregate the number of instructions in single Run command in Dockerfile.

RUN apt-get update && apt-get install

  • Sort multi line arguments for readability.

CMD comamnds

CMD instruction specifies what command to run during the start up of Docker container.

CMD ["echo","hello-world"]

  • Docker build and run to see message.

  • Override the CMD instruction while running docker container.

Docker Cache:

  • Each time docker executes an instruction it builds a new image layer.

  • If there is no change in instruction, next time Docker executes an instruction it reuses the image layer. ** Specify No Cache Instruction** --no-cache=true to avoid caching.

Copy Instructions

  • Copy instruction copies files or Directories from build context and adds them to the file system of Docker Container.

  • touch abc.txt

  • vi Dockerfile

  • COPY abc.txt /src/abc.txt

  • docker build -t username/imagename .

  • successfully build image id:

  • docker run -t image id to enter into docker file system of container.

  • cd src

  • ls

  • ADD instruction is similar to COPY but ADD can download from internet and unpack compressed files.

PUSH Docker Image to Docker hub

official images are preferred.

  • User always version instead of latest tag of image.
  • User docker hub credentials to push image to docker hub.

Containerize the Simple Web Application

*** see ip address of docker machine**

  • docker-machine ls

**Run commands inside the Running Docker container **

  • docker exec -t containerId bash(which command to run)

  • ps axu displays all running process in the container.

EXAMPLES:

  • yum install docker

  • service docker start

  • Create Docker File

  • vi Dockerfile

  • FROM centos:7

  • Build Docker Image:

  • docker build -t openldapimage .

  • [root@angularmachine Downloads]# docker build -t openldapimage .

  • Sending build context to Docker daemon 176.6 MB

  • Step 1/1 : FROM centos:7

  • Trying to pull repository docker.io/library/centos ...

  • 7: Pulling from docker.io/library/centos

  • 8ba884070f61: Pull complete

  • Digest: sha256:b5e66c4651870a1ad435cd75922fe2cb943c9e973a9673822d1414824a1d0475

  • Status: Downloaded newer image for docker.io/centos:7

  • ---> 9f38484d220f

  • Successfully built 9f38484d220f

  • Start Docker image with bash command

  • it is for interactive mode

  • docker run -it 9f38484d220f bash

  • docker cp openldap-2.4.47 de588d2e6f74ff795ce15abd0e8e8826b2e4a70a332a9f04f923709222a46e07:/home/openldap-2.4.47

  • COPY Contents from/To Docker Container

  • https://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container

  • The cp command can be used to copy files. One specific file can be copied like:

  • docker cp foo.txt mycontainer:/foo.txt

  • docker cp mycontainer:/foo.txt foo.txt

  • For emphasis, mycontainer is a container ID, not an image ID.

  • Multiple files contained by the folder src can be copied into the target folder using:

  • docker cp src/. mycontainer:/target

  • docker cp mycontainer:/src/. target

  • Reference: Docker CLI docs for cp

  • In Docker versions prior to 1.8 it was only possible to copy files from a container to the host. Not from the host to a container.

  • docker ps -a

  • CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

  • 128de889d173 9f38484d220f "bash" 2 minutes ago Exited (0) 2 minutes ago kickass_jones

  • de588d2e6f74 9f38484d220f "bash" 3 minutes ago Exited (0) 3 minutes ago elastic_clarke

  • 26ed3e64b217 9f38484d220f "bash" 7 minutes ago Exited (130) 3 minutes ago pedantic_easley

  • bd54200017b4 9f38484d220f "/bin/bash" 7 minutes ago Up 7 minutes serene_joliot

  • 903349c074ae 9f38484d220f "bash" 9 minutes ago Up 9 minutes lucid_engelbart

  • https://www.youtube.com/watch?v=7tGcnOvRQ9o

  • working cp:

  • docker run -it --name openldapimage 9f38484d220f /bin/bash

  • copy text file from docker image to current directory.

  • docker cp openldapimage:/home/test.txt .

  • Copy tar file from vm to docker image

  • docker cp openldap-2.4.47.tgz openldapimage:/home/

  • docker ps

  • docker start openldapimage

  • docker exec -it openldapimage /bin/bash