What is Docker?
Docker is an open-source platform that automates the deployment, scaling, and management of applications in lightweight, portable containers. These containers encapsulate an application and its dependencies, ensuring it runs seamlessly across different computing environments.
Why Use Docker?
Consistency: Docker ensures that your application behaves the same way on any machine, eliminating the "it works on my machine" problem.
Isolation: Each container runs in its own isolated environment, allowing multiple containers to run on the same host without conflicts.
Portability: Docker containers can run on any system that supports Docker, making it easy to move applications between development, testing, and production environments.
Efficiency: Containers share the host OS kernel, making them more lightweight and faster to start than traditional virtual machines (VMs).
Key Concepts
Images and Containers
Images: Immutable, read-only templates that define how a container should be constructed. Think of an image as a blueprint for your application.
Containers: Instances of images. While images are read-only, containers can be modified and run as isolated processes on the host system.
Dockerfile
A Dockerfile is a text file that contains a series of instructions on how to build a Docker image. Here’s a simple example:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run
app.py
when the container launches
CMD ["python", "
app.py
"]
Here are several ways to create Docker images:
1). Using a Dockerfile: Involves creation of file as above.
2). Using Docker Commit:
Run a container:
docker run -it ubuntu
Install or modify anything inside the container:
apt-get update apt-get install -y python3
Commit the container to create an image:
docker commit my-custom-ubuntu
3). Using Docker Compose:
Docker Compose is a tool for defining and running multi-container Docker applications. While its primary use is to run containers, it can also be used to build images.
Example -- docker-compose.yml:
##Example docker-compose.yml for building and running a service
version: '3.8'
services:
web:
build: .
ports: - "5000:80"
Docker Hub
Docker Hub is a cloud-based registry service for sharing Docker images. It hosts both public and private repositories. You can pull pre-built images from Docker Hub or push your custom images for sharing.
Basic Docker Commands
Installing Docker: Follow the official Docker installation guide to set up Docker on your system.
Starting/ Stoping docker: To start and stop this service.
service docker start service docker stop
Running a Container: To run a container from an image, use the docker run command.
docker run -it hello-world
here -it Allows interactive terminal access.
- Building an Image: Use the docker build command with a Dockerfile to create a custom image.
docker build -t my-python-app .
- Listing Containers: The docker ps command lists all running containers.
docker ps
- Stopping a Container: The docker stop command stops a running container.
docker stop <container_id>
Starting stopped Container: The docker start command starts a stoped container.
docker start <container-id>
Attaching to Container: It is used to connect your terminal to a running container to view its output and provide input.
docker attach <my_container>
Execute Container: This command is used to run commands inside a running Docker container.
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Options::
-i or --interactive: Keep STDIN open even if not attached. Useful for interactive sessions.
-t or --tty: Allocate a pseudo-TTY. Useful for interactive sessions where you need a terminal interface.
--detach: Run the command in the background and return the container ID.
Removing a Container: Use docker rm to remove a stopped container.
docker rm <container_id>
Status checking of service: Used to ensure that Docker is running properly on their system.
service docker status
orsystemctl status docker
Docker images listing: To enlist images present in system.
docker images
ordocker image ls
Pushing an Image to Docker Hub: Use the docker push command to upload your image.
docker tag my-python-app username/my-python-app
docker push username/my-python-app
Renaming container name:
docker rename <old_name> <new_name>
Getting Started with Docker
To start using Docker, follow these simple steps:
Install Docker: Download and install Docker from the official website.
Pull an Image: Use docker pull to download an image from Docker Hub.
docker pull nginx
- Run a Container: Start a container using the pulled image.
docker run -d -p 80:80 nginx
- Build Your Own Image: Create a Dockerfile for your application and build it.
docker build -t my-app .
Share Your Image: Push your image to Docker Hub to share it with others.
Inspect changes made to Docker: This command lists the differences between the container's filesystem and the image it was originally based on.
docker diff CONTAINER
The output includes the following types of changes:
A: Added files or directories.
D: Deleted files or directories.
C: Changed files or directories.
Commit changes of current state of running container: It is useful when you have a running container and you want to save its current state as a new image.
docker commit -a "Your Name
your.email@example.com
" -m "Added new feature" my-container my-new-image:latest
Creating container ,naming it, starting an interactive bash shell in it:
docker run -it --name mycont3 -v /host/path:/anush ubuntu bin/bash
Starting new container naming it, running the container in privileged mode:
Start a new container named mycont3 from the Ubuntu image, Run the container in privileged mode, Mount all volumes from the existing container named mycont1 , Start an interactive bash shell inside the new container, allowing you to interact with it directly.
docker run -it --name mycont3 --privileged=true --volumes-from mycont1 ubuntu /bin/bash
Running a container webcount, Mapping port 80 of the host to port 80 of the container ,using the official Nginx image to run on port 80:
docker run -td --name webcount -p 80:80 nginx
Docker networks :
It allow containers to communicate with each other, the host, and external systems. They provide isolation, flexibility, and control over communication between containers.
Docker Network Commands:
Command | Description |
docker network ls | Lists all available networks. |
docker network inspect <network> | Displays detailed information about a specific network. |
docker network create <name> | Creates a custom network (default: bridge). |
docker network rm <name> | Removes a custom network. |
docker network connect <network> <container> | Connects a container to a network. |
docker network disconnect <network> <container> | Disconnects a container from a network. |
Conclusion
Docker revolutionizes how we develop, ship, and run applications. By encapsulating applications in containers, Docker ensures consistency, isolation, portability, and efficiency. Whether you're a developer, system administrator, or a DevOps engineer, understanding Docker is essential for modern software development and deployment.
Further Reading
With Docker, the possibilities are endless. Start exploring and see how Docker can transform your workflow today!