This article provides an introduction to Docker for a non-technical audience.
Why would non-technical people care about docker?
Within my organisation, like many others, we have mixed technical and non technical team members. Within the tech team, we are often using jargon and tech terms. I think it is important to educate and make sure, all staff, no matter tech background, can understand to a basic level, what we are talking about.
Docker is actually a large and quite complex application and eco system. There are many advanced features. For this particular article, I want to narrow down the focus to a particular use case of docker, which is to help us setup our computers and laptops ready for application testing and development. This particular use case is often referred to as a development environment (DevEnv).
What do I mean by that?
Essentially, what I can going to talk about is how we can use docker to create a virtual machine on your laptop that acts like a server. Docker can help us achieve this in a consistent and efficient manner.
This is a simplified scenario, but typically, if we want to develop an application, we need a server, with two main components, the web service part, and the database part.
Docker DevEnv Components
I am going to use a php laravel example that you can read more about if interested. In this explanation, I am going to concentrate on two main components:
- Web service part is so we can www.application.com and get a result.
- Database part is so we can store/retrieve information.
I am going to explain with these components using two files. There is a lot more happening here, but for simplicity, I am just going to concentrate on two files. A Dockerfile and a docker-compose.yml file. These are both in yaml format, so are easily readable.
As you can see from the Dockerfile, this describes the basic server setup. This is the virtual server that will be setup on your laptop when starting docker with these files.
Three main parts I want to draw your attention to. Lines 1 and 2.
#Ubuntu 18.04 Base Image - Nginx + MySQL + PHP 7.2 [LEMP] FROM ubuntu:18.04 ...
This described the type of server operating system. This could be CentOS etc, but in this case, it is Ubuntu.
Lines 42 to 50 show certain php libraries being installed. In this case, they are installed as per the latest version for this operating system, Ubuntu 20.04. But, depending on OS, we can specific the version. This would be something we are going to look at in order to replicate our production servers and closely as possible.
... php-fpm \ php-cli \ php-common \ php-zip \ php-mbstring \ php-bcmath \ php-image-text \ php-gd \ php-mysql \ ...
Lines 53 to 55 show the installation of database, in this case, mysql.
... mysql-server \ mysql-client \ mysql-common \ ...
Here is a look at the entire dockerfile in this example.
#Ubuntu 18.04 Base Image - Nginx + MySQL + PHP 7.2 [LEMP] FROM ubuntu:18.04 #Label LABEL version="1.0" LABEL name="ubuntu-base+nginx+mysql+php" #Set TZ ENV TZ=Europe/London RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone #apt update & install RUN apt-get update && apt-get install -y \ git \ curl \ libpng-dev \ libonig-dev \ libxml2-dev \ libzip-dev \ zip \ unzip \ graphviz \ npm \ libxpm4 \ libxrender1 \ libgtk2.0-0 \ libnss3 \ libgconf-2-4 \ chromium-browser \ xvfb \ gtk2-engines-pixbuf \ xfonts-cyrillic \ xfonts-100dpi \ xfonts-75dpi \ xfonts-base \ xfonts-scalable \ imagemagick x11-apps \ libwebp-dev \ libfreetype6-dev \ libjpeg62-dev \ libpng-dev libxpm-dev \ php-fpm \ php-cli \ php-common \ php-zip \ php-mbstring \ php-bcmath \ php-image-text \ php-gd \ php-mysql \ composer \ nginx \ mysql-server \ mysql-client \ mysql-common \ php-xml \ php-curl
A few keys points from the docker-compose file. If you look at the first block, this is essentially starting up the server, using the image as you have just see in previous section dockerfile (it is actually a bit more complicated than that, but, for simplicity).
... # LEMP Server from image app: image: lemp-dev:v2 container_name: webapp env_file: - .env working_dir: /var/www/html/ ports: - 80:80 - 443:443 - 3306:3306 volumes: - ../www:/var/www/html/ networks: - lempnet tty: true ...
There are some settings that you don’t have to understand for the purposes of our use case in the section.
The second part is starting phpmyadmin from the official image on the docker hub web repository.
... # DB access via PHP my admin phpmyadmin: image: phpmyadmin container_name: phpmyadmin environment: - PMA_ARBITRARY=1 restart: always ports: - 8181:80 networks: - lempnet ...
Here is a look at the complete docker-compose file
version: "3.3" networks: # docker internal network lempnet: driver: bridge services: # LEMP Server from image app: image: lemp-dev:v2 container_name: webapp env_file: - .env working_dir: /var/www/html/ ports: - 80:80 - 443:443 - 3306:3306 volumes: - ../www:/var/www/html/ networks: - lempnet tty: true # DB access via PHP my admin phpmyadmin: image: phpmyadmin container_name: phpmyadmin environment: - PMA_ARBITRARY=1 restart: always ports: - 8181:80 networks: - lempnet
Here is a look what the docker desktop application looks like on MacOS.
You can read more about how I have used docker in my other projects.