Thursday 4 July 2019

Docker basic & commands | Docker part 1

What is Docker?
Docker is an open platform for developing, shipping and running application.


Why use Docker?
Docker help us in rapidly deployment using containers. No extra configuration, installation of features & roles or other software required.


Installing Docker on Windows
Download Docker.
Double-click InstallDocker.msi to run the installer.
Follow the Install Wizard: accept the license, authorize the installer, and proceed with the install.
Click Finish to launch Docker.
Docker starts automatically.

What is Docker Container?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.


Docker Plateform
Use
Docker Engine
To build and package apps as
Docker Images
Created using file written in
Dockerfile 
Format that run into
Container
That contains package of software and its dependencies.


Common commands
docker container ls
List containers 
docker container rm ""
Remove one or more containers
docker rmi 
Removes one or more images. docker rmi my_image
docker container kill
Kill one or more running containers
docker ps
 Lists running containers
docker ps -a
List of all existing containers.
docker stop <imageid>
Stop specific image
docker kill $(docker ps -q)
kill all running containers
docker rm $(docker ps -a -q)
delete all stopped containers
Docker rm <containerid>
Remove a container from registry
docker rmi $(docker images -q)
delete all images
Docker start/stop <containerid>
Run a container
Docker run
Download image, create container and start image.
docker create + docker start
docker run -t -i redis
Run image from container with terminal, give std in/out/error

Docker exec -it <container id> sh
Go into container and use terminal


Port Binding/forwarding in Docker for Redis

Lets take example of installing redis server in to container and connecting through redis-cli client out side of container.

1. We are binding 7000 to host computer to forward the request to container's port 6379, where redis is listening.

PS C:\Users\raj.kumar1> docker run --name my-redis -p 7000:6379 -d redis
ab8ef8348d65a9a7d4f08d933ed4f5ccc16b7ec08e7f476ce139b8b5afa268cf



2. Installing redis-cli without installing redis server on my client machine.

PS C:\Users\raj.kumar1> npm install -g redis-cli
[..................] / rollbackFailedOptional: verb npm-session 6b2a732498b4ad48


3. With redis-cli trying to connect with port 7000 on host client.
PS C:\Users\raj.kumar1> rdcli -h 0.0.0.0 -p 7000
0.0.0.0:7000>



4. Set and get value from redis server which is running inside container on port 6379.
0.0.0.0:7000> set name "Raj Kumar"
OK
0.0.0.0:7000> get name
Raj Kumar



5. Get redis container id by running docker ps command.

PS C:\Users\raj.kumar1> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
ab8ef8348d65        redis               "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        0.0.0.0:7000->6379/tcp   my-redis


6. Open a terminal inside container and verify that the same value persist that we set from out side.

PS C:\Users\raj.kumar1> Docker exec -it ab8ef8348d65 sh
# redis-cli
127.0.0.1:6379> get name
"Raj Kumar"



Docker Image Pull/Run/remove command

Docker pull microsoft/mssql-server-linux:2017-latest

Docker run --name demosqlserver \
-p 1431:1433 \
-e "ACCEPT_EULA=Y" \
-e "SA_PASSWORD=password1234" \
-d microsoft/mssql-server-linux:2017-latest

Docker rm - f demosqlserver

Process a docker-compose.yaml command

Build and Run container
docker-compose up --build

Run container in background
docker-compose up -d

Above command run all services in background that could be verified by
Docker-compose ps


Building block
Docker Pull
Described above

Docker Build
Docker build . -t rajkrs/myappimage:latest

Docker Login
To login into docker hub account

Docker Push
Docker push rajkrs/myappimage:latest



Sample Compose.yaml for asp.net core app with sql server

version: '3.4'

services:
  webapi:
    image: ${DOCKER_REGISTRY-}webapi
    build:
      context: .
      dockerfile: WebApi/Dockerfile
    depends_on:
      - db
  
  db:
    image: microsoft/mssql-server-linux:2017-latest
    container_name: webapisqldb
    environment:
        ACCEPT_EULA: Y
        SA_PASSWORD: "Bassword@123"
    ports:  
      - '1433:1433'  
    expose:  
      - 1433
    volumes:  
      - sql-data:/var/opt/mssql3  

volumes:
      sql-data:


Get complete example on GitHub