Home/DockerHosting/Docker for Dummies: Answers To Most Silly Questions

Docker for Dummies: Answers To Most Silly Questions

In this post we’ll try to understand the docker. This guide is for absolute newbies, who have heard about docker but never got chance to get hands on experience.

We’ll try to answer most silly questions that can arise in your mind when thinking about docker.

What is docker?

Docker is a platform that can be used to build and deploy applications in isolation. For example you have two applications:

  • Drupal 6 (System requirements: PHP 5.6, MySQL 5.4, Ubuntu: 16)
  • Drupal 10 (System requirements : PHP 8.2, Mysql 8.0, Ubuntu: 22)

Now if you have to setup these both applications on machine then you’ll run into multiple problems like: PHP 5.6 not installing on ubuntu 22, PHP 5.6 and PHP 8.2 compatibility issues etc.
Here docker can be a great help.
You can create two separate docker containers one with PHP 5.6 and One with PHP 8.2. They can have their independent OS, PHP, Mysql etc. that too without affecting your existing applications/configurations on your host machine.

It gives one more benefit. Now suppose configuration of some application is quite complex where you need to install many extensions and configure them, set paths etc. You just need to defined them in your Dockerfile and then same configuration can be replicated anywhere. So it will save you great time for deployment on Staging or on Production environments.

And you’ll never hear complaints like “Something is working on my local but not on production.”. You can simply use your docker configuration files (dockerfile, docker-compose.yml on production and install).

What is difference between Docker and Virtual machine?

Containers operate at the application layer. They encapsulate the application and its dependencies. Containers share the host OS kernel, making them lightweight and fast to start.
Whereas VMs operate at the hardware level. Each VM includes a full operating system.

So you might be considering if I install Docker on windows and use linux image for my docker then how can it use Host machine’s kernel (because windows and linux have completely different DNA 🙂 ).

Answer is: it utilizes a lightweight virtualization technology called Hyper-V to create a small Linux virtual machine (VM) called MobyLinuxVM. This VM acts as the Linux environment where Docker containers run. It’s important to note that this Linux VM runs alongside the Windows host operating system.

How docker can help developers in real world scenarios?

  • Running multiple applications on local system or development server which has conflicting requirements.
  • Use docker to quickly share configurations with team so that they can setup projects very quickly.
  • In scenarios where one wishes to explore or test applications without impacting the host machine’s configurations, Docker provides an elegant solution. By pulling the application’s image from Docker Hub and running it within a container, developers can experiment with various applications in isolated environments, maintaining the integrity of their host machine’s settings.

Personal Example:
Lately, we’ve been receiving numerous leads for upgrading from Drupal 7 to Drupal 10. Given that these versions require different PHP versions and that both need to run concurrently during the upgrade process, managing this setup could become quite cumbersome. To alleviate the hassle of configuring separate machines for each PHP version, we’ve developed configurations tailored for different PHP versions. Now, with just a few commands, we can effortlessly spin up a container, streamlining the entire process.
Although in this particular case we can use PHP-FPM (Fastcgi).

What is Docker image?

An image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software. It serves as a blueprint for creating containers.

In simple words docker image is ingradients and recipes packed togather. It is not complete dish, just all the raw materials and instructions to cook.

What is a docker container?

A container is a running instance of a Docker image. It is a lightweight, isolated environment that encapsulates an application and its dependencies.

It is a complete dish that has been made using the instructions in recipe and ingradients.

What is difference between docker image and container?

Docker image is a static and portable snapshot that encapsulates an application and its environment, while a Docker container is a dynamic, running instance of that image. Images are used to create containers, and containers provide the runtime environment for applications.

So you can create your custom image using dockerfile (by defining commands/instructions) or use images from dockerhub. Once the image is creating or downloaded you can then run (kickstart) that image to create a container, which will run your application.

This is how you can see running containers.

docker ps

Similarly you can list all the images on your system. you can delete those if you want to. Docker containers are instances of images, so they work independent of those (once created).

docker images

What is Docker-compose?

In most of the cases we use multiple containers. For example one for PHP/Mysql, One could be for nginx, one for Redis cache , one for corn job etc. So Docker-compose make it possible to run all at once and make them talk to each other (Share same volume, network etc.).

What is difference between Docker and docker-compose?

Docker is the broader platform for working with containers, while docker-compose is a tool within the Docker ecosystem designed for running multi-container applications.

What is Dockerfile?

A Dockerfile is a script or a configuration file used to create a Docker image. Here we simply specify list of commands that we want to run while building docker image. Here is one sample file which uses official php image and also create virtual host using apache.

# Use an official PHP image with apache2
FROM php:8.2-apache

# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

# Set memory_limit to 512M
RUN sed -i -e 's/memory_limit = .*/memory_limit = 512M/' "$PHP_INI_DIR/php.ini"

# Install required PHP extensions and tools
RUN docker-php-ext-install pdo pdo_mysql mysqli bcmath
RUN apt-get update && apt-get install -y \
		libfreetype-dev \
		libjpeg62-turbo-dev \
		libpng-dev \
	&& docker-php-ext-configure gd --with-freetype --with-jpeg \
	&& docker-php-ext-install -j$(nproc) gd

# Set the working directory in the container
WORKDIR /var/www/html

# Copy Apache virtual host configuration file
COPY apache-config.conf /etc/apache2/sites-available/000-default.conf

# Enable Apache modules
RUN a2enmod rewrite

# Expose port 80
EXPOSE 80

# Start Apache in the foreground
CMD ["apache2-foreground"]

What is docker-compose.yml?

docker-compose.yml is used by docker-compose tool. When we run:

docker-compose up -d --build

then it reads that yml file and runs those images into containers and configure networks etc. Here is sample docker-compose.yml file:

version: '3'

services:
  # Service 1: Web Application
  web-app:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./web-app:/usr/share/nginx/html
    networks:
      - custom-net

  # Service 2: Database
  database:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: example_root_password
      MYSQL_DATABASE: example_db
      MYSQL_USER: example_user
      MYSQL_PASSWORD: example_password
    volumes:
      - ./database/data:/var/lib/mysql
    networks:
      - custom-net

  # Service 3: Backend API
  backend-api:
    image: node:14
    working_dir: /app
    command: npm start
    volumes:
      - ./backend-api:/app
    networks:
      - custom-net

networks:
  custom-net:
    driver: bridge

Will docker add extra load on my server?

Docker is known for its lightweight nature, especially when compared to traditional virtual machines. Unlike virtual machines that require a complete operating system stack for each instance, Docker containers share the host system’s kernel, making them highly efficient and resource-friendly.

If you’re running a single application on your server without the need for multiple isolated environments, the use of Docker might introduce minimal overhead. In fact, it can even optimize resource utilization by enabling the deployment of multiple isolated applications on the same host.

However, it’s essential to consider the specific requirements of your application and the trade-offs involved. For scenarios where the benefits of containerization, such as portability and isolation, are not crucial, the additional layer introduced by Docker might be considered as a slight overhead. It’s always recommended to assess the specific needs of your use case and evaluate whether Docker’s advantages align with your deployment goals.

Leave a Reply

We'll try to resolve your queries asap.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

2