V9
Velocity9
Docker
Documentation

What is a Container and How to Create It

Simple explanation of what containers are and how to create them in Docker.


🐳 What is a Container and How to Create It

A container is a lightweight, standalone, and executable software package that includes everything an application needs to run β€” like code, runtime, system tools, libraries, and settings. It’s a virtual environment created from a Docker image, allowing you to run your app consistently on any machine.

Containers are isolated from the host system, meaning each container runs its own process independently without interfering with others. This makes containers perfect for microservices, DevOps, and cloud deployment.


βš™οΈ How a Container Works

  • A Docker image is like a blueprint (a static snapshot).
  • A Docker container is a running instance of that image.
  • You can have multiple containers running from the same image, each isolated and independent.

Example:

  • Image = recipe
  • Container = dish made from that recipe

πŸš€ How to Create a Container

Before creating a container, make sure Docker is installed and the image you want to use is available.

You can either:

  1. Use an existing image from Docker Hub (like nginx, ubuntu, mysql, etc.)
  2. Or build your own image using a Dockerfile and then run it as a container.

🧩 Step 1: Pull or Build an Image

To pull a ready-made image from Docker Hub:

docker pull nginx

Or build your own image (example: myapp):

docker build -t myapp .

πŸ‹ Step 2: Create and Run a Container

Once you have an image, run it using:

docker run -d -p 8080:80 --name mycontainer nginx

Explanation:

  • docker run β†’ creates and starts a container.
  • -d β†’ runs it in detached mode (background).
  • -p 8080:80 β†’ maps port 8080 on your computer to port 80 in the container.
  • --name mycontainer β†’ assigns a custom name to the container.
  • nginx β†’ is the name of the image used to create the container.

🧱 Step 3: Check Running Containers

To list all active containers:

docker ps

To see all containers (including stopped ones):

docker ps -a

🧹 Step 4: Manage Containers

You can stop, start, or remove containers anytime.

Stop a container:

docker stop mycontainer

Start a stopped container:

docker start mycontainer

Remove a container:

docker rm mycontainer

βœ… In short: A container is a running environment built from an image that isolates your application and its dependencies, making it portable, efficient, and reliable across any system running Docker.

Need help with this topic?