Docker Quick Reference

Some quick reference notes.

 

Docker Command Examples

// Run a docker images
docker run -p 8080:3000 -v $(pwd):/var/www -w "/var/www" node npm start
docker inspect mycontainer
...
C:\Projects\docker> docker run -it -p 8080:80 -v ${pwd}:/app -w "/app" microsoft/dotnet:2.1-sdk /bin/bash
root@a00327ed64c0:/app# dotnet restore


// Docker EXEC Interactive bash
[lee@macbook:/c/project/]$ docker exec -it f3ce68 bash

// Alias for quickly shutting down and removing containers:
alias dockerstop='docker rm -f $(docker ps -aq)'

// Remove an image
[lee@macbook:/c/project/]]$ docker rmi myproject/node
Untagged: myproject/node:latest
Deleted: sha256:b2f44f3c730c8eb638b963e67e540f407808c10eef550442ef25c07b211e72fa
Deleted: sha256:605aa5a62611536eed51d667547d3740f0234ff9ded1f7a32d24fcd31ea7c4c

// Grep for images containing "eshop"
[lee@macbook:/c/project/]$ docker images -a | grep eshop
eshop/payment.api                latest                          3c41567e0d22        3 months ago        258MB
eshop/ordering.signalrhub        latest                          b7ce42a48f40        3 months ago        259MB
eshop/mobileshoppingagg          latest                          a7db9551abd5        3 months ago        260MB
eshop/webshoppingagg             latest                          5941dfb59d0d        3 months ago        258MB

// Grep for image ids containing "eshop"
[lee@macbook:/c/project/]$ docker images -a | grep eshop | awk '{print $3}'
3c41567e0d22
b7ce42a48f40
a7db9551abd5

// Based on the ids retreived above, we can pipe it to xargs to remove those images
[lee@macbook:/c/project/]$ docker images -a | grep eshop | awk '{print $3}' | xargs docker rmi
Untagged: eshop/payment.api:latest
Deleted: sha256:3c41567e0d22b9b1361900668688ed1efa71623768ba594cac0ade36645a4450
Deleted: sha256:a0656a2b6fb5e2669aa77b5f64ce204cecbe958d56d3c3f7b6a8fe622e6dc889
Deleted: sha256:72dd021efd8d164f923958119ea7bb2e5fb25c6fb0f22eac8600f94b1578c85f
...

// A shortcut for deleting untagged or dangling images
[lee@macbook:/c/project/]$ docker rmi -f $(docker images -f "dangling=true" -q)

// An alias for the above command
alias dockerrmiempty='docker rmi -f $(docker images -f "dangling=true" -q)'



// Some docker-compose command examples
docker-compose up --no-deps node
docker-compose up -d
docker-compose start
docker-compose stop // stops the containers
docker-compose down // stops and removes the containers
docker-compose down --rmi all --volumes // remove all images and volumes
docker-compose logs
docker-compose ps
docker-compose rm
docker-compose config // shows full config combined with .env file


// Docker commands for registry
solidfish@mac:~/$ docker images
REPOSITORY                                           TAG                 IMAGE ID            CREATED             SIZE
ImageName   latest              c2c790df2fa7        3 seconds ago       200MB

// Login to registry
docker login hub.docker.com
Username: solidfish
Password: 
Login Succeeded

// Commit a container as image
solidfish@mac:~/$ docker commit abc123ContainerId ImageName:Tag

// Push an image to registry
docker push hub.docker.com/solidfish/myapp
The push refers to repository [hub.docker.com/solidfish/myapp]
0e0fc55644a0: Preparing 
e0cdd61c5330: Preparing 
94ff001d9154: Preparing 
bdec0ea39ac3: Preparing 
3611fca35e9c: Preparing 
b533ba42c703: Waiting 
2a80d4780eec: Waiting 
fa9e51cb1f75: Waiting
...

 

 

Docker Environment Setup

 

Follow the architecture shown in the eshopContainers app:

  • Have single “.env” file in each environment
  • Have multiple docker-compose files for each environment. CI/CD tool will deploy to environment using the correct file
  • User “.override” files for those configurations that must go into environment. This is useful to use in Development environments

https://github.com/dotnet-architecture/eShopOnContainers

 

 

Docker Commands

 

