Dockerizing an Application
The following is a brief description of Docker Containers and what they have to offer based on the getting started page of the docker site.
Building and deploying applications can be made faster with containers. Docker containers wrap up software and its dependencies into a standardized unit for software development that includes everything needed to run: code, runtime, system tools, and libraries. This guarantees that the application will always run the same and makes collaboration much simpler (see docker's get started page). The containers help to ensure that all environments have the exact same setups.
Docker containers whether Windows or Linux are backed by Docker tools and APIs helping to build consistent environments:
- Onboard faster and stop wasting hours trying to set up development environments, spin up new instances and make copies of production code to run locally.
- Enable polyglot development and use any language, stack or tools without worry of application conflicts.
- Eliminate environment inconsistencies and the "works on my machine" problem by packaging the application, configs and dependencies into an isolated container.
- Alleviate concern over application security
Try Docker containers with free, hosted lab tutorials or download and take a tutorial to start building apps.
Branching Strategy
Every section of the lesson has a corresponding branch. The branches are listed in the notes for that particular section but as an overview I have listed out the branches below.
master - final README.md documentation for the lesson but does not have the full demo application
develop - has the final version of the application along with the docker setup
feature-dockerfile - Single Docker Container for the beginning of the lesson
feature-docker-compose - Adding Docker Compose in order to setup the idea of what Docker Compose is because it's needed in order to setup multiple containers
feature-docker-compose-db - Database Container setup within Docker Compose
feature-docker-compose-server - Server Container setup within Docker Compose
feature-base-app - base application with need of only one container for client-side
feature-server-app - full stack application with DB Schema, Server, and Client-Side
Introducing the Issue
We have a simple Client Side / Front-End Application that we want to maintain across different teammates and different environments. Typically we would be asking our team to run:
- npm install
- npm start
After that we would expect that they should be able to run and develop for the application locally. Seems pretty straight forward at first glance but...
- What if someone on the team is on Windows and not Mac?
- What if someone on the team has an older version of Node.js loaded that isn't compatible with our application?
- What if someone on the team doesn't even have Node.js installed?
- Now what happens if we start adding a Server API and Database Schema to be setup for our application? Our issues have just been compounded.
Luckily Docker can help us to solve these issue by handling environment setup, dependency loading, and launching of our application all in one place. Best of all regardless of who is using it the environment will run consistently for any team member working on the project.
Lesson Stages
Installing Docker
We navigate to Docker's Get Started page in order to start downloading Docker. Please see the DOCKER_INSTALLATION.md file for a step by step installation for Docker.
Single Docker Container
We're gonna start by looking at how we setup a single Docker Container. This single Container will be used to launch and run a React application built using create-react-app as the base setup.
Dockerfile, Docker with Single Container
In order to setup a single docker container for your development environment you need to create a file named Dockerfile in the root of your project directory.
In order to set the base environment we use an existing image from the Docker Hub using the FROM setting in the Dockerfile.
# Base image we are modifying from https://hub.docker.com/
FROM node:current-alpine
Make a new directory and set it as the working directory for the Docker image.
# set working directory
RUN mkdir -p /app
WORKDIR /app
Copy over the existing package.json to the new working directory and install the application dependencies using npm install.
# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install
After dependency installation copy over all assets to the working directory.
COPY . /app
Ensure that the default create-react-app port is exposed to the network created by Docker.
# Exposing a specific PORT for viewing the application
EXPOSE 3000
Define the final command(s) that need to be run to kick off the application when the container launches.
# Run final command to kick off client build
CMD ["npm", "start"]
Dockerfile Commands
That's great we have a configuration for Docker but how do we actually use it. Because we downloaded the Docker desktop application we now have access to the command line tools which we'll be leveraging to build our Docker Image and then run the Docker Container.
Build and Tag the Docker Image:
docker build --tag=[CONTAINER_NAME] .
docker build - is the command used to build our application's Docker image
--tag=[CONTAINER_NAME] - the tag flag assigns a tag or name to the built docker image so it can be used to run the docker container based on the image
[CONTAINER_NAME] - is a placeholder and can be whatever you want to use in order to identify the Docker image
. - is a required argument showing the location of the Dockerfile to be used for the Docker configuration
Running the Docker Container:
docker run -p 3001:3000 [CONTAINER_NAME]
- The docker run command creates a new container instance, from the image we just created, and runs it.
-p 3001:3000 exposes port 3000 to other Docker containers on the same network (for inter-container communication) and port 3001 to the host.
- Additionally the
-d option can be used in order to run the container in detach mode.
Warm Reloading with create-react-app:
Our application was built using create-react-app which does support web page reloading.
To make this work, we need to do two things:
- Mount the current working directory into the Docker container
-v $(pwd):/app, will be added to our docker run command
- Expose the WebSocket port
- The WebSocket thing is set up by exposing port 35729 to the host (
-p 35729:35729).
- Add
EXPOSE 35729 to our Dockerfile just below the other exposed port.
-p 35729:35729, will then also be added to our docker run command
When we run our container the command should now be:
docker run -p 3001:3000 -p 35729:35729 -v $(pwd):/app [CONTAINER_NAME]
Adding Docker Compose
We had to add a lot of additional settings to the Docker commands but we can repurpose them as configuration settings in a new Docker yaml configuration file. This configuration file can hold many of these custom settings along with eventually letting us run and configure many different Docker Containers.
Create a new file called docker-compose.yml at the root of the project.
The configuration settings in the docker-compose.yml file are as follows:
version: '3'
services:
##
## CONTAINER for Client Side Application
## to test service run:
## docker-compose up --build -d client
## ----------------------------------------
client:
build:
context: ./
ports:
- 3001:3000 # expose ports - HOST:CONTAINER (for create-react-app)
- 35729:35729 # expose ports - HOST:CONTAINER (for serviceworker warm reloading)
volumes:
- '.:/app'
- '/app/node_modules'
command: npm start
Let's breakdown what the Yaml configuration settings are.
version - sets the version of the Docker Compose Yaml file we are working with
services - the names written as children of this define the different containers that we want to create like our client container
- For the individual
client container:
build - allows us to set custom build settings for this particular container
context - sets the build context and if dockerfile is not set to a specific file it will look for a Dockerfile existing in the root of the directory defined in the context
ports - here we define the various ports that we want exposed from our container to our host
HOST:CONTAINER - the HOST port is the port that will be available on your computer @ http://localhost:HOST and CONTAINER represents that port that is being exposed inside the Docker Container
- this setting replaces the
-p 3001:3000 -p 35729:35729 options that were added to the docker run command we used previously
volumes - mounts host paths or named volumes, specified as sub-options to a service.
- this setting replaces the
-v $(pwd):/app option that were added to the docker run command we used previously
command - this should be the run command needed to start the application and it will override the CMD set in the Dockerfile
Docker Compose Commands
Docker Compose Commands
- In order to standup our new setup run:
docker-compose up
- Builds, (re)creates, starts, and attaches to containers for a service. Unless they are already running, this command also starts any linked services.
- Running
docker-compose up -d starts the containers in the background and leaves them running.
- In order to view the containers run:
docker-compose images
- List images used by the created containers.
Running Multiple Containers
The biggest benefit to using the docker-compose.yml is that we can now run multiple containers from the one Docker Image allowing us to spin up an entire full stack application. Where as before we had to run create db, several SQL queries, npm instal, npm run server, & npm run client in order to spin up our application and develop locally with Docker Compose we just run docker-compose up and it's all taken care of.
Database Container
- create a
database directory in the root of the project folder
- create an
init.sql file inside of the database directory
- this file will contain all of our queries for our database tables
- create a
data.sql file inside of the database directory
- this file will be used to seed some initial data into our tables for local development
Before we used these types of files to make it easier for other people setup the local environment for our application but now we can use it to actually setup and run our database. Now let's configure Docker Compose with a new container to handle our database.
add a new container to the docker-compose.yml file for the database: