---
title: "Docker Basics: Installation & Commands | Ameya Shenoy"
description: "Get started with Docker using Ameya Shenoy’s primer on installation, Linux post-install setup, images, containers and the essential commands for everyday use."
canonical: "https://www.codingcoffee.dev/blog/docker-primer"
published: "2019-04-12"
tags: "blog"
---

# Docker Primer

By Ameya Shenoy · Published 2019-04-12

Get started with Docker using Ameya Shenoy’s primer on installation, Linux post-install setup, images, containers and the essential commands for everyday use.

When we think of virtualization today, we may think of Virtual Box, which abstracts away the system processes, and lets you run a complete system from another. Think of Docker as Virtual Box, but extremely lightweight (in terms of resource consumption). Obviously I'm over simplifying the explanation a little, and a whole lot of things are getting lost in simplification. But for now, this will do.

## Installation

Install docker:

```bash
apt remove docker docker-engine docker.io
apt update && apt -y upgrade
apt install -y linux-image-extra-$(uname -r) linux-image-extra-virtual
apt update && apt -y upgrade
apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt update && apt -y upgrade
apt install -y docker-ce
```

Install docker-compose:

```bash
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
```

## Post Installation

Allow docker usage without sudo access:

```bash
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
```

## Terminology

- **Images**: Read-only templates uploaded to Docker Hub; analogous to classes in OOP
- **Containers**: Runtime instances spawned from images; similar to object instances
- **Tags**: Version identifiers for images
- **Container ID or Image ID**: Unique identifiers assigned to each container or image

## Usage

A `Dockerfile` can be used to build docker images.

List all images available on your system:

```bash
docker images
```

List all containers:

```bash
docker ps -a
```

Pull images from the docker hub:

```bash
docker pull ubuntu
```

By default the `latest` tag is pulled. To pull a specific tag:

```bash
docker pull ubuntu:12.04
```

Delete an image:

```bash
docker rmi ubuntu:latest
```

or

```bash
docker rmi IMAGE_ID
```

Create a container from an image and run it:

```bash
docker run -it ID /bin/bash
```

- `i` stands for interactive
- `t` stands for pseudo tty
- `ID` can be an `IMAGE-ID` or `CONTAINER-ID`

Note: Every time you use `docker run` with an `IMAGE-ID`, a new container is created based on the image. Using `--rm` flag automatically deletes the container after exiting.

Exit a container:

```bash
exit
```

Start / stop a container:

```bash
docker start CONTAINER_ID
docker stop CONTAINER_ID
```

Delete a container:

```bash
docker rm CONTAINER_ID
```

Build your own docker image using a `Dockerfile`:

```bash
docker build -t username/image_name:image_tag .
```

Delete dangling images:

```bash
docker rmi $(docker images -f dangling=true -q)
```

Save an existing docker container as an image:

```bash
docker commit CONTAINER_ID IMAGE_NAME:IMAGE_TAG
```

Save an image as a tar file:

```bash
docker save IMAGE_ID -o file_name.tar
```

---

Canonical page: https://www.codingcoffee.dev/blog/docker-primer

[Site guide](/llms.txt) · [Contact](/contact) · [Sitemap](/sitemap-index.xml)