Command Description
docker attach Attach local standard input, output, and error streams to a running container
docker build Build an image from a Dockerfile
docker checkpoint Manage checkpoints
docker commit Create a new image from a container’s changes
docker config Manage Docker configs
docker container Manage containers
docker cp Copy files/folders between a container and the local filesystem
docker create Create a new container
docker deploy Deploy a new stack or update an existing stack
docker diff Inspect changes to files or directories on a container’s filesystem
docker events Get real time events from the server
docker exec Run a command in a running container
docker export Export a container’s filesystem as a tar archive
docker history Show the history of an image
docker image Manage images
docker images List images
docker import Import the contents from a tarball to create a filesystem image
docker info Display system-wide information
docker inspect Return low-level information on Docker objects
docker kill Kill one or more running containers
docker load Load an image from a tar archive or STDIN
docker login Log in to a Docker registry
docker logout Log out from a Docker registry
docker logs Fetch the logs of a container
docker manifest Manage Docker image manifests and manifest lists
docker network Manage networks
docker node Manage Swarm nodes
docker pause Pause all processes within one or more containers
docker plugin Manage plugins
docker port List port mappings or a specific mapping for the container
docker ps List containers
docker pull Pull an image or a repository from a registry
docker push Push an image or a repository to a registry
docker rename Rename a container
docker restart Restart one or more containers
docker rm Remove one or more containers
docker rmi Remove one or more images
docker run Run a command in a new container
docker save Save one or more images to a tar archive (streamed to STDOUT by default)
docker search Search the Docker Hub for images
docker secret Manage Docker secrets
docker service Manage services
docker stack Manage Docker stacks
docker start Start one or more stopped containers
docker stats Display a live stream of container(s) resource usage statistics
docker stop Stop one or more running containers
docker swarm Manage Swarm
docker system Manage Docker
docker tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker top Display the running processes of a container
docker trust Manage trust on Docker images
docker unpause Unpause all processes within one or more containers
docker update Update configuration of one or more containers
docker version Show the Docker version information
docker volume Manage volumes
docker wait Block until one or more containers stop, then print their exit codes

https://docs.docker.com/engine/reference/commandline/docker/#child-commands

 

Docker Compose Commands

 

List of common commands for docker-compose can be seen with the ‘-h’ help option:

  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

https://docs.docker.com/compose/reference/overview/#command-options-overview-and-help

 

Docker Compose Build

From: https://stackoverflow.com/questions/50230399/what-is-the-difference-between-docker-compose-build-and-docker-build

docker-compose is a wrapper around the docker CLI in order to gain time and avoid 500 characters-long lines (and also start multiple containers at the same time). It uses a file called docker-compose.yml in order to retrieve parameters.

You can find the reference for the docker-compose file format here.

So basically docker-compose build will read your docker-compose.yml, look for all services containing the build: statement and run a docker build for each one.

Each build: can specify a Dockerfile, a context and args to pass to docker.

To conclude with an example docker-compose.yml file :

version: '3.2'

services:
  database:
    image: mariadb
    restart: always
    volumes:
      - ./.data/sql:/var/lib/mysql

  web:
    build:
      dockerfile: Dockerfile-alpine
      context: ./web
    ports:
      - 8099:80
    depends_on:
      - database 

When calling docker-compose build, only the web target will need an image to be built. The docker build command would look like :

docker build -t web_myproject -f Dockerfile-alpine ./web

 

Docker Compose Build vs Image

Tip from: https://nickjanetakis.com/blog/docker-tip-57-using-build-and-image-in-the-same-docker-compose-service

If you add build: "." to a service that will build a Docker image out of the Dockerfilethat exists in the directory when you build or up your compose file.

If you add image: "postgres:10.3-alpine" to a service that will pull down that image from the Docker Hub when you pull or up your compose file.

web:
  build: "."
  image: "source/imagename:1.0"

The above will build based on the current folder’s Dockerfile and the resulting image of that build will be named with what was set in the “image” field above.

 

Setting up Docker with TFS

Docker EE can be installed on Windows 2016 and Windows 2019 Servers. They have a joint license agreement for the forseeable future. Instructions from Docker on installing the environment:

https://docs.docker.com/install/windows/docker-ee/

Insturctions from Microsoft seem to work better. That is here:

https://docs.microsoft.com/en-us/virtualization/windowscontainers/quick-start/quick-start-windows-server

 

Once Docker is installed, if we want to run docker-compose commands we must install that separately. That is described here:

https://docs.docker.com/compose/install/

 

After installing the compose I was still having some errors on my Windows 2016 Server. This was due to the Docker Engine and how it’s configured to run inside a Windows Server master.Docker Engine is now running as a Windows service, listening on the default Docker named pipe. For development VMs running (for example) in a Hyper-V VM on Windows 10, it might be advantageous to make the Docker Engine running in the Windows Server 2016 VM available to the Windows 10 host:

# Open firewall port 2375
netsh advfirewall firewall add rule name="docker engine" dir=in action=allow protocol=TCP localport=2375

# Configure Docker daemon to listen on both pipe and TCP (replaces docker --register-service invocation above)
Stop-Service docker
dockerd --unregister-service
dockerd -H npipe:// -H 0.0.0.0:2375 --register-service
Start-Service docker

The Windows Server 2016 Docker engine can now be used from the VM host by setting

DOCKER_HOST: $env:DOCKER_HOST = “<ip-address-of-vm>:2375”

Need Windows Server version 1709 or greater for supporting non-Windows based containers.

 

 

 

 

 

References

Docker ARG vs ENV
https://vsupalov.com/docker-arg-vs-env/

Microservice application using .Net and Docker
https://github.com/dotnet-architecture/eShopOnContainers

Commit containers to an Image
https://www.techrepublic.com/article/how-to-commit-changes-to-a-docker-image/

CI/CD TFS using Docker
https://www.youtube.com/watch?v=yNESSInONyk&t=922s
https://www.youtube.com/watch?v=d_kHKGiI7lU&t=135s

TFS and Docker
https://blog.docker.com/2016/09/build-your-first-docker-windows-server-container/
https://www.b2-4ac.com/lcow-linux-containers-on-windows-server/
https://blog.docker.com/2017/09/docker-windows-server-1709/