Docker Containers
Synopsis
Docker is a powerful platform for developing, shipping, and running applications in isolated environments called containers. This write-up provides an overview of Docker, including basic instructions and a use case.
What is Docker
Docker is an open-source platform designed to automate the deployment of applications inside lightweight, portable containers. These containers package an application and its dependencies together, ensuring that it runs consistently across various environments. Docker containers are isolated from each other and the host system, making them ideal for microservices architectures, continuous integration/continuous deployment (CI/CD), and development workflows.
Docker Image
A Docker image is a lightweight, stand-alone, and executable software package that includes everything needed to run a piece of software, including the code, runtime, libraries, environment variables, and configuration files. It is a template for creating Docker containers.
Immutable: Once an image is created, it does not change. Any modifications require creating a new image.
Layered: Docker images are built in layers. Each layer represents a set of filesystem changes, and images can share layers to save space and speed up builds.
Storage: Images are stored in Docker registries like Docker Hub, from where they can be pulled to any Docker-enabled machine.
Docker Container
A Docker container is a runnable instance of a Docker image. It is the operational element that executes the software defined in the image.
Mutable: Containers can have a writable layer on top of the image’s read-only layers, allowing for changes while the container is running.
Isolated: Containers provide isolated environments for applications, ensuring that each container runs in its own separate namespace.
Lifecycle: Containers have a lifecycle that includes states like running, stopped, and paused. They can be started, stopped, restarted, and removed independently of each other.
Instructions:
Step 1: Installing Docker
1.1 Ubuntu
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
1.2 Windows
Download and install Docker Desktop from the official Docker website.
Step 2: Basic Commands:
2.1 Run a Docker Container:
sudo docker run hello-world
2.2 List Running Containers (use “docker ps -a” to list all running and stopped):
sudo docker ps
2.3 Stop a Running Container:
sudo docker stop <container_id>
2.4 Remove a Container:
sudo docker rm <container_id>
2.5 List Images:
sudo docker images
2.6 Remove an Image:
Note: To remove an image, you first have to stop and remove the container that’s using the image.
sudo docker rmi <image_id>
Use Case: Starting an Ubuntu Container with Docker
Step 1: Pull the Ubuntu Image:
1.1 To ensure you have the latest Ubuntu image, pull it from Docker Hub.
sudo docker pull ubuntu:latest
Step: 2 Run the Ubuntu container:
2.1 Start a new container from the Ubuntu image. This command starts a container and opens an interactive terminal session.
sudo docker run -it ubuntu:latest
The “-it” flags are used to run the container in interactive mode with a terminal session.
Step 3: Using the Ubuntu Container:
3.1 Once the container is running, you will have access to an Ubuntu shell where you can run commands as if you were on a regular Ubuntu system.
You can install packages, run scripts, and perform other tasks within this isolated environment.
Here are some basic commands to use for Ubuntu. Basics of Linux Command Line
Step 4: Exiting and Managing Containers:
4.1 To exit the container, type exit or press Ctrl + D. The container will stop when you exit.
You can start it again with the following command.
sudo docker start -ai <container_id>
Note:
To list all containers (running and stopped):
sudo docker ps -a
To remove a container:
sudo docker rm <container_id>
Conclusion
Docker containers provide a consistent, lightweight, and efficient way to deploy applications. By encapsulating applications and their dependencies, Docker ensures that your software runs smoothly in any environment. Its versatility makes it a cornerstone of modern DevOps practices and microservices architectures.
What Next:
Learn Advanced Docker Commands: Explore more advanced Docker features like networking, volumes, and Docker Compose.
Explore Kubernetes: Dive into container orchestration with Kubernetes to manage and scale Docker containers in production environments.
Join Docker Community: Engage with the Docker community through forums, meetups, and online courses to expand your knowledge and skills.
Visual Walkthrough
Check out this video for a walkthrough and demo “Coming Soon”
References