meta data for this page
  •  

This is an old revision of the document!


  • Docker image - operating system with preconfigured application (service)
  • Docker container - running instance created from docker image
  • Data volume - persistent storage of data outside of container. Can be shared between containers.
  • Dockerfile - is a recipe which describes the files, environment, and commands that make up an image.
  • docker-compose - tool for defining and running multi-container Docker application (e.g. web app + mysql db). Compose preserves all volumes used by your services. When docker-compose up runs, if it finds any containers from previous runs, it copies the volumes from the old container to the new container. This process ensures that any data you’ve created in volumes isn’t lost.

command line

run

Create new container based on image and execute optional command inside.

docker run -i --rm busybox
docker run -i --rm busybox ps aux
docker run -i --rm debian:jessie-slim
docker run -i --rm nginx bash
  • docker run - run new isolated container …
    • -rm to Automatically remove the container when it exits
    • –restart always
    • –name
docker run --rm -ti -v `pwd`:/opt/myservice remote/path
--rm                            Automatically remove the container when it exits
-i, --interactive               Keep STDIN open even if not attached
-t, --tty                       Allocate a pseudo-TTY
-v, --volume=[]                 Bind mount a volume

stopping

docker stop sends SIGTERM to PID1 and waits 10 seconds before force kill SIGKILL.

docker stop ----time=30 foo
docker kill ----signal=SIGWINCH apache
docker kill ----signal=SIGQUIT nginx

More on handling signals https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/

restart policy

docker update --restart=always 5ba1f7f3d67e
# or usign container name
docker update --restart=always portainer

GUIs

https://blog.ouseful.info/2015/08/10/seven-graphical-interfaces-to-docker/

Usefull:

Working:

Not working:

Other managers

Volumes

https://docs.docker.com/engine/tutorials/dockervolumes/

Data volumes are designed to persist data, independent of the container’s life cycle. Docker therefore never automatically delete volumes when you remove a container, nor will it “garbage collect”     volumes that are no longer referenced by a container.
A Docker data volume persists after a container is deleted.

Volumes types:

  • local storage (original image data located in specified directory are copied to volume during creation)
  • bind-mounted host (original image data are not copied)
  • volume plugins

Volume destination inside container must be a absolute path.

Single file can be mounted as volume:

docker run --rm -it -v ~/.bash_history:/root/.bash_history debian:jessie-slim bash /bin/bash

Create named volume and share it between multiple containers:

docker run -d -P -v my-named-volume:/opt --name test1 debian:jessie-slim bash
docker run -d -P -v my-named-volume:/opt --name test2 debian:jessie-slim bash
docker run -d -P -v my-named-volume:/opt --name test3 debian:jessie-slim bash

To protect data from being deleted with volume use local-persist plugin: https://github.com/CWSpear/local-persist

Find orphaned volumes

docker volume ls -f dangling=true
docker volume rm <volume name>

Network

data persistence in swarm

Backup

No universal backup solution. Possible scenarios:

  • docker images - use docker save
  • running container:
    • pause (but what with not flushed data?)
    • commit container as image (volumes are not included!)
    • backup a image using save
  • backup data volumes
    • gracefully stop container to ensure all data are flushed
    • run new container only to execute backup script on other container's volume –volumes-from
  • connect remotely to service to get dump (i.e. mysql)
  • configure service to make daily backup to bind mounted host directory

by committing state to images

To make backup of running container it is need to commit its current state and save as docker image. With option -p container will be paused before saving snapshot.

# docker commit -p portainer portainer1
sha256:c48af304eed09c0ef7f557e6f5e02f10b2637c4c02dd765c186ee29805c31272
 
# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE
portainer1              latest              c48af304eed0        About a minute ago   9.16 MB

Now image can be pushed to remote hub or registry. See man docker push Or dumped to file:

docker save -o portainer1.tar portainer1

To load dump file check man docker load

run backup on existing volume

To get data from container outside:

$ sudo docker run --rm --volumes-from dbdata -v $(pwd):/backup busybox tar cvf /backup/backup.tar /dbdata

exporting filesystem

export exports only container filesystem and has some limitations: it won't export the data volume (VOLUME in Dockerfile or specified by -v)

docker export $CONTAINER_ID > $CONTAINER_ID-backup.tar
docker import - slava/$CONTAINER_ID-backup < $CONTAINER_ID-backup.tar

Build image

Create build directory and Dockerfile

Dockerfile
FROM debian:jessie-slim
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay

docker build -t mydocker . docker run mydocker

ns