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
Docker Engine
To build and package apps as
Docker Images
Created using file written in
Format that run into
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

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 -p 7000>

4. Set and get value from redis server which is running inside container on port 6379.> set name "Raj Kumar"
OK> 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>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> 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 "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 core app with sql server

version: '3.4'

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


Get complete example on GitHub