Virtual Machines vs. Container Images

In today's article we want to explain the differences between a virtual machine and a container and show examples of how you can build container images yourself and get a container running.

Difference between a virtual machine and a container

Table of Contents

Differences Virtual Machine vs. Container

The virtual machines are based on a hypervisor that runs on a host (e.g. a server or the laptop at home). A completely different operating system can be represented in a virtual machine than that of the host system. For example, you can virtualize a Windows machine on a Linux system. Applications that would not run on the host operating system can be executed in this virtual machine.

However, the problem with this principle is resources. Let's assume a simple web server to be hosted in this virtual machine. In order to operate this web server, resources are required to operate a system that is decoupled from the host operating system.

However, containers are based on a slightly different principle. Instead of a hypervisor, you need a container service (in this example the Docker Daemon). This service takes care of the management of the various containers.

Containers save a lot of resources because a completely new operating system (including binaries and libraries) is not created, but only an environment is created to operate a web server.

Containers also simplify the development of software. When I give my software to someone to test, I simply package it in a Docker container which contains all the libraries and other binaries needed to start my software.

Is there a security incident?
Trust our certified IT forensic experts in the event of attacks.
Contact us

How can I use containers?

In our example we want to set up a simple web server and show different ways to use containers. We use Docker for this.

Container Images

Images are required as a template for a container. These images are provided either by the manufacturer or the community and essentially contain a blueprint of the services that should run in the container. We'll take a look at how you can build images yourself.

Docker hub

Ready-made Docker images can be found in the Docker Hub. Regardless of whether it is a Linux distribution or a functioning mail server.

The Docker Hub software interface

Start container

To start our Apache HTTP Server as a container we simply run the following command.

				
					docker run -d --name web -p 80:80 httpd
				
			
  • -d starts the container in detached mode
  • --name sets the name of the container
  • -p80:80 links port 80 on the host system to port 80 in the container

Let's now display all containers and we will see the Web container. If we now open a browser and visit http://127.0.0.1 we will see the default page of the web server. Perfect!

Apache software

Interact with the container

To be able to execute commands in the container you can docker-exec to use. This executes commands within the container. If you now want to work within the container, simply use the following command.

				
					docker exec -it web /bin/bash
				
			
Docker

As you can see from the prompt, we are now in the container and can, for example, adjust the web server configuration, create our own website or something similar.

Create your own container image

Since manually editing each Docker container is very time-consuming, you can make any adjustments you need in an image.

For example, if we assume based on our web server that the website is already developed, we could put it into the image. This means we would only have to create a container from the image and have a functioning website.

Dockerfile

The Dockerfile basically contains the instructions for building our image.

It contains the information on which image (see Dockerhub) our image should be based. In our example on the httpd Image

Furthermore, one should apt update and the installation of additional tools can be carried out.

As a final step, Docker should copy our index.html to the website path in the container.

Dockerfile

There are two files in the current directory. The Dockerfile you just edited and the index.html that should be stored in the container.

Dockerfile

Create image

With the following command we tell Docker to create an image named apache2 based on the Dockerfile in the current directory.

				
					docker build -t apache2 .
				
			

Each command specified in the Dockerfile is added to the image as a so-called layer.

Dockerfile

After completion, we will find the image we created ourselves in the image overview apache2 image.

Dockerfile

Start container from your own image

To use our image as a template for a container, we use the same command change the image name. However on Apache2. Before searching for images in the Docker Hub, Docker first looks for suitable images in the local image storage.

Dockerfile

If we open the browser again and visit http://127.0.0.1 we will find a website with a login page, and no longer the default page from the actual httpd image.

Dockerfile
DIVIDE
Newsletter Form

Become a Cyber ​​Security Insider

Get early access and exclusive content!


By signing up, you agree to receive occasional marketing emails from us.
Please accept the cookies at the bottom of this page to be able to submit the form!
OTHER CONTRIBUTIONS

Table of Contents

PSN_KU_Cover
NewsLetter Form Pop Up New

Become a Cyber ​​Security Insider

Subscribe to our knowledge base and get:

Early access to new blog posts
Exclusive content
Regular updates on industry trends and best practices


By signing up, you agree to receive occasional marketing emails from us.
Please accept the cookies at the bottom of this page to be able to submit the form!