What is Docker?

Docker is an open platform for developing, shipping and running applications inside containers. A container packages the code and everything it needs, so it runs the same on any machine.

Updated 6 min read

Learning objectives

After reading this article you will be able to:

  • Understand images, containers, Dockerfiles and registries
  • Read and write a simple Dockerfile and run it with docker build and docker run
  • Know when Docker helps and when you do not need it
On this page

"It works on my machine" is one of the oldest lines in software. On the server, the Node version was different, a system library was missing or an environment variable had another name. Docker solves this by changing what you ship: instead of only your code, you ship the code together with the environment it runs in.

Dockerfiledocker buildImageread-onlypush / pullRegistryDocker Hubdocker runContainerContainerContainerDocker daemon (dockerd)runs the build, push and run the docker client asks for
One recipe, one immutable image, many identical containers; the daemon carries out every docker client command.

What is Docker?#

Docker packages an application and everything it depends on into a container, an isolated environment that doesn't depend on what is installed on the host. You can run many containers at once on the same machine, and each one behaves the same wherever it runs.

The official documentation defines a container as an isolated process with all the files it needs to run. There is no second operating system: containers share the host kernel. That is the central difference from a virtual machine, covered in containers vs virtual machines.

What are the parts of Docker?#

Five concepts cover almost everything:

  • Image: a read-only template with instructions for creating a container. It is built from layers, and each layer is a set of filesystem changes. Once created, an image doesn't change: to alter it, you build a new one.
  • Container: a runnable instance of an image. You create, start, stop and remove containers. By default a container is isolated from other containers and from the host.
  • Dockerfile: the text file with the instructions to build the image.
  • Registry: where images are stored. Docker Hub is the public registry Docker searches by default, and you can also run a private one.
  • Docker Engine: the program that does the work, in a client-server model. The client (docker) sends commands such as docker run to the daemon (dockerd), which creates and manages images, containers, networks and volumes. They talk over a REST API.

On Linux you install Docker Engine directly. On Windows and macOS the route is Docker Desktop, which bundles the daemon, the client and Compose in one application.

How do you run a container in one minute?#

With Docker installed, the first test is:

bash
docker run --rm hello-world

Docker looks for the hello-world image locally, doesn't find it, pulls it from Docker Hub, creates a container, prints a message and exits. The --rm flag removes the container when it finishes. Run docker ps to list running containers; docker ps -a includes stopped ones.

How do you put an app in a container?#

You describe the image in a Dockerfile at the project root. The example below packages a Node.js app that listens on port 3000:

dockerfile
FROM node:lts-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

Instruction by instruction:

  • FROM sets the base image. Every Dockerfile starts with it. If you omit the tag, Docker assumes latest, so always set one. lts-slim follows the active Node.js LTS; for fully reproducible builds, pin the major version too.
  • WORKDIR sets the working directory for the following instructions and creates it if it doesn't exist.
  • COPY and RUN create layers. Copying package*.json and installing dependencies before copying the rest of the code lets Docker reuse that layer in later builds, as long as the dependencies don't change.
  • EXPOSE only documents the port. It doesn't publish anything.
  • CMD is the container's default command. Prefer the exec form, a JSON list with double quotes, which doesn't go through a shell.

Also create a .dockerignore file with node_modules and .env, so local dependencies and secrets stay out of the image. Then build and run:

bash
docker build -t my-app .
docker run --rm -p 3000:3000 my-app

-p 3000:3000 publishes container port 3000 on port 3000 of your machine. Open http://localhost:3000 and the app should respond. If it doesn't, check that your code listens on 0.0.0.0 and not only 127.0.0.1: inside the container, 127.0.0.1 is the container itself. For the full walkthrough, see how to dockerize an app.

What happens to a container's data?#

Changes a container makes to its own files are deleted when the container is removed. To keep data, such as a database's files, use a volume, which exists independently of any container's lifecycle:

bash
docker volume create app-data
docker run -d -v app-data:/var/lib/data my-image

The syntax is -v volume-name:path-in-container. A volume can be shared by several containers and is only removed when you ask, with docker volume rm.

What is Docker used for in practice?#

  • The same environment everywhere: the container that passed on your laptop is the one that runs on the server.
  • Fast onboarding: a new teammate starts the app and its database with one command, installing nothing by hand. For several containers together, Docker Compose describes everything in one file.
  • Isolation: two apps that need different versions of the same language live on one machine without conflict.
  • Predictable delivery: the image is a versioned artifact. To roll back, you start the previous image.

When is Docker not necessary?#

Docker adds a layer you have to learn and maintain: Dockerfiles, images, volumes, networks and base image updates. If you host a bot or an API on a platform that takes your code and provides a ready runtime, you don't need to write any of it. Think of Docker when you must control the operating system, install packages the default environment lacks, or repeat the exact same environment on several servers.

Keep two things in mind. A container is not as strong a security boundary as a VM, because it shares the kernel. And Docker Desktop has its own license: it is free for personal use, education and non-commercial open source, but companies above a certain size, measured by employees or annual revenue, need a paid subscription. Check the current thresholds on the Docker Desktop license page.

How to do this on Square Cloud#

Square Cloud is one of those platforms that provide the environment for you. Applications run in isolated containers in a Tier 3 datacenter in New York, but you don't bring a Dockerfile or your own image: you pick one of several runtimes, such as Node.js, Python and Go, or the one for static sites, and send your code. See the runtimes available today on Runtimes.

You can deploy by uploading a ZIP in the dashboard, with the CLI, through GitHub (a push deploys, via the official GitHub Action or a webhook) or with the VS Code extension. The squarecloud.app configuration file is optional: the dashboard generates it at deploy time, with runtime, memory and main file, or you write it yourself at the root of the project.

Containers are always on, with no cold starts and automatic restart (on by default for dashboard deploys) when the process exits with an error. Vertical scaling is up to you: change each application's RAM in the settings or through the API. See what is included in Square Cloud features.

Deploy

Push from GitHub, the CLI or a ZIP and go live in seconds

Learn more

FAQs

Is Docker a virtual machine?

No. A virtual machine runs a full operating system with its own kernel. A Docker container is an isolated process that shares the host kernel, which makes it lighter.

Is Docker free?

Docker Desktop is free for personal use, education and non-commercial open source projects. Companies above a certain size, measured by employees or annual revenue, need a paid subscription. The current thresholds are on Docker's license page.

What is the difference between an image and a container?

An image is a read-only template with instructions for creating a container. A container is a runnable instance of that image. You can create many containers from one image.

Do I need Docker to put my app online?

Not necessarily. Many platforms take your code and provide the runtime environment for you. Docker makes sense when you need to control the system and its dependencies or repeat the same environment in several places.

Does Docker run on Windows and macOS?

Yes, through Docker Desktop. On Linux you install Docker Engine directly.

Next articleBuild-time vs runtime environment variablesBuild-time variables are baked into your bundle when you build; runtime variables are read when the app starts. Learn which is which and where secrets can live.

More in Deploy

See all 33 articles