Skip to content

Latest commit

 

History

History
213 lines (155 loc) · 7 KB

docker.md

File metadata and controls

213 lines (155 loc) · 7 KB

Docker

Introduction to Docker

What is Docker?

  • Product/Company that made Container technology very popular
  • In classical Virtual Machines (VM), dach VM runs its own guest operating system
  • Containers reuse the host operating system
  • Container run in user space
  • Not a total replacement of classical hypervisors!
  • Editions
    • Community Edition (free)
    • Enterprise Edition
  • Learn more in Docker docs...

What is Docker?

Image Source and more information...

Advantages and Disadvantages

  • Advantages
    • Fast (boot time), small, and agile (e.g. Docker in Docker)
    • Portable
    • Immutable
  • Disadvantages
    • Linux on Linux and Windows on Windows, no mix (yet)
    • Security (less isolated)

Technical Components

  • Isolation layers
    • Filesystem - each container has its own filesystem (layered, copy-on-write)
    • Processes - each container has its own process environment
    • Network - separate virtual network interfaces
    • Resources - individually allocated CPUs, memory
  • Logging - STDOUT, STDERR, STDIN are logged for analysis purposes
  • Interactive shell - Pseudo-tty attached to STDIN

Installation

  • Windows and macOS: Docker for Windows/macOS is recommended
  • Runs natively on Linx servers
  • Connect to remote Docker engine
    • Set environment variables (e.g. DOCKER_HOST)
    • Read more...
    • E.g. export DOCKER_HOST=tcp://192.168.1.99:2376 to access Docker running on server with the specified IP address

Containers

  • Launched from images
    • Layered, copy-on-write
  • Contain one or more processes
    • Can be short-lived
    • Sometimes even to run just a single command
  • Shared via registries
    • Docker Hub (private and public repositories)
    • Run your own private registry

Containers

Operation Docker CLI command
Start a new container docker run
List running containers docker ps
Stop container docker stop
Start a stopped container docker start
Attach to a running container docker attach
Displays processes in container docker top
Run process in container docker exec
Remove container docker rm

Start Containers

  • Interactive container:
docker run --name helloDocker -i -t ubuntu /bin/bash
           |                  |  |  |      |
           |                  |  |  |      +-- Command to run
           |                  |  |  +--------- Base image
           |                  |  +------------ Attach pseudo-tty
           |                  +--------------- Keep STDIN open
           +---------------------------------- Name of container

Start Containers

  • Background process:
docker run -d ubuntu /bin/bash -c "while true; do echo hello world; sleep 1; done"
           |
           +---- Detached (=background server process)
  • Try running (run), stopping (stop), starting (start), listing (ps), removing (rm), etc. containers

Network Connections

docker run -d -p 8080:80 nginx
              |  |    |  |
              |  |    |  +-- nginx web server
              |  |    +----- Container port (nginx)
              |  +---------- Host port (your desktop/server)
              +------------- Port mapping
  • Try running your own nginx web server in Docker

Volume Mapping

docker run -it --rm -v C:\temp\htl-csharp-linq:/data ubuntu
               |    |  |                       |
               |    |  |                       +---- Folder in container
               |    |  +---------------------------- Folder on host (e.g. your desktop)
               |    +------------------------------- Volume mapping
               +------------------------------------ Remove container after exit
  • Don't forget to enable sharing if you use Docker for Windows:

Docker Images

  • Union-mount file system over the read-only file system
    • Multiple file systems stacked on top of each other
  • Only top-most file system is writable

Image Operations

Operation Docker CLI command
List images docker images
Search images docker search
Pull image from Docker Hub docker pull
Push image to Docker Hub docker push

Important Images

Images important for this course

Image Description
ubuntu Ubuntu Linux OS
node Base image for Node.js applications
nginx Webserver, reverse proxy, load balancer, etc.
microsoft/dotnet Base image for .NET Core applications
openjdk Base image for Java applications
couchdb CouchDB

Dockerfiles

  • Build images automatically based on instructions from a Dockerfile
  • Text file that contains all the commands needed to build a given image
  • Use docker build to generate image from Dockerfile
  • Read more...

Dockerfile Sample

FROM nginx
LABEL Author="Rainer Stropek"
ENV REFRESHED_AT 2018-01-01
RUN apt-get -qq update
COPY *.html /usr/share/nginx/html/
docker build -t staticweb .
docker run --name staticwebcontainer -d -p 80:80 staticweb