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.
Create and start a new container based on an image.
docker run -it ubuntu:latest bash
Start a stopped container.
docker start container_name
Gracefully stop a running container.
docker stop container_name
Stop and then start a container.
docker restart container_name
Pause all processes in a running container.
docker pause container_name
Unpause a paused container.
docker unpause container_name
Forcefully stop a running container.
docker kill container_name
Run a command inside a running container.
docker exec -it container_name bash
Attach to a running container's console.
docker attach container_name
Display logs from a container.
docker logs container_name
List all running containers.
docker ps -a:
List all containers (both running and stopped).
docker ps -a
Remove a stopped container.
docker rm container_name
Create a new image from a container's changes.
docker commit container_name new_image_name
Display detailed information about a container.
docker inspect container_name
Display real-time resource usage statistics for containers.
docker stats container_name
Display the running processes in a container.
docker top container_name
Rename an existing container.
docker rename old_container_name new_container_name
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.