Container: Exploring Docker's Building Blocks - Docker objects

Docker container - commands and use cases


3 min read

Container: Exploring Docker's Building Blocks
 - Docker objects

In our last post, we discussed Docker images and how they serve as the essential building blocks of containerization, and how Docker images encapsulate an application along with its dependencies, providing consistency across various environments. As a continuation of our Docker journey, this write-up will focus on another important part of the Docker object: Docker containers.

Containers: Dynamic and Agile Application Environments

Think of containers as isolated, lightweight runtime environments for applications. They encapsulate the application, its runtime, system tools, libraries, and settings, providing an efficient and consistent way to deploy applications across various environments.

Containers are engineered for agility and portability. Since they bundle everything needed to run an application, from its dependencies to configuration files, developers can be confident that their code will run consistently across development, testing, and production environments. Containers are isolated from the host system and other containers, ensuring that changes made in one container do not affect others or the underlying host system.

Key Advantages of Containers:

  1. Resource Efficiency: Containers share the kernel of the host operating system, reducing overhead and allowing the deployment of multiple programs on the same hardware.

  2. Consistency: Containers ensure consistent behaviour across diverse environments, eliminating the "it works on my machine" issue.

  3. Rapid Deployment: Containers can be spun up and torn down quickly, making it easy to scale applications based on demand.

  4. Isolation: Containers provide isolation between applications, enhancing security and preventing conflicts.

  5. Version Control: Docker images and containers can be versioned, allowing easy rollback and updates.

Commonly used commands and their use case include.

  1. docker run:

    • Create and start a new container based on an image.

      docker run -it ubuntu:latest bash

  2. docker start:

    • Start a stopped container.

      docker start container_name

  3. docker stop:

    • Gracefully stop a running container.

      docker stop container_name

  4. docker restart:

    • Stop and then start a container.

      docker restart container_name

  5. docker pause:

    • Pause all processes in a running container.

      docker pause container_name

  6. docker unpause:

    • Unpause a paused container.

      docker unpause container_name

  7. docker kill:

    • Forcefully stop a running container.

      docker kill container_name

  8. docker exec:

    • Run a command inside a running container.

      docker exec -it container_name bash

  9. docker attach:

    • Attach to a running container's console.

      docker attach container_name

  10. docker logs:

    • Display logs from a container.

      docker logs container_name

  11. docker ps:

    • List all running containers.

      docker ps

  12. docker ps -a:

    • List all containers (both running and stopped).

      docker ps -a

  13. docker rm:

    • Remove a stopped container.

      docker rm container_name

  14. docker commit:

    • Create a new image from a container's changes.

      docker commit container_name new_image_name

  15. docker inspect:

    • Display detailed information about a container.

      docker inspect container_name

  16. docker stats:

    • Display real-time resource usage statistics for containers.

      docker stats container_name

  17. docker top:

    • Display the running processes in a container.

      docker top container_name

  18. docker rename:

    • Rename an existing container.

      docker rename old_container_name new_container_name

  19. docker cp:

    • Copy files or folders between a container and the local filesystem.

      docker cp container_name:/path/to/file local_path

These are just a subset of Docker container commands. Depending on your specific use case, you might find other commands relevant as well. Remember to use docker --help or consult the official Docker documentation for detailed information on each command's options and usage.

While Docker images serve as the blueprint, containers are the dynamic actors that execute the scripts. These components, when combined, constitute the foundation of Docker's containerization method, which is transforming software deployment and development practices. In future articles, we'll go further into another Docker object: Docker networks, Capabilities and how they enable seamless communication and collaboration among containers.