Skip to content
Lesson: Docker ContainersSwitch
← DevOps Operations

Delivery and containers

Docker Containers

Learn what a container is, how it differs from a virtual machine, and its basic lifecycle.

A container is an isolated process (or group of processes) that runs from a packaged filesystem called an image. It brings its libraries and tools with it, so an application behaves much the same on different machines.

Not a virtual machine

A virtual machine runs its own operating system kernel on virtual hardware. Linux containers share the kernel of the Linux machine they run on. The kernel uses namespaces to limit what a process can see and cgroups to limit what it can use. This makes containers light and quick to start, but the separation is not the same as a full virtual machine.

Docker Desktop on macOS and Windows runs Linux containers inside a Linux virtual machine.

Run your first container

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
...
Hello from Docker!

Docker downloads the image if you do not have it, creates a container, and runs it. If you get a "permission denied" error on Linux, use sudo. Adding your user to the docker group also works, but that group grants root-level control of the host.

A container lives as long as its main process

When the main process ends, the container stops. hello-world prints a message and exits. A web server keeps running, so its container does too.

Stop, start and remove

$ docker run -d --name web nginx
$ docker stop web
$ docker start web
$ docker stop web
$ docker rm web

stop asks the main process to shut down and forces it after a timeout. A stopped container still exists, with its filesystem changes, and start runs it again. rm deletes it. Use docker rm -f to remove a running one.

Data and throwaway containers

Files written inside a container go into its own writable layer. They disappear when the container is removed. Keep important data in a volume:

$ docker run -d --name db-demo -v appdata:/data nginx

The volume appdata stays after the container is removed. Here nginx only stands in for an application that writes to /data.

Containers are ordinary processes underneath, so the processes lab is useful background.

Try it

Run docker run -it alpine sh, create a file, and type exit. Run the same command again and look for the file. Explain what you see.

Check your understanding

Docker Containers

All 4 sections · no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.