Wednesday, February 15, 2023

Docker Cheat Sheet


----------------------------------------------------------------------
References
----------------------------------------------------------------------

Docker Engine -- runtime and ops https://docs.docker.com/engine/
Create Docker Containers -- build your own https://docs.docker.com/build/
Orchestrate Containers -- combine containers https://docs.docker.com/compose/

----------------------------------------------------------------------
Basic Docker - Overview
----------------------------------------------------------------------


Basic Docker Architecture

  • Image as container - Binary images of s/w systems published as Containers
  • Versioned - Containers are versioned
  • Decentralized - Your server downloads/stores containers and their parents...warning, this gets large!

Basic Docker Lifecycle

  • Pull containers to use or extend
  • Run containers and expose local resources to them
  • Extend existing containers to create custom run-anywhere binaries

----------------------------------------------------------------------
Basic Docker - Setup
----------------------------------------------------------------------


Install
sudo apt install docker.io
sudo apt install docker-compose


Running without sudo
Assign your user into the docker group. Add group if necessary.

sudo groupadd docker
sudo usermod -a -G docker myuser
# refresh groups or restart server


Other post-install tasks
See https://docs.docker.com/install/linux/linux-postinstall/

Configure - start on boot
On CentOS:
sudo systemctl [enable|disable] docker

----------------------------------------------------------------------
Docker service
----------------------------------------------------------------------

Starting the daemon
sudo service docker start

Start/Stop Docker Service
Debian:
sudo service docker start
CentOS:
sudo systemctl [start|stop] docker 

Test Docker Service
sudo docker run hello-world

----------------------------------------------------------------------
Managing Images
----------------------------------------------------------------------


List all Images
docker images

Image Info - i.e. How do I use this image?
docker image inspect <imageid>

Rename Image...by aliasing the image
docker tag <imageid> <alias>

Remove Image (untagging)
docker rmi <alias>

Remove Unused Images
docker image prune [-a]

Move docker image location off to another drive (for large files)

sudo service docker stop
ps faux|grep docker
sudo ls /var/lib/docker
mv /var/lib/docker <new location> 
sudo service docker start


----------------------------------------------------------------------
Running Containers
----------------------------------------------------------------------


See running containers
docker ps

Launch container from image
docker run -it [-d] [--rm] [--network <name>]  <image_id> 
where:
-d starts detached (daemon)
--rm removes on exit
--network network name to use (see `docker network ls`)

Retain image changes
docker commit <container_id> <group>/<alias>

Attach and execute shell
docker exec -it <container_id> bash

Build image from local Dockerfile
docker build . -t <alias>

Attach to running shell
docker attach <container_id>

Drop out of running shell - but leave running
ctl-p ctl-d

View container logging
docker logs <container_id>

----------------------------------------------------------------------
Creating Images
----------------------------------------------------------------------


Create a Dockerfile with a base image
Search DockerHub for a suitable base
FROM python:latest

Add some standard labels
LABEL org.opencontainers.image.title="Data Service" \
      org.opencontainers.image.version="0.0.1" \
      org.opencontainers.image.authors="aaa@baz.com,bbb@baz.com"

Add some content
ADD data-svc/ /home/python/data-svc/.

Open port for web service
EXPOSE 8080

Attach volumes for in/out of data
VOLUME ["/home/python/data", "/var/log/data-svc"]

Launch a service on container start
ENTRYPOINT ["flask", "--app", "data-svc", "run"]

Build the Docker Image
From the directory containing Dockerfile...
docker build -t <group>/<artifact>:<version> .

----------------------------------------------------------------------
Docker and Docker-Compose Runtime
----------------------------------------------------------------------

Start docker-compose containers
docker-compose up -d



















No comments:

Post a Comment