Thursday, 4 July 2019

Basic Docker Commands


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


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

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