Docker is a tool that packages applications and their dependencies into a standardised unit called a container
Key Concepts
1. Docker Container
A container is a self-contained, executable software package that includes everything needed to run an application, such as code, libraries, and dependencies. Containers are isolated, self-contained environments that run on a single operating system kernel. Think of it like a shipping container for software: it holds everything an application needs to run, no matter where it’s being shipped (or deployed). A container is an instance of a Docker image. A container is a running instance of a Docker image.
When you run an image, Docker creates a container, which is a lightweight, isolated environment. You can start, stop, move, or delete containers. Multiple containers can run on the same machine, each completely separate from the others. Docker containers are isolated, ensuring that containers can run on the same host machine without interfering with each other or with the host’s operating system. Docker’s isolation is a key benefit, allowing you to run multiple applications on the same server safely. It’s not the same as a virtual machine (VM), a VM is a heavy, resource-intensive solution because it has to virtualize hardware and run a complete, separate operating system for each application. Instead, containers share the host’s operating system kernel. This lightweight approach is what makes containers so fast and efficient.
2. Docker Image
An image is a read-only template that contains all the instructions needed to create a Docker container. It is a snapshot of an application’s environment, containing everything needed for the application to run. It includes the application code, system libraries, system dependencies, system tools, system utilities, and an operating system (e.g., a lightweight version of Alpine Linux or Ubuntu). Images are built from a Dockerfile, a simple text file with a set of instructions. This ensures that the image is consistent and reproducible. Once an image is built, it can be stored in a registry like Docker Hub and shared with anyone.
When you run a command like “docker run my-image”, Docker uses that image to create a container, which is a live, running instance. Multiple containers can be created from the same image, and they will all be identical. This is why Docker provides consistency across different environments—because everyone is running a container from the same, standardized image. Docker image is like a blueprint for your application, including the code, libraries, and system tools. You can get images from public or private repositories, like Docker Hub, or build your own.
3. Dockerfile
A text file that contains a set of instructions for building a Docker image. It’s a simple, human-readable script that defines everything needed to create your application’s environment. You’ll specify the base operating system, add your application files, and install dependencies. A Dockerfile consists of a series of instructions, each on its own line. These instructions are processed sequentially by Docker when you run the docker build command. Some common instructions include: – FROM: This is always the first instruction.
It specifies the base image you want to start from. For example, FROM node:18-alpine means your image will be built on top of a lightweight Alpine Linux image that has Node.js version 18 pre-installed. – WORKDIR: Sets the working directory inside the container for any subsequent RUN, CMD, COPY, or ADD instructions. This prevents you from having to type out full file paths. WORKDIR /app sets the working directory to the / app folder. – COPY: Copies files or directories from your local machine (the build context) into the container image. For example, COPY . . copies all the files from your current directory into the container’s working directory. – RUN: Executes any command inside the container. This is typically used to install packages or dependencies. For example, RUN npm install would run the npm install command to install your Node.js application’s dependencies. – CMD: Provides a default command that will be executed when the container starts. A typical use would be CMD [“npm”, “start”].
4. Docker Hub
A public registry (a repository of images) where you can find and share Docker images. It’s the central place to get official images for popular software like Nginx, Python, and Node.js. Why Use Docker? – Portability: Containers ensure your application runs the same way on any machine— whether it’s on a developer’s laptop, a test server, or a production environment. This eliminates the “it works on my machine” problem. – Isolation: Containers are isolated from each other and the host system. This means one application’s dependencies won’t interfere with another’s. It also adds a layer of security. – Efficiency: Containers are much more lightweight than traditional virtual machines because they share the host’s operating system kernel. This means they start faster and use fewer resources.