Programming

Docker

What is Docker?

Docker packs a program together with its libraries. The gap of “it worked on my computer” gets smaller. It is not a full virtual machine. It shares the kernel with the host, so it is lighter. Installing Docker on an Ubuntu server is an ordinary path.

What is Docker used for?

It is for keeping the same environment on a developer machine and on a server.

  • Building the app as an image
  • Starting a database locally
  • Running the same test in CI
  • Separating several services on one server

What can you do with Docker?

A Dockerfile writes the steps: a base image, packages, and the command. docker run opens a container from that image. A network port and a disk volume are attached separately. Docker does not replace a firewall or an HTTPS certificate.

Is Docker hard to learn?

hello-world starts quickly. The difficulty is image size, writing a secret into the image, and the network. A password and a key must not be put inside the image. They should be given from the environment when it starts.

Advantages and limits

Advantage: the environment repeats, and the mess of extra packages on a server gets smaller. Limit: there is learning, the disk fills, and a badly written image carries an old hole. Root inside a container can give a wide right.

Basic ideas

An image is a readable template. A container is the process created from it. A registry, for example Docker Hub, stores images.

  • A Dockerfile is the list of instructions
  • docker build creates an image
  • A port is mapped with -p 8080:80
  • A volume can keep data when the container is deleted

A short example

This command downloads a test image, runs it, and deletes it when it finishes.

docker run --rm hello-world

Common questions

  • Is Docker a virtual machine? No. It is a container that shares the kernel.
  • Are an image and a container the same? No. The image is the template. The container is the running copy.
  • Does Docker give HTTPS? No. Encryption is the certificate, usually on the Nginx side.