/container-notes

My notes on containers, docker, and K8s

Primary LanguageDockerfile

Containerization Notes

My notes on containers, docker, and K8s.

Docker Commands

Containers

A container is a runnable instance of an image. As you build your image, you deploy your application and dependencies. Then, multiple containers can be instantiated, each isolated from one another. Each container instance has its own filesystem, memory, and network interface.

Container Commands

  • docker ps shows running containers.

    • docker ps -a shows running and stopped containers.
  • docker container ls check if the container is running. The output tells us:

    • Which image the container is running; a short form of the container ID that Docker uniquely generates;
    • The container name that Docker will randomly assign unless we supply a name;
    • The command running in the container.
  • docker stats shows containers' resource usage statistics.

    • docker stats --all shows a running list of containers.
  • docker create creates a container but does not start it.

  • docker run creates and starts a container in one operation.

    • Normally, if you run a container without options, it will start and stop immediately.
    • If you want to keep it running, you can run docker run -it container_id --name friendly-name
      • -i will keep the STDIN open even if not attached.
      • -t will allocate a command line session (pseudo-TTY).
      • -d will detach the container from the current session and run it in the background.
      • --name will assign a friendly name to the container.
    • If you want to map a directory on the host to a docker container: docker run -v $HOSTDIR:$DOCKERDIR
    • If you want a transient container, docker run --rm will remove the container after it stops.
      • docker run --name yourname docker_image will allow you to start and stop a container by calling it with the name.
      • To create a new container from the "nginx" image and run it in the background with the --detach flag and port 80 published with the --publish flag, run:
        • docker container run --detach --publish 80:80 nginx:alpine
    • To get into a shell environment within a container (running or not), run:
      • dociet run -it my-container sh
  • docker rename allows the container to be renamed.

    • Example: docker rename old_name new_name
  • docker rm deletes a container.

    • If you want to remove the volumes associated with the container, the deletion of the container must include the -v switch.
  • docker update updates a container's resource limits.

  • docker start starts a container so it is running.

    • If you want to integrate a container with a host process manager, start the daemon with -r=false then use docker start -a.
  • docker stop stops a running container.

  • docker restart stops and starts a container.

  • docker pause pauses a running container, "freezing" it in place.

  • docker unpause will unpause a running container.

  • docker wait blocks until running container stops.

  • docker kill sends a SIGKILL to a running container.

  • docker attach will connect to a running container.

  • docker logs gets logs from container. (You can use a custom log driver, but logs is only available for json-file and journald in 1.10).

    • docker logs -f --tail 100 {ContainerId}
  • docker inspect looks at all the info on a container (including IP address).

  • docker events gets events from container.

  • docker port shows public facing port of container.

    • Example: docker port my-container
  • docker top shows running processes in container.

  • docker diff shows changed files in the container's FS.

  • docker cp copies files or folders between a container and the local filesystem.

  • docker export turns container filesystem into tarball archive stream to STDOUT.

  • docker exec to execute a command in a container.

    • To enter a running container, attach a new shell process to a running container called foo, use: docker exec -it foo /bin/bash

A Dockerfile is a file that defines a set of instructions that creates an image. Each instruction in the Dockerfile creates a layer in the image. For the most part, when you rebuild the image, only the layers that have changed are rebuilt. The Dockerfile can be distributed to others, allowing them to recreate a new image as you created it. While this allows you to distribute the instructions on creating the image, the primary way to distribute your image is to publish it to a registry.

  • Build an image based on a Dockerfile: docker build -t saeid-image:image-tag .
  • Run an instance of the image using docker run -d --name saeid-container -p 8080:8080 saeid-image:image-tag

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

Compose works in all environments: production, staging, development, testing, and CI workflows. It also has commands for managing the whole lifecycle of your application:

  • Start, stop, and rebuild services
  • View the status of running services
  • Stream the log output of running services
  • Run a one-off command on a service

Learn more on how to use Docker Compose.

  • Run docker compose up in the directory with a docker-compose.yml to run and build the package.
  • Run docker compose down in the directory with a docker-compose.yml to turn it off.

Images

  • An image is an ordered collection of filesystem changes that form the basis of a container.
  • The image doesn't have a state and is read-only.
  • Most of the time, an image is based on another image but with some customization.
  • Because containers are created from images, images have a set of run parameters (such as a starting executable) that run when the container starts.

Image Commands

Registries

  • A repository is a hosted collection of tagged images that create a container's file system.
  • A registry is a host (a server) that stores repositories and provides an HTTP API for managing the uploading and downloading of repositories.
  • Docker.com hosts its own index to a central registry which contains a large number of repositories.
  • The Microsoft Container Registry (MCR) is the official source of Microsoft-provided container images.
    • The MCR is built on Azure CDN to provide globally-replicated images.
    • The MCR does not have a public-facing website and the primary way to learn about Microsoft-provided container images is through the Microsoft Docker Hub pages.

Docker Image Repositories Command