What is Docker?

NB!!!!

RESOURCES

Overview

What is a Container?

  • A way to package applications with all the necessary dependencies and configuration - no more "I don't know why, but it works on my machine!!"

  • Makes development and deployment more efficient

Where do containers live?

As containers are shareable, portable and can move around - so where do these live so that they can be shared and moved around?

  • Container Repository - special type of storage for containers

    • Private Repositories - companies store their own containers

    • Public Repositories - DockerHub

Application Development

BEFORE Containers

  • Installation of OS as well as applications (PostgresSQL, NGINX etc) would be different on each developers laptop, one could be on MAC and others on different Linux distros.

  • Many steps during these installations and something could go wrong, tedious etc

  • The developers would produce an application with a database, as well as a set of instructions on how to deploy it. This would be handed over to the Operations guys where this application would be deployed directly onto the OS

  • This led to dependency issues, version conflicts as well as backwards and forwards between the 2 groups, resulting in misunderstandings, delays and frustrations

AFTER Containers

  • No applications need to be installed directly on your OS as the container is in its own isolated environment.

  • Packaged with all needed configuration

  • One command to install the app

  • Able to run same app with different versions

  • Here the developers and operations work together in packaging the applications into a container. No applications are directly installed onto the server, so no server configurations are required.

  • The only thing that would be needed on the server would be the 'docker runtime' - the docker would pull the container from a repository and all would be configured without interfering with the server OS

What is a Container Technically?

From the previous section we know what the concept of a container is, but technically a container is made up of layers of stacked images, with the base image a SMALL Linux distro (usually Alpine) and then stacked on this are the other intermediate images that ultimately leads up to the application image (e.g. NGINX, PostgreSQL etc)

Example 1

Go to hub.docker.com and search for postgresql:

Here the container is when the image is pulled into the local machine and docker and is running otherwise the image is just an artifact that can be moved around.

Docker vs VM's

Operating system has 2 layers - OS Kernel and Applications all o top of the Hardware layer

Docker virtualises the APPLICATION LAYER and uses the kernel of the host. VMs virtualise the OS kernel as well as the application

Here we have 2 instances of nginx running and both listening on port 80:

Container ports vs Host Ports

So, from the NGINX example, we need to map the ports when we RUN the containers. So we will need to stop both instances of NGINX, and run them again by mapping (port binding) different HOST ports to the same CONTAINER PORTS

docker run -d <host port>: <container port> nginx:1.20.1

Docker Debugging / Troubleshooting

docker logs <container-id> or <conatiner_name/name> docker exec -it <container-id/name> /bin/bash - get into the terminal of a container. The option -it stands for "interactive terminal"

Demo Project - Overview

Workflow with Docker - Its important to see how docker works in practice. In software development workflow you have the classical steps of:

  • Development

  • Continuous Integration/Delivery

  • Deployment on some environment (test/production etc)

and so its important to see how docker integrates into the above steps

Workflow with Docker

Were going to use a JavaScript (JS) and Nodejs application in the back-end to simulate local development process and connect this to a docker container with a Mongo D database in it.

We;re going to use MongoDB container as well as MongoExpress container (UI used to manage the mongodb)

So we have issued the commands:

docker pull mongo docker pull mongo-express

So we want to connect the mongo express container to the mongodb container

Docker Network

docker network -ls

If containers are on the same machine they can communicate between each other using their container names as they are in the same network. Now nodejs will run OUTSIDE the isolated docker network from 'node server' and will connect to them using the localhost and port number

Later when we package the application into its own docker image

The browser which is outside the isolated docker network, but on the localhost will connect via hostname and port number:

We are going to create a mongodb network:

And to add the new containers to this network we will need to stipulate them when we create the containers. Checking on the usage of mongodb:

docker run -d \ -p 27017:2717 \ -e MONGO_INITDB_ROOT_USERNAME=admin \ -e MONGO_INITDB_ROOT_PASSWORD=password \ --net mongo-network \ --name mongodb \ mongo

OK so we have created the mongodb container now we need to create the mongo-express container and ensure that the correct credentials are made etc. Checking on the mongodb-express docker info on dockerhub we see

docker run -d \ -p 8081:8081 \ -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \ -e ME_CONFIG_MONGODB_ADMINPASSWORD=password \ -e ME_CONFIG_MONGODB_SERVER=mongodb \ --net mongo-network \ --name mongo_express \ mongo-express

There is a tool that makes this easier (ie not issuing all these docker run commands) and that is DOCKER COMPOSE. Here we take all the commands we would use and map it to a file: HERE IS THE DOCKER-COMPOSE file for the MONGODB CONTAINER AND MONGO-EXPRESS CONATINER IN ONE YAML FILE

This just continues - its a SINGLE FILE :)

Docker compose takes care of the common network - we dont have to add it into the YAML file

Creating the Docker Compose File

Dockerfile - A blueprint for creating docker images

What is a docker file? To build a docker image from an application we have to copy the contents of that application into a docker file (copy artifacts - jar, war, bundle.js etc)

Last updated

Was this helpful?