Harshverm776 / docker-exercise

All Docker Related Exercise

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Docker Exercise

What are docker image and docker containers?

Docker Images are Templates used to create Docker Container

Container is a running instance of an image

Basic Commands

  • Version
docker -v

Docker Images

  • List of Images
docker images
  • List of commands for images
docker images --help
  • Pull an image
docker pull ubuntu
  • Filtering images
docker images -f "dangling=true"

Dangling images are images which are associated with any containers

  • Running an image
docker run --name MyUbuntu -it ubuntu bash
  • Inspecting an image
docker inspect ubuntu
  • Remove an Image
docker rmi ubuntu

Docker Containers

  • Show the list of running containers
docker ps
  • Show the list of all containers
docker ps -a
  • Start a container
docker start <container name/id>
  • Stop a container
docker stop <container name/id>
  • Pause a container
docker pause <container name/id>
  • Resume a container
docker unpause <container name/id>
  • Displaying stats
docker stats <container name/id>
  • Remove a container
docker rm <container name/id>
  • Get the history of an image
docker history <image name/id>

Dockerfile

What is a dockerfile?

A Dockerfile is a simple text file with instruction to build images

Automation of docker image creation

Dockerfile

How to create a Dockerfile?

  • Step 1 : Create a file named Dockerfile

  • Step 2 : Add instructions in Dockerfile

  • Step 3 : Build Dockerfile to create image

  • Step 4 : Run image to create container

Dockerfile example

FROM ubuntu

MAINTAINER ramanuj das <ramanujds9@gmail.com> 

RUN  apt-get update

CMD ["echo", "Hello World...! Image Running.."]

How to build image from Dockerfile?

docker build -t myUbuntu:1.0 .

#t flag is used for tagging an image

Running the image:

docker run <image id>

Docker Volume

Docker volumes are used for decoupling container from storage

Command to create a docker volume:

docker volume create myVolume1

Running jenkins with the volume:

docker run --name myJenkins1 -v myVolume1:/var/jenkins_home -p 9000:8080 -p 50000:50000 jenkins

Docker Mysql + SpringBoot

How to run MySql on Docker -

 docker pull mysql
 docker run -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydb -e MYSQL_USER=docker -e MYSQL_PASSWORD=password -p 3308:3306 --name mysql mysql

Running Spring Boot 3 Projects on Docker

  • Creating image through Spring Boot -

    mvn spring-boot:build-image
  • Running docker image -

  • Link Based Communication -

    docker container run -p 5000:5000 -e RDS_HOSTNAME=mysql -e RDS_PORT=3306 --link=mysql --name book-store book-store-server:0.0.1-SNAPSHOT
  • Host Based Communication -

  docker container run -p 5000:5000 --network=host book-store-server:0.0.1-SNAPSHOT
  • Custom Network

    • Creating a Custom Network :
docker network create book-store-mysql-network
  • Running Mysql Container on that network -
docker run -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydb -e MYSQL_USER=docker -e MYSQL_PASSWORD=password -p 3308:3306 --name mysql --network=book-store-mysql-network mysql
  • Running book-store on that network -
docker container run -p 5000:5000 -e RDS_HOSTNAME=mysql -e RDS_PORT=3306 --network=book-store-mysql-network -d --name book-store book-store-server:0.0.1-SNAPSHOT

Using Docker volume to Persist Data -

docker run -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydb -e MYSQL_USER=docker -e MYSQL_PASSWORD=password -p 3308:3306 --name mysql --volume mysql-db-volume:/var/lib/mysql --network=book-store-mysql-network mysql

Running Angular Application

How to run an Angular Application in Docker?

  • Build the Angular App
ng build [app-name] --prod=true
  • Create a Dockerfile in the root directory
FROM nginx
COPY dist/[app-name] /usr/share/nginx/html
  • Build the image
docker build -t [image-name] .
  • Run the image
docker run -p 80:80  [image-name]

Docker Compose

What is Docker compose?

  • Tool for defining and running musti-container Docker application
  • It uses yaml file to configure application services(docker-compose.yml)
  • Can start all services with a single command
  • Can scale up selected services when required

Steps

  • Step 1 : Create the docker compose file (docker-compose.yml)
  • Step 2 : Check the validity of the file using command:
docker-compose config
  • Step 3 : Run docker-compose yml file using command:
docker-compose up
  • Step 4 : Stop docker-compose using command:
docker-compose down

docker-compose.yml exmaple:

version: '3'

services:
    web:
        image: nginx
        ports:
            - 9090:80
    database:
        image: redis

How to use scaling?

docker-compose up -d --scale database=4

Running Fullstack Application using Docker-Compose

version: '3.7'

services:

  book-store-frontend:
    image: book-store-client-app 
    #build:
      #context: .
      #dockerfile: Dockerfile
    ports:
      - "4200:80"
    restart: always
    depends_on: # Start the depends_on first
      - book-store-spring-boot 
    networks:
      - book-store-app-network

  book-store-spring-boot:
    image: book-store-server:0.0.1-SNAPSHOT
    #build:
      #context: .
      #dockerfile: Dockerfile
    ports:
      - "5000:5000"
    restart: always
    # depends_on: # Start the depends_on first
    #   - mysql 
    environment:
      RDS_HOSTNAME: mysql
      RDS_PORT: 3306
      RDS_DB_NAME: mydb
      RDS_USERNAME: docker
      RDS_PASSWORD: password
    networks:
      - book-store-app-network

  mysql:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - "3308:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: docker
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: mydb
      MYSQL_ROOT_HOST: 192.168.0.48/255.255.255.248
    # volumes:
    #   - mysql-db-volume:/var/lib/mysql
    networks:
      - book-store-app-network
      # network:
      #   ipv4_address: 172.20.0.2
  
# Volumes
# volumes: 
#   mysql-db-volume:

networks:
  book-store-app-network:

Some useful Docker Compose Commands

  • Docker-compose Configuration
docker-compose config
  • Docker-Compose Images Used
docker-compose images
  • Docker-Compose Container Used
docker-compose ps
  • Docker-Compose Pause, Resume, Stop, Kill
docker-compose pause
docker-compose unpause
docker-compose stop
docker-compose kill
  • Docker-Compose Events
docker-compose events
  • Build Images from Dockerfile
docker-compose build

Running Microservices on Docker

About

All Docker Related Exercise


Languages

Language:Java 100.0%