Introduction to Docker, What is Docker?
Docker is an environment which a advanced system that allows developers to do most of the deployment work automate and can easily help in , scaling, and maintenance of applications via containerisation. Unlike typical virtual machines, Docker containers offer lightweight, portable, and efficient environments that include everything required to run applications across different computers or servers.
Key Components of Docker
- Containers: A containerised system is Docker’s core unit, packaging a program and its dependencies to ensure consistency across development, testing, and production environments.
- Images: Docker images are lightweight, independent, executable bundles that contain everything required for running a container, including code, runtime, libraries, and configurations.
- Engine: Docker Engine is a service that executes and manages containers on an operating machine.
- Hub: Docker Hub is a cloud-based hub where all the developer can collaborate & distribute container images.
- Compose: In Docker Compose is a tool for creating and controlling multi-container Docker applications using a simple YAML configuration file.
Managing Docker Containers – Basic Docker Command’s
Starting, Stopping, and Removing Containers
- Start a container:
docker start <container_id>
- Stop a container:
docker stop <container_id>
- Remove a container:
docker rm <container_id>
Running a Simple Container
To verify Docker installation, use:
docker run hello-world
Accessing a Running Container’s Shell
docker exec -it <container_id> bash
Understanding Container Lifecycle
- Created: The container exists but has not started.
- Running: The container is actively executing processes.
- Paused: The container’s processes are temporarily suspended.
- Stopped: The container has been terminated.
- Deleted: The container is removed from the system.
Lifecycle Management Commands
- Pause a container:
docker pause <container_id>
- Unpause a container:
docker unpause <container_id>
- Restart a container:
docker restart <container_id>
Monitoring Containers
List all running and stopped containers:
docker ps -a
Managing Persistent Data with Docker Volumes
What are Docker Volumes?
Docker volumes allow data to persist beyond the container lifecycle. They store data in a dedicated directory on the host system.
Creating and Managing Volumes
- Create a volume:
docker volume create my_volume
- List available volumes:
docker volume ls
- Inspect a volume:
docker volume inspect my_volume
Using Volumes in Containers
To mount a volume:
docker run -d --name my_container -v my_volume:/app/data my_image
Bind Mounts vs. Docker Volumes
- Bind Mounts: Directly map host directories to containers.
- Docker Volumes: Managed by Docker and stored in
/var/lib/docker/volumes
.
Example Use Cases
- Bind Mounts: Ideal for development environments requiring immediate code changes.
- Docker Volumes: Best for persistent database storage and data sharing across containers.
Backing Up and Restoring Volumes
- Backup a volume:
docker run --rm -v my_volume:/volume -v $(pwd):/backup busybox tar cvf /backup/backup.tar /volume
- Restore a volume:
docker run --rm -v my_volume:/volume -v $(pwd):/backup busybox tar xvf /backup/backup.tar -C /volume
Writing Dockerfiles
Simple Dockerfile Example
FROM ubuntu:latest RUN apt-get update && apt-get install -y python3 COPY . /app WORKDIR /app CMD ["python3", "app.py"]
Multi-Stage Build Dockerfile
FROM node:14 AS build WORKDIR /app COPY . . RUN npm install && npm run build FROM nginx:alpine COPY --from=build /app/build /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
Conclusion
Docker is a game-changer in application deployment, offering portability, efficiency, and scalability. By mastering Docker containers, volumes, and lifecycle management, developers can streamline workflows and optimize their applications for cloud and on-premise environments.