Skip to main content

Docker Basics

Docker installation

Docker installation instructions from docker main site

If you on debian like Kali

  1. Set up Docker's apt repository.
# 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/debian/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/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
  1. Install the Docker package.
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  1. Verify that the installation is successful by running the hello-world image:
sudo docker run hello-world

Docker registry

Find various docker images on docker hub

Example: mongodb image on docker hub

Download mongodb image:

docker pull mongo

Start a mongo server instance:

 docker run --name some-mongo -d mongo:tag

starting mongodb and port mapping

docker run -d -p 27018:27017 mongo

27018:27017 the hosts port 27018 is mapped to the container port 27017

docker images

list local images

docker images

delete an image

docker rmi <image_name>

remove all unused images

docker image prune

Build an Image from a Dockerfile

docker build -t <image_name>

Build an Image from a Dockerfile without the cache

docker build -t <image_name> . –no-cache

Docker HUB

login into Docker

ocker login -u <username>

Publish an image to Docker Hub

docker push <username>/<image_name>

Search Hub for an image

docker search <image_name>

Pull an image from a Docker Hub

docker pull <image_name>

Once you have an image, start the container and interact with it. Following are basic commands

Docker containers

To list currently running containers

docker ps

List all docker containers (running and stopped)

docker ps --all

View resource usage stats

docker container stats

Run a container in the background

docker run -d <image_name>

Start or stop an existing container

docker start|stop <container_name> (or <container-id>)

Create and run a container from an image, with a custom name

docker run --name <container_name> <image_name>

Run a container with and publish a container’s port(s) to the host

docker run -p <host_port>:<container_port> <image_name>

Remove a stopped container

docker rm <container_name>

Forcefully stop the container - kill

docker kill <container_name>