What is Docker?

Overview

Imagine you are creating a new web app. Like most apps, you may need to configure a database, a web server, and an API server. You then need to set up your database schema and connect them all together, not to mention the networking involved. Finally after hours of slaving you have it all connected and you create a beautiful app. Now time to do it all again in production, only if there was a better way… there is. With docker, you essentially can copy and paste it all.

Docker allows us to spin up environments with the click of a button, and as many as we want, ensuring they are all the same. For local development, this is amazing as it installs all the dependencies for you. Imagine you want to start up your app: the database, web server, and an API server. Even if you had just acquired a new computer and have no programs installed, you could install Docker and run a single command to get all the pieces up and running. Even better, if you did have all the correct languages and libraries installed, but they were outdated, there would be no need to upgrade them as there would be no conflicts.

How Does It Work?

I will be giving a high level of how Docker works and what it can be used for. If this sparks your interest a good book to pick up would be Docker Deep Dive as Nigel gives a great walkthrough.

With Docker you can create something called an Image from a config file, called a Dockerfile. When you run the Docker build command it `cooks` a new Image. This image can be shared or uploaded to the web, or even used as a base to make another Image. Once you have this Image, that has all the setup for your environment, you can start it as if it was its own system. You can even spin up as many of them as you would like. Say your Dockerfile looks like this one below:

FROM node
EXPOSE 3000
COPY . ./app
RUN yarn install
CMD yarn start

The above Dockerfile defines a simple node app that would be used to create your app server Image. Once you create the Image you can deploy as many containers as you need. Take for example you deploy it to Google Cloud Run. Your app then starts getting thousands of requests so 100 Containers are spun up from the new Image you created. As more requests come in even more Containers can be brought up with the same exact environment specs as you defined.

Cool so now we know how to make one piece of the puzzle but what about the rest of the setup, the database and api server. Well we can use something called Docker Compose for this. It allows us to define a whole set of environments than with one command it will create all the images needed. With the docker-compose.yaml file below you can run docker-compose build and all 3 images will be created: database, app server, API server.

version: "3"

services:
  dev-mariadb:
    image: dev-mariadb
    build:
      context: ./dev-database
    container_name: dev-mariadb
    environment:
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_DATABASE=dev_wordpress
      - MYSQL_PASSWORD=password
      - MYSQL_USER=admin
    networks:
      - dev_frontend_network
    ports:
      - "3307:3306"
  frontend:
    image: frontend
    container_name: frontend
    build:
      context: ./frontend
      dockerfile: ./config/docker/Dockerfile.dev
    networks:
      - dev_frontend_network
    ports:
      - "3000:3000"
    expose:
      - "3000"
    volumes:
      - ./frontend:/app
  backend:
    image: backend
    container_name: backend
    build:
      context: ./backend
    networks:
      - dev_frontend_network
    ports:
      - "8080:8080"
    expose:
      - "8080"
    volumes:
      - ./backend:/var/www/html
    environment:
      WORDPRESS_DB_HOST: dev-mariadb
      WORDPRESS_DB_NAME: dev_wordpress
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_USER: admin
networks:
  dev_frontend_network:

With the above config file, we can ‘cook’ as many Images as we need. Each item under services will output an image when building. If we run docker-compose up it would not only create the Images but spin up a container for each. Given the above config, it tells us that:

  • Our app will be available on localhost:3000
  • Our API server on localhost:8080
  • Our database on localhost:3307

Conclusion

We have only scratched the surface of what Docker can do. Docker is extremely powerful and allows us to build very complex and scalable systems. Docker is also an amazing tool if you are moving to a microservice architecture. If you are interested in other use cases or a more in-depth discussion on the power of Docker feel free to let us know in the comments below.

Thank you for reading and please feel free to share using the links below!

One Reply to “What is Docker?”

Leave a Reply