Docker Tips

Getting Started

  1. Follow the instructions for installing a Docker environment or Install for Linux.

  2. 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:

  1. Follow the instructions for installing the Docker engine on Debian at https://docs.docker.com/install/linux/docker-ce/debian/

  2. Install docker-compose following the instructions at https://docs.docker.com/compose/install/

  3. Follow the instructions at https://docs.docker.com/install/linux/linux-postinstall/ to enable running the Docker daemon on boot.

Debugging

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 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:

  1. Stop the Docker daemon

    $ sudo systemctl stop docker.service
    
  2. Delete all the Docker data, which destroys all containers, images and volumes etc

    $ sudo su --command='rm -rf /var/lib/docker/*'
    
  3. Start the Docker daemon

    $ sudo systemctl start docker.service
    

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}

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

Related Topics: NodeJS