Understanding Docker Containers and Images
Docker Containers
Docker containers are the fundamental units of Docker. A container is a lightweight, standalone, and executable package that includes everything needed to run an application: the code, runtime, system tools, system libraries, and settings. Containers are isolated from each other and from the host operating system, ensuring consistent and reliable application behavior.
To create a container, you start with a Docker image, which is a read-only template that defines the environment for running your application. When you run a Docker image, it creates a Docker container, which is a running instance of the image.
Here's an example of creating a Docker container using the Ubuntu 22.04 image:
docker run -it ubuntu:22.04 /bin/bash
This command creates a new container based on the ubuntu:22.04
image and starts an interactive shell (/bin/bash
) within the container.
Docker Images
Docker images are the building blocks of Docker containers. An image is a read-only template that contains a set of instructions for creating a Docker container. Images are typically built from a Dockerfile, which is a text file that contains all the commands a user would need to assemble an image.
Here's an example of a simple Dockerfile that creates an image with a custom message:
FROM ubuntu:22.04
RUN echo "Hello, LabEx!" > /message.txt
To build this image, you can run the following command:
docker build -t my-message-image .
This will create a new Docker image named my-message-image
based on the instructions in the Dockerfile.
Docker images are stored in a Docker registry, which is a centralized repository for Docker images. The most popular registry is Docker Hub, which provides a wide range of pre-built images for various applications and services.
By understanding the concepts of Docker containers and images, you'll be able to effectively build, deploy, and manage your applications using Docker.