Skip to main content

Images and Containers in Practice

As we saw in the introduction, to interact with Docker you need to understand the lifecycle between Images and Containers.

Images (The Mold)

An image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform. It provides a convenient way to package applications and preconfigured server environments.

There are millions of pre-built images (like a pure Ubuntu, an official Node.js release, a ready-to-use MySQL database, etc.) hosted publicly on Docker Hub (the "GitHub" of Docker images).

Downloading Images (docker pull)

If you need to download the official Nginx image (a web server), you run:

docker pull nginx

That command will download the latest stable version (barely ~15MB) to your local computer.


Containers (The Living Application)

If the image is the class in OOP, the container is the instantiated object. A container is an executable unit of software. Upon starting one from one of your downloaded images, an isolated environment will be created on your computer where the application will run seamlessly encapsulated.

Spinning up a Container (docker run)

The most important and daily used command is run. If you execute docker run nginx, Docker will bring up a live web server.

However, since containers are fully isolated from the hosting computer (host), you will not be able to access the website unless you intentionally expose the ports (the "window").

Instead of a bare command, a useful deployment looks like this:

docker run -d -p 8080:80 --name my-website nginx

Breaking down the magic:

  • -d (Detached): Tells the container to run "in the background", so it doesn't hijack and lock up your console. This way you can continue using the terminal.
  • -p 8080:80 (Port): This is a "Network Mapping". It establishes a secure tunnel. It means: "When I type localhost:8080 in my laptop's browser, redirect that traffic to port 80 INSIDE the container".
  • --name my-website: You give it an easy-to-remember nickname. If you don't put it in, Docker will invent an absurd random name (e.g. brave_hawking).
  • nginx: The name of the pre-built Image from which this container will be born.

Essential Management Commands

As you advance, you will end up having dozens of containers over time. It is vital to know what is happening.

1. What is running right now?

docker ps

This command lists all your active containers, their IDs, their name, how long they've been running and what ports they have occupied. (Use docker ps -a to also see the containers that are turned off).

2. Stop an active Container

To stop (like turning off the PC) a container that we no longer need and free up its port, we use the stop command followed by the ID (just the first 3 numbers suffice) or the name you assigned to it.

docker stop my-website

3. Delete a Container

Stopping it does not erase it from the hard drive. To destroy it completely (which involves tearing up a new one from scratch the next time you need Nginx), use:

docker rm my-website  
# Or add -f if it is on and you want to forcibly kill it:
# docker rm -f my-website