Docker Tips
Getting Started
Follow the instructions for installing a Docker environment or Install for Linux.
Run through the Docker 101 Tutorial.
See also Play with Docker
PostgreSQL
$ docker exec -it aa9ac73a1818 psql -U postgres
$ docker run -it --network ${NETWORK_NAME} nicolaka/netshoot
Installing
General instructions for installing the Docker Engine are at https://docs.docker.com/install/
If installing for Linux, click on the Linux
link in the left side-bar and
choose a distribution.
E.g. for the Debian distribution:
Follow the instructions for installing the Docker engine on Debian at https://docs.docker.com/install/linux/docker-ce/debian/
Install
docker-compose
following the instructions at https://docs.docker.com/compose/install/Follow the instructions at https://docs.docker.com/install/linux/linux-postinstall/ to enable running the Docker daemon on boot.
Installing with MacPorts
If you don't want to use Docker Desktop for Mac, or cannot as you are running a version of macOS older than 10.14, you can use Docker Machine to provision a Dockerized host running in [VirtualBox[(VirtualBox.html).
Firstly, download and install VirtualBox.
The install docker-machine
from MacPorts.
$ sudo port install docker-machine
Test docker-machine
:
$ docker-machine create -d virtualbox default
$ docker-machine ls
$ docker-machine env default
$ eval "$(docker-machine env default)"
$ docker run busybox echo hello world
$ docker-machine ip default
$ docker-machine ssh default
$ exit
To delete the VirtualBox instance:
Note: that this removes all images, volumes, containers etc. that were created in the docker virtual machine.
$ docker-machine stop
$ docker-machine rm default
$ docker-machine env -u
$ eval $(docker-machine env -u)
See docker/machine: Machine management for a container-centric world
for brief instructions on using, docker-machine. As Docker Machine has been deprecated, it is necessary to use the WaybackMachine to reference the documentation.
-- Frank Dean - 23 Apr 2021
Docker Machine Memory and Volume Size
To create the VirtualBox instance with a larger virtual memory and disk space than the defaults, specify the size in MB, E.g.
$ docker-machine create --help
$ docker-machine create --virtualbox-memory "4096" --virtualbox-disk-size "60000" \
--virtualbox-cpu-count "4" bigvm
See https://stackoverflow.com/questions/32485723/docker-increase-disk-space
-- Frank Dean - 17 Oct 2021
Debugging
Note: There are currently two build systems available. The original build and a new BuildKit. This section describes how to debug image builds with the original
build
system. Should you find that your system is defaulting to BuildKit, which does not show the image hashes during the build, see how to enable BuildKit builds, and reverse the instructions in order to disable BuildKit. See also GitHub—always display image hashes · Issue #1053, a closed issue that implies no resolution, but describes a good workaround for debugging in BuildKit usingRUN sleep infinity
and the Linux nsenter commandsudo nsenter -a -t $PID_OF_SLEEP sh
to attach a shell in the running process.-- Frank Dean - 30 May 2021
Run the build, keeping each build layer (--rm=false
) allowing layers to be
examined for debugging purposes:
$ docker build --rm=false --no-cache -t my-server .
Sending build context to Docker daemon 28.84MB
Step 1/6 : FROM node:buster
---> 3650b71dec5e
Step 2/6 : WORKDIR /app
---> Running in c9d979ce772c
---> fb780e2090ff
Step 3/6 : COPY package.json yarn.lock .yarn-offline-cache ./
---> a3efe90ca086
Step 4/6 : RUN yarn install --offline --production 2>&1 | tee build.log
---> Running in 4b2db88143c5
yarn install v1.22.0
[1/4] Resolving packages...
[2/4] Fetching packages...
error Can't make a request in offline mode ("https://registry.yarnpkg.com/@turf/bbox/-/bbox-5.1.5.tgz")
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
---> 7596aadaace0
Step 5/6 : COPY . .
---> 1ea57397ccc9
Step 6/6 : CMD ["node", "/app/index.js"]
---> Running in fa82b9b5f26c
---> cda10404c0ad
Successfully built cda10404c0ad
Successfully tagged my-server:latest
Run the Docker layer that either preceeded the error to run the command interactively or choose the layer where the error occurred as in this example:
node:buster
is a Debian based image and has bash
so we can invoke Bash as
an interactive shell behaving as though we had logged in:
$ docker run --rm -it 7596aadaace0 bash -il
You can now examine the state of the layer after the command failed.
See also How can I inspect the file system of a failed docker build?
Image Layers
View each of the layers making up an image, including their sizes:
$ docker image history --no-trunc ${IMAGE_ID}
Reclaiming Disk Space
You can remove all redundant containers and dangling images with:
$ docker container prune
$ docker image prune
$ docker container ls --all
$ docker image ls
See also docker system prune --help
See Prune unused Docker objects
For an aggressive cleanup, on a Debian 10 (Buster) system, Docker stores all
its data under /var/lib/docker
. If the folders under there are deleted,
all Docker data, containers, images and volumes are destroyed. The
folders are re-created by Docker when required.
See How to clean up Docker for more details.
In the event that you really still need to clean out all the Docker data:
Stop the Docker daemon
$ sudo systemctl stop docker.service
Delete all the Docker data, which destroys all containers, images and volumes etc
$ sudo su --command='rm -rf /var/lib/docker/*'
Start the Docker daemon
$ sudo systemctl start docker.service
Backing up volumes
https://docs.docker.com/storage/volumes/#back-up-restore-or-migrate-data-volumes
Swarm/Stack
The following commands set up a swarm on the local machine, starts a local
registry and deploys an applicaton with an appropriate docker-compose.yml
file.
See Docker docs-Deploy a stack to a swarm for full details.
Note: The image
attribute in the docker-compose-swarm.yml
configuration should begin with 127.0.0.1:5000/
in order to be pushed to
the local registry.
Initalise the swarm, run a local registry and deploy the stack:
$ docker swarm init
$ docker swarm join-token worker
$ docker service create --name registry --publish published=5000,target=5000 registry:2
$ docker-compose -f docker-compose-swarm.yml build
$ docker-compose -f docker-compose-swarm.yml push
$ curl -X GET http://127.0.0.1:5000/v2/_catalog
$ curl -X GET http://127.0.0.1:5000/v2/${IMAGE_NAME}/tags/list
$ docker stack deploy --compose-file docker-compose-swarm.yml ${STACK_NAME}
$ docker stack ls
$ docker stack ps ${STACK_NAME}
$ docker service ls
$ docker service logs ${SERVICE_NAME}
$ docker stats
Shut everything down:
$ docker stack rm ${STACK_NAME}
$ docker service rm registry
$ docker swarm leave --force
Optionally, if retention of data is not desired, remove the appropriate docker volume:
$ docker volume ls
$ docker volume rm ${VOLUME_NAME}
- https://hub.docker.com/_/registry
- https://docs.docker.com/engine/reference/commandline/service_create/
- https://docs.docker.com/engine/reference/commandline/stack_deploy/
- https://github.com/docker/hub-feedback/issues/1222#issuecomment-591599629
Secrets
$ openssl rand -hex 16 | docker secret create postgres_password -
$ docker secret ls
Emacs Mode
docker-file
and docker-compose-mode
can be installed using MELPA. See
EmacsTips for help on using MELPA.
References
- Docker
- Docker Hub
- Docker Machine
- Docker Machine Docs
- Play with Docker Classroom
- Dockerfile Reference Documentation
- docker-node/BestPractices.md at master · nodejs/docker-node
- Containerizing a Node.js Application for Development With Compose | DigitalOcean
- Problem on yarn install with node docker image
- Introducing Docker Secrets Management
- How To Share Data between Docker Containers
- Your secret's safe with me: Securing container secrets with Vault
- https://github.com/kubernetes/minikube
- Docker - Docker Reference Architecture: Docker Logging Design and Best Practices
-- Frank Dean - 29 Mar 2020
Related Topics: InstallingMacPorts, NodeJS, PodmanTips, VirtualBox