Summary

In this post, we covered an introduction to docker containers and explained the concept of containerization through concepts illustration and a practical example. we also covered an configuring, building and running docker containers and explained the concept of running multiple docker containers together through docker-compose.

This post covers the answers for Intro to Docker and Intro to Containerisation room in TryHackMe.

What is Containerization

In computing terms, containerization is the process of packaging an application and the necessary resources (such as libraries and packages) required into one package named a container. The process of packaging applications together makes applications considerably portable and hassle-free to run.

Modern applications are often complex and usually depend on frameworks and libraries being installed on a device before the application can run. These dependencies can:

  • Be difficult to install depending on the environment the application is running (some operating systems might not even support them!)
  • Create difficulty for developers to diagnose and replicate faults, as it could be a problem with the application’s environment – not the application itself!
  • Can often conflict with each other. For example, having multiple versions of Python to run different applications is a headache for the user, and an application may work with one version of Python and not another.

What are Docker Containers?

Docker is a relatively hassle-free, extensive and open source containerisation platform. The Docker ecosystem allows applications (images – we’ll come onto this in a later room) to be deployed, managed and shared with ease.

Working on Linux, Windows and MacOS, Docker is a smart choice for running applications. Applications can be published as “images” and shared with others. All that is required is pulling (downloading) the image and running it with Docker.

Docker employs the same technology used in containerisation to isolate applications into containers called the Docker Engine. The Docker Engine is essentially an API that runs on the host operating system, which communicates between the operating system and containers to access the system’s hardware (such as CPU, RAM, networking and disk)

Because of this, the Docker engine is extensive and allows you to do things like:

  1. Connect containers together (for example, a container running a web application and another container running a database)
  2. Export and import applications (images)
  3. Transfer files between the operating system and container

Docker uses the programming syntax YAML to allow developers to instruct how a container should be built and what is run. This is a significant reason why Docker is so portable and easy to debug; share the instructions, and it will build and run the same on any device that supports the Docker Engine.

Building Docker Containers

I learned two main ways to build Docker containers:

  • From Scratch: This involves creating a Dockerfile, which is essentially a blueprint. It’s a text file with instructions like FROM (to specify a base image, e.g., ubuntu:22.04), WORKDIR (to set the working directory inside the container), RUN (to execute commands during the image build, like apt-get update -y or apt-get install apache2 -y), EXPOSE (to inform Docker about ports the container listens on, e.g., 80), and CMD (the command to run when the container starts, like apache2ctl -D FOREGROUND). Once the Dockerfile is ready, I build the image using:
    • docker build -t test-docker .
  • Pulling an Image: If I don’t want to build from scratch, I can simply download pre-built images from repositories like Docker Hub. I can do this with the command:
    • docker pull ubuntu:latest

Managing Docker Containers

Once I have images, I can start managing containers:

  • Running a Container: To run a container in detached mode (background), name it, and map ports (e.g., host port 8080 to container port 80), I use:
    • docker run -d --name apache-web-server -p 8080:80 test-docker
  • Listing Images: To see all the Docker images on my system, I use:
    • docker image ls
  • Removing an Image: If I need to remove an image, I use:
    • docker image rm webserver:latest (or by its image ID)
  • Getting Help for Image Commands: For a quick reference on image commands, I can type:
    • docker image --help
  • Listing Running/Stopped Containers: To see all containers, both running and stopped, I use:
    • docker ps -a
  • Starting/Stopping Containers:
    • To start a stopped container: docker start <container_id_or_name>
    • To stop a running container: docker stop <container_id_or_name>
  • Interacting with a Container (getting a shell): To open an interactive shell inside a running container, I use:
    • docker run -it apache-server /bin/bash Inside the container, I can use commands like id to see the user (often root within the container’s isolated environment) or check for the .dockerenv file, which indicates I’m inside a Docker container.

Docker Compose for Multi-Container Applications

For applications that need multiple services (like a web server and a database), managing individual containers can get complicated. This is where Docker Compose comes in. It uses a YAML file (usually docker-compose.yml) to define and run multi-container applications.

The docker-compose.yml file typically includes:

  • version: Specifies the Compose file format version.
  • services: Defines each container, including its build path (for Dockerfile) or image (for pre-built), networks, ports, and environment variables.
  • networks: Defines how services communicate with each other.

To manage applications with Docker Compose, I use these commands:

  • docker-compose build: Builds or rebuilds services.
  • docker-compose up: Creates and starts containers.
  • docker-compose stop: Stops running containers.
  • docker-compose down: Stops and removes containers, networks, etc.

Technical Commands Extracted from the Terminal

Here’s a list of the specific commands I saw being used in the terminal:

  • docker image ls
  • pwd
  • cat Dockerfile
  • docker build -t apache-server .
  • docker run -d --name web-server -p 8080:80 apache-server
  • docker ps -a
  • docker start <container_id>
  • docker stop <container_id>
  • docker stop <container_name> (e.g., apache-server)
  • docker run -it apache-server /bin/bash
  • id (executed inside the container)
  • ls -la (executed inside the container)
  • cat .dockerenv (executed inside the container)

TryHackMe Answers

What is the name of the kernel feature that allows for processes to use resources of the Operating System without being able to interact with other processes?
In a normal configuration, can other containers interact with each other? (yay/nay)
What does an application become when it is published using Docker? Format: An xxxxx (fill in the x’s)

What is the abbreviation of the programming syntax language that Docker uses?

In what year was Docker originally created?

Where was Docker first showcased?

What version of Unix had the first concepts of containerisation?

What command can we use to view a list of running processes?
Containerise the applications in the static site. What is the flag?
If we wanted to pull a docker image, what would our command look like?

If we wanted to list all images on a device running Docker, what would our command look like?

Let’s say we wanted to pull the image “tryhackme” (no quotations); what would our command look like?

Let’s say we wanted to pull the image “tryhackme” with the tag “1337” (no quotations). What would our command look like?

What would our command look like if we wanted to run a container interactively?

Note: Assume we are not specifying any image here.

What would our command look like if we wanted to run a container in “detached” mode?

Note: Assume we are not specifying any image here.

Let’s say we want to run a container that will run and bind a webserver on port 80. What would our command look like?

Note: Assume we are not specifying any image here.

How would we list all running containers?

Now, how would we list all containers (including stopped)?

What instruction would we use to specify what base image the container should be using?
What instruction would we use to tell the container to run a command?

What docker command would we use to build an image using a Dockerfile?

Let’s say we want to name this image; what argument would we use?

I want to use docker-compose  to start up a series of containers. What argument allows me to do this?

I want to use docker-compose  to delete the series of containers. What argument allows me to do this?

What is the name of the .yml file that docker-compose uses?

Note: for this question, you will need to include the .yml file extension in your answer

What does the term “IPC” stand for?
What technology can the Docker Server be equalled to?
Connect to the machine. What is the name of the container that is currently running?

Use Docker to start a web server with the “webserver” image (no quotations). You will need to run the container with port 80.

After starting the container, try to connect to https://LAB_WEB_URL.p.thmlabs.com/ in your browser. What is the flag?

Video Walk-through

About the Author

Mastermind Study Notes is a group of talented authors and writers who are experienced and well-versed across different fields. The group is led by, Motasem Hamdan, who is a Cybersecurity content creator and YouTuber.

View Articles