Dive into Docker: A Comprehensive Exploration of the Basics

·

3 min read

Dive into Docker: A Comprehensive Exploration of the Basics

Introduction:

The use of Docker in contemporary software development is widespread, and for good reason. Docker helps developers build lightweight and portable software containers that simplify application deployment.

Docker Engine Architecture:

The Docker Engine is comprised of several components working together seamlessly. Let's delve into these components:

  1. dockerd (Daemon): The heart of the Docker Engine, responsible for managing Docker objects such as images, containers, networks, and volumes.

  2. docker containerd: Handles container lifecycle management, including the creation, execution, and deletion of containers.

  3. docker containerd-shim: A lightweight component that acts as an intermediary between the containerd and the container, facilitating better communication.

  4. docker run: The command to create and start a container.

Essential Docker Commands:

Managing Containers:

  • docker run -it ubuntu: Run an interactive Ubuntu container.

  • docker ps: Display information about running containers.

  • docker images: Show available images.

  • docker rmi <repository> -f: Remove a Docker image forcefully.

  • docker container exec #bashid bash: Run a shell in an existing container.

  • docker container ls: List all running containers.

  • docker ps -a: Show all containers (including stopped ones).

  • docker ps -a -q: Retrieve only container IDs.

  • docker rm #id: Remove a specific container.

  • docker rm $(docker ps -a -q): Remove all containers.

  • docker kill <container_name/ID>: Stop a running container.

  • docker stop #id: Stop a container.

  • docker start #id: Start a stopped container.

  • docker logs 9694: Display logs for a specific container.

  • docker --since 5s 9694: Show logs since a specific time.

  • docker inspect #id or ubuntu: Retrieve detailed information about an image or container.

  • docker container prune -f: Delete all stopped containers

Working with Docker Images

  • docker run alpine ping www.civo.com: Run a container and ping a website.

  • docker run -d alpine ping www.civo.com: Run a container in detached mode.

  • docker run ubuntu echo hey: Run a command in a container.

  • docker run -d nginx: Run Nginx in detached mode.

  • docker run -d -p 8080:80 nginx: Port forward and run Nginx on port 8080.

  • docker commit -m 'message' #id <imageName>:<version>: Create a new image with changes.

  • docker images -q: Retrieve image IDs.

  • docker rmi $(docker images -q): Remove all images.

Understanding Docker Layers

Docker uses a layered file system for images, enabling efficiency and optimization. Key points about Docker layers:

  • Every image comprises layers.

  • These layers prevent redundant downloads.

  • Docker layers optimize and accelerate image building and sharing.

To view image hash values:

  • docker images -q --no-trunc

Creating Docker Files

Creating a Dockerfile allows you to define custom images. Here's a basic example:

DockerfileCopy codeFROM ubuntu
RUN apt-get update
CMD ["echo", "Hello world"]

To create your image:

  • docker build -t my-hello-image:2.03 . // '.' represents current directory

This comprehensive guide should provide you with a solid foundation for working with Docker, from understanding its architecture to mastering essential commands and creating custom images. Experiment with these concepts, and you'll soon find yourself efficiently managing containers and deploying applications with Docker.