Container: Exploring Docker's Building Blocks - Docker objects
Docker container - commands and use cases
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:
Resource Efficiency: Containers share the kernel of the host operating system, reducing overhead and allowing the deployment of multiple programs on the same hardware.
Consistency: Containers ensure consistent behaviour across diverse environments, eliminating the "it works on my machine" issue.
Rapid Deployment: Containers can be spun up and torn down quickly, making it easy to scale applications based on demand.
Isolation: Containers provide isolation between applications, enhancing security and preventing conflicts.
Version Control: Docker images and containers can be versioned, allowing easy rollback and updates.
Commonly used commands and their use case include.
docker run:
Create and start a new container based on an image.
docker run -it ubuntu:latest bash
docker start:
Start a stopped container.
docker start container_name
docker stop:
Gracefully stop a running container.
docker stop container_name
docker restart:
Stop and then start a container.
docker restart container_name
docker pause:
Pause all processes in a running container.
docker pause container_name
docker unpause:
Unpause a paused container.
docker unpause container_name
docker kill:
Forcefully stop a running container.
docker kill container_name
docker exec:
Run a command inside a running container.
docker exec -it container_name bash
docker attach:
Attach to a running container's console.
docker attach container_name
docker logs:
Display logs from a container.
docker logs container_name
docker ps:
List all running containers.
docker ps
docker ps -a:
List all containers (both running and stopped).
docker ps -a
docker rm:
Remove a stopped container.
docker rm container_name
docker commit:
Create a new image from a container's changes.
docker commit container_name new_image_name
docker inspect:
Display detailed information about a container.
docker inspect container_name
docker stats:
Display real-time resource usage statistics for containers.
docker stats container_name
docker top:
Display the running processes in a container.
docker top container_name
docker rename:
Rename an existing container.
docker rename old_container_name new_container_name
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.