Image vs container

  • Docker image: is a template, a blueprint of a filesystem with application and dependencies installed. It is read-only.
  • Docker container: is a running instance of a docker image. It is a lightweight, isolated

Quick commands

# List docker images
    docker images

# List running containers
    docker container ps

# Run container.
    mkdir -p ./storage/openwritings.net
    docker run -p 9000:8080 --user root:root -v ./storage/openwritings.net:/storage dokuwiki/dokuwiki:stable
    # Open with your browser: http://localhost:9000/install.php

# See how docker was build, dockerfile.
    docker image history --no-trunc <IMAGE ID>

# DEBUG: Get in the shell of the container.
    docker exec -it fin-D8.9.20 sh
        docker container run -it <CONTAINER> sh

# DEBUG: Execute commands in shell: https://docs.docker.com/reference/cli/docker/container/exec/#description
    docker exec -it my_container sh -c "echo a && echo b"
    # Run container and execute commands
        docker run -it <IMAGE ID> /bin/sh -c "cd /app; du -ch -d 1"

Build docker

# Dockerfile
    FROM amd64/alpine:3.14
    CMD ["date"]

# Build Dockerfile with name=hello-date, tag=latest.
    docker build --tag hello-date:latest .
    docker build --tag hello-date:latest -f Dockerfile .

# Run container created from image, hello-date.
    docker run hello-date

# Run it and get in the shell
    docker run -it hello-date /bin/sh

Cleanup

# Delete container
    docker rm -f <container_id_or_name>

# Delete dangling images
    docker rmi $(docker images -f "dangling=true" -q) || true

# Removes **every image** on your system that is not currently being used by a container.
    docker system prune -a

Image sizes

# Display all images with sizes
    docker images

# Check Size of a Specific Image's Layers
    docker history <image_name_or_id>

Debug

# Overriding the Entrypoint to ls
docker run --rm --entrypoint ls stable-ts-app -ahl /root/.cache
    docker run --rm --entrypoint du stable-ts-app -h /root/.cache/pip
    docker run --rm --entrypoint pip stable-ts-app cache info

    docker run --rm --entrypoint du stable-ts-app -h /lib

    # List directories size
    docker run --rm --entrypoint /bin/sh stable-ts-app -c "du -h --max-depth=1 /usr | sort -rh"
    docker run --rm --entrypoint /bin/sh stable-ts-app -c "du -h --max-depth=1 /usr/local/lib/python3.10/site-packages | sort -rh"

    # Find large files: 125MB+. Note: Ensure that you don´t run "docker --rm" to keep the container alive for inspection.
    docker run --rm --entrypoint /bin/sh stable-ts-app -c "find / -type f -size +125M -printf '%s %p\n' | sort -k1,1n | numfmt --field=1 --to=iec-i  --format='%10f' --suffix B" | tee big-files.txt

Tips

  • The --rm flag is incompatible with --restart. In production, you typically want a policy like --restart always or unless-stopped so your Next.js server automatically comes back online if the server reboots or the process crashes.