Docker: What Is It?
Tom Most
NBLUG 2019-02-12
Tom Most
NBLUG 2019-02-12
What is a container? ∙ Docker basics ∙ Use cases
Two Linux features:
namespaces | + | control groups |
---|---|---|
isolation | resource limits | |
hostname | CPU | |
network | memory | |
processes | I/O | |
filesystem | … | |
… |
Usually also paired with virtual network devices
Containers that resemble a virtual machine
init
process$ docker run -it ubuntu:xenial bash
Unable to find image 'ubuntu:xenial' locally
xenial: Pulling from library/ubuntu
✂
7ea47a67709e: Pull complete
Digest: sha256:e4a134999bea4abb4a27bc437e6118fdddfb172e1b9d683129b74d254af51675
Status: Downloaded newer image for ubuntu:xenial
root@ef57b341461f:/# hostname
ef57b341461f
root@ef57b341461f:/# ps fax
PID TTY STAT TIME COMMAND
1 pts/0 Ss 0:00 bash
17 pts/0 R+ 0:00 ps fax
Docker Engine adds some opinions:
/var/lib/docker.sock
UNIX socket, accessible to the docker
groupcontainerd
/runc
)docker
CLI toolBuilding and running containers
FROM ubuntu:bionic
RUN apt-get update -qqq && apt-get install memcached -yqq
EXPOSE 11211/tcp
USER memcache:memcache
CMD ["memcached", "-p", "11211", "--memory-limit", "128"]
Build with docker build
$ docker build -t memcache .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM ubuntu:bionic
---> 47b19964fb50
Step 2/5 : RUN apt-get update -qqq && apt-get install memcached -yqq
---> Running in 9a1269bf9bbf
✂
Unpacking memcached (1.5.6-0ubuntu1) ...
✂
Removing intermediate container 9a1269bf9bbf
---> 1ef9a4caf1c1
Step 3/5 : EXPOSE 11211/tcp
---> Running in 2070f6abcada
Removing intermediate container 2070f6abcada
---> ccc1d5c153a6
Step 4/5 : USER memcache:memcache
---> Running in 485ab2cd2c0a
Removing intermediate container 485ab2cd2c0a
---> 88bafee3e318
Step 5/5 : CMD ["memcached", "-p", "11211", "--memory-limit", "128"]
---> Running in 4e68567f195e
Removing intermediate container 4e68567f195e
---> a107a3c11dc3
Successfully built a107a3c11dc3
Successfully tagged memcache:latest
Caching!
$ docker build -t memcache .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM ubuntu:bionic
---> 47b19964fb50
Step 2/5 : RUN apt-get update -qqq && apt-get install memcached -yqq
---> Using cache
---> 1ef9a4caf1c1
Step 3/5 : EXPOSE 11211/tcp
---> Using cache
---> ccc1d5c153a6
Step 4/5 : USER memcache:memcache
---> Using cache
---> 88bafee3e318
Step 5/5 : CMD ["memcached", "-p", "11211", "--memory-limit", "128"]
---> Using cache
---> a107a3c11dc3
Successfully built a107a3c11dc3
Successfully tagged memcache:latest
$ docker run --publish 11211:11211 --detach memcache:latest
967cfd35f00e3ec2ce42d3d701a6bf08a3886a82ac310c310bb4446ab7fb3cc2
--publish
forwards ports from the host
--detach
runs in the background
$ docker ps
CONTAINER ID IMAGE COMMAND ✂ NAMES
967cfd35f00e memcache:latest "memcached -p 112…" ✂ romantic_jepsen
$ docker exec -it romantic_jepsen bash
memcache@967cfd35f00e:/$ ps fax
PID TTY STAT TIME COMMAND
18 pts/0 Ss 0:00 bash
27 pts/0 R+ 0:00 \_ ps fax
1 ? Ssl 0:00 memcached -p 11211 --memory-limit 128
FROM ubuntu:bionic
RUN apt-get update -qqq && apt-get install memcached -yqq
EXPOSE 11211/tcp
USER memcache:memcache
CMD ["memcached", "-p", "11211", "--memory-limit", "128"]
FROM
— base imageCOPY
and ADD
— add filesRUN
— run a commandWORKDIR
, ENV
, ARG
, and SHELL
FROM ubuntu:bionic
RUN apt-get update -qqq && apt-get install memcached -yqq
EXPOSE 11211/tcp
USER memcache:memcache
CMD ["memcached", "-p", "11211", "--memory-limit", "128"]
CMD
and ENTRYPOINT
— command to runUSER
— user/group when container is runEXPOSE
— port numbersLABEL
— key/value metadataVOLUME
, STOPSIGNAL
, HEALTHCHECK
Running Picky Software ∙ Building ∙ Testing ∙ Distribution ∙ Deployment
Buiding in a container can improve repeatability of builds
manylinux1
wheels: CentOS 5Similar benefits to building:
You can distribute software as a Docker image
Similar to systemd’s portable services concept
* Normal microservice caveats apply
Docker Engine and docker
CLI tool — the software we’re discussing.
Moby — open source project, basis of the above. Formerly called “Docker”.
Docker, Inc. — a San Francisco startup, owner of docker.com and the Docker trademarks. Formerly called dotCloud.
Docker Hub — hub.docker.com, a software repository run by Docker, Inc.
Docker Swarm, Docker Enterprise, Docker Desktop — other Docker, Inc. products, not discussed here.