Introduction to 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...
Image Source and more information...
- 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)
- 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
- 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
- Set environment variables (e.g.
- 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
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 |
- 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
- 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
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
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
- 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
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 |
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 |
- 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...
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