Introduction to Docker for Web Development

Introduction to Docker for Web Development
Introduction to Docker for Web Development

Popularly known as ‘hotter than hot’, Docker is your solution to the ‘it works on my machine’ problem. In addition to this, docker is open source, meaning, very fewer chances of having bugs since it is being worked on by a lot of developers and is being reviewed and managed by a couple more.

It helps one to make sure their code in production does not fail and works the same way it did in development.  No code breaks = Reliability of your applications. Pretty amazing isn’t it?

Why do you need Docker?

Docker wraps the code and the necessary libraries and packages in a container and makes sure that the code that was built and tested on the developing team’s machines is the same that gets plugged and played on VMs, on cloud and on the user’s machine. With Docker, the web developer has the option of segregating the services he wants to use for his application in different containers.   

Docker Containers Vs Virtual Machines

Figure 1 © Cloudboost – Concept of Docker Containers and VMs

As you can see in the image, a virtual machine has a separate bin, libraries and OS for every part of the application. Whereas in containers, everything is shared ranging from bins, libraries, OS and the server. In addition to this, virtual machines feast on resources while Docker containers help make your applications fast and efficient without the need of utilising any heavy resources.  

Use Of Docker in Web Development

1. Cross-Platform Production and Testing: When the developers are building and testing the application, often they have to optimise their applications for it to work on different platforms. With docker, testing and deployment become a boon for the developer. The deployment gets automated, the testing becomes simpler and hence time, efforts and money are saved. 

2. Docker Images: Docker images are readymade templates which will be acting as a container for your final web application. Docker’s Hub has images for PHP, HTTPd, Apache, Ubuntu, Mongo, MySQL, Node etc. You can find the docker image you need which resonates with your technical stack by visiting the page

3. Community: Docker has one of the best developer communities where people can engage, contribute and grow with each other. Here is the link to join the Docker community: https://www.docker.com/docker-community 

Breaking Down the Docker Architecture

Figure 2 © Docker – Docker Architecture
  • Docker Daemon: It handles Docker API requests and manages images, containers, networks and volumes in addition to handling services by Docker. 
  • Docker Client: The docker client is the source of the user’s interaction with Docker. The docker client makes communications with the docker daemons. 
  • Docker Registries: A docker registry is the store of the docker images. 
  • Docker Objects:
    • Images: Readymade template which contains instructions about a docker container. These images are light, small and fast. 
    • Containers: A container, as defined by the official docker documentation, is a runnable instance of an image. 
  • Services: Services are the tools offered by Docker which allow containers to be scaled across Docker daemons.

Docker & Angular

If you are an angular developer and want the web application you created using Angular to have all the benefits mentioned above, you now want to “dockerize” it. First, you need to install docker on your machine and create an account on docker hub. It is a free service and hence, you can easily access it.

You can dockerise your angular application in four easy steps: launch docker on your machine, create a docker file in the same directory where your angular application is, create a docker image from the docker file you created in the previous step and finally run the angular application in the Docker container using the image you created in the previous step.

Docker & NodeJS

The prerequisites for dockerising your NodeJS application are having docker and nodeJS on your system and obviously, a NodeJS web application. I am assuming you are done testing your node application, if not you can run it on a local server and send some JSON and try to fetch it using a GET request and see if that gets saved. If not, you might want to take care of some internal bugs before deploying it in docker containers.

After you’re certain everything works perfectly, you now need to create a docker image. Docker Hub has a predefined image for the node which has all the necessary support required to run your application. After you’re done creating an image, you need to build your docker image. The commands for all these can be found in the official docker documentation with extremely descriptive examples. And finally, you need to run your image in the docker container.

blog banner 1

Docker & PHP

Refer to the official Docker documentation to make sure you have installed Docker properly on your machine. In addition to this, you need to have composer – the package manager for PHP, installed on your system. Next, you need to have a PHP project ready. It could be a project you wrote in plain PHP or by using a framework, every PHP application will be able to get dockerised if you follow the right procedures. Create a new dockerfile for your application, direct the web host to your application, build the docker image and finally create a container using the image you just created.

Docker & MySQL

Applications require databases to be connected with them for collection and maintenance of logs and user data. When you will be creating a docker container for your web application, you will come across a step wherein you will need to create docker-compose.yml file for development purposes. This file will be the gateway to the connection of your database and your application inside a docker container.

Docker & Apache

Docker is also a tool for providing administrative access to developers and users of a certain web application. You can set up an Apache web server right from your terminal with the help of already existing and available docker images on DockerHub.

Resources

Interested yet? I’m sure. In case you wish to learn more about docker and how everything works, given below are some resources for you to start:

Docker Tutorial For Beginners by FreeCodeCamp
Freecodecamp is a widely acclaimed and trusted educational non-profit organisation which offers ad-free content. You can check out their website and YouTube playlists for more.
Language: English

Introduction to Docker by Jacky Zhao
This introduction was given as a part of Gear Up by Hack the North and it contains a detailed walkthrough of all the basics you need to understand to get your application up and running in no time.
Language: English

Books:

  • Docker: Up and Running by O’Reilly
  • Docker for Web Developers by Craig Buckler
  • Docker for Developers by Rafael Gomes

To know more about our courses and read more blogs, please visit our website.

By Pooja Gera