What is the difference between an image and a container?
sobes.tech AI
Answer from AI
An image is a static, immutable snapshot of the file system and configuration parameters necessary to run an application. It serves as a template.
A container is a running instance of an image. It is dynamic and represents a working process with its own network interface, filesystem (based on the image), and isolated environment.
| Characteristic | Image | Container |
|---|---|---|
| State | Immutable | Mutable |
| Entity | Template | Working instance |
| Size | Usually smaller than a running container | Can be larger due to the write layer |
| Lifecycle | Created, stored | Created, started, stopped, deleted |
Example: Dockerfile:
# Base image
FROM ubuntu:latest
# Copy application
COPY app /app
# Install dependencies
RUN apt-get update && apt-get install -y python3
# Set working directory
WORKDIR /app
# Run command
CMD ["python3", "main.py"]
This Dockerfile describes how to build an image. After building the image, for example with the command docker build -t myapp ., we get a static image myapp.
Then, by running the command docker run -d myapp, we create a container based on this image. This container is an active process in which our application runs. Multiple containers can be created from a single image. Changes inside one container (e.g., file writing) do not affect the image itself or other containers launched from the same image, thanks to the Copy-on-Write mechanism.