Getting started with Docker
A quick start guide on your journey containerized journey
You may have encountered the term “docker” or “container”, while looking on open source or developer tooling, or maybe even when watching some programming tutorials on the internet. Let’s understand its importance on today’s development envinroment and by the end of this article you should be familiarized and able to run your first docker container.
- What is Docker?
- What is a container?
- What is an image?
- Why docker and the magic of reproducible environments
- Installing Docker
- Running your first docker container
- Must know CLI commands
- How to build a custom image? Answer is Dockerfile
- Docker compose (advanced)
- Recap
- Final considerations
What is Docker?
Docker is a tool that allows you to “package” an application alongside everything, like libraries needed for running basic functions, environment, configurations, and more, all that in a single asset called container.
What is a container?
A container is “a sandboxed process running on a host machine that is isolated from all other processes running on that host machine”, according to docker’s own documentation. In plain english it means that docker creates a single package with an isolated instance of an operating system (and its processes) to run, based on instructions provided by the user, in a standalone and isolated environment. Containers allows users to run instances of an image using base ubuntu OS in Windows desktop for example.
What is an image?
An image is used to provide all of the required pieces for the container’s file system. This means an image should include all application code, libraries, configurations, binaries, envinronment variables and etc. An image can be explained as a standalone artifact that docker should be able to instantiate and run in a container.
Why docker and the magic of reproducible environments
I see, now I understand what docker, containers and images are but isn’t it the same as running a virtual machine?
Not really. Although the concept of “virtualization” is applicable in this case, since the host machine has to virtualize the required resources by the docker container, the benefit of docker is in the portability of the image and container. It is expected that the same docker image should run succesfully, without any configuration changes, whether the host OS is a linux, macOS or windows machine.
This is specially important for developer environments so you can be assured that the docker image you just tested in your local machine is the exact same, and behaves the exact same, as the image deployed on you home lab or in the cloud. The fact that you could share the same docker image with a coworker or friend and the person downloading the image can simply reproduce the same envinronment you had on your machine is also a big advantage for software developers.
Installing Docker
Let’s gets started, shall we?
Docker has many installation options available for installation (including their docker desktop app) and for today’s guide, we are going to install through apt-get on Ubuntu. Run the following commands, from Install Docker Engine on Ubuntu, on your terminal:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Refresh the list of available packages and their versions
sudo apt-get update
# Install docker packages
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# Verify installation
sudo docker --version
Head over to Install Docker Desktop on Ubuntu page for full details or Get Docker to find instructions for your own OS.
Running your first docker container
Images can be shared in repositories. Docker Hub is a cloud-based repository service where users can store, manage and share Docker container images. Head over to Docker Hub and explore the extensive list of available images.
Let’s use their example of minimal Dockerization, hello-world.
sudo docker run hello-world
You should see docker working to download the image, stand up containers and more.
That’s it, you have now officially started your first container!
Must know CLI commands
| Command | Description |
|---|---|
docker pull <image> | Downloads an image from Docker Hub (like downloading an app template). |
docker run <image> | Starts a new container from an image |
docker ps | Lists running containers. |
docker ps -a | Lists all containers, including stopped ones. |
docker stop <container> | Stops a running container. |
docker start <container> | Starts a stopped container. |
docker rm <containers> | Deletes a container. |
docker images | Shows all downloaded images. |
docker rmi <image> | Deletes an image. |
docker logs <container> | Displays output (logs) from a container. |
docker exec -it <container> bash | Opens a terminal in a running container for troubleshooting. |
docker build -t <name> . | Builds a custom image using a Dockerfile. |
docker cp <src> <container>:<dest> | Copies files into/out of a container. |
How to build a custom image? Answer is Dockerfile
A Dockerfile is a plain text file that contains a set of instructions used to build a Docker image. Here is a basic example:
# syntax=docker/dockerfile:1
FROM debian
RUN <<EOT bash
set -ex
apt-get update
apt-get install -y vim
EOT
Visit Docker docs for the full reference for Dockerfile.
Docker compose (advanced)
Docker compose, as described in its documentation, “is a tool for defining and running multi-container applications”. Much like defining your own image using Dockerfile, docker compose enables advanced use cases for defining multiple images, its network connections, its dependencies and more. Think of it as a Dockerfile of Dockerfiles.
Recap
In this article we discussed:
- what docker is and its purpose;
- core concepts such as containers and images;
- how to run a container from an image from docker hub;
- important CLI commands; and
- what docker compose is.
Final considerations
Docker makes it very easy to reproduce development and sandbox environments, allowing for a great isolation and reproducible builds from development to production. A lot of today’s technology industry is powered by containers and widespread usage has lead to standards and best practices like the OWASP - Docker Security Cheat Sheet and even tools like Snyk to automate security on product environments using containers. Docker is not alone in the container world, there are very powerful and well rounded alternatives such as Podman and Kubernetes.
See ya! 👋