Development Environment with Docker

The term Docker is very widely heard in the devOps communities and big cloud or production environments with multiple distributed computing resources.

But you would be very surprised to find using Docker in the developer environment on your single developer machine would ease the development and it would be worth spending time learning and setting up for your back-end projects even though it could take some significant time and effort.

Let go though what is Docker how is it useful and how to set it.

What is Docker?

So docker is this container engine that runs container in the host operating system.

Ok, ok I know that’s text book definition,

So you must have heard about virtual machines, Where you can install a virtual machine software like VirtualBox on the host OS and then you can create a virtual environment sharing you computer resources like cpu, ram …. so on with it and install any guest OS in the virtual space that you just created.

But these virtual machines takes lots of space and memory and other resources,

Docker to the rescue, Docker takes the same concept of virtualization and comes with application(s) and file system level virtualization rather than hardware level virtualization. In traditional virtual machines the resources cpu, ram, io and so on are provided as virtual resources to the Guest OS, But in Docker the system kernel, file system is provided as virtual resource. Also the virtual environments in docker are packaged into these virtual spaces called containers. This Container environment runs in isolation from the actual host OS and other Containers.

What are the benefits of using it in the Development environment?

  • First and most important benefit, It solves the works on my machine problem, If you are able to run the container with your code on it then you can be sure that you will be able to run it any where.
  • If you are working on multiple projects then installing,maintaining and setting up dependencies like databases will be pain. Using docker you can spin the dependencies whenever you want also cleaning them will be a breeze.
  • Faster shipment, you can just build your code to a container once and ship it to multiple environments like dev, QA, staging, production saving time and valuable computing resources.
  • Its easy and faster for the new on-boarding developer to set up and cold start the project as it does not involve setting up things manually one by one.

How to set it up?

Alright, The setting up of Docker in development we all been waiting for.

Install the Docker Community edition from the Docker website. It available for all major operating system platforms. The installation is pretty straight forward. (note: If you have old windows/mac or windows home that is not supported by community edition use Docker Toolbox which uses a virtual machine to run the containers.)

Once you have docker installed and running the simple thing is to run an nginx server, nginx is a simple web server with load balancing and reverse-proxy features.

docker run -d -p 8000:80 nginx

When we run this command the docker checks if it has the container source if the source is missing then it download from the repository (default repo is Docker Hub). The -d option specifies Docker to run this in the background and -p option to set the ports to expose 8000 is the port in the host os mapped to 80 port in the container(remember if you don’t expose the port then you obviously cant access the endpoint). And as we din’t specify the name for this it pics some random name.

Now open localhost:8000 in your browser and you see the default nginx page.

Working with docker is simple isn’t it.

If you want any DB for your project then just go to Docker hub and find the official image for the DB you need and run the DB you need. it’s that simple.

To see the running process use this command.

docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
cf927d64e40b nginx "nginx -g 'daemon ..." 11 minutes ago Up 11 minutes 0.0.0.0:8000->80/tcp focused_mcnulty

To stop the server.

docker kill focused_mcnulty

It’s easy but the command can grow longer if you have a complicated setup so Docker has Dockerfile. Instead of configuring your tech stack through manual steps and command line, a Dockerfile allows you to define your infrastructure as code.

For example a simple node js application can be executed in a nodejs container like this.

FROM node:8.4WORKDIR /usr/src/app
ADD . /usr/src/app
CMD npm start

Let’s go through the file line by line, FROM node:8.4 says this image will run on top of node:8.4 . WORKDIR /usr/src/app says make this as my working directory and run the next commands here. ADD . /usr/src/app says copy all the contents from my current directory in host os to the /usr/src/app directory of the container. Now to run start the container we have to make an image out of this config we just wrote.

docker build -t nitinsreeram/my-node-app .

Once the image is created, you can use the docker images command to see all the images on your computer:

docker imagesREPOSITORY              TAG        IMAGE ID         CREATED       VIRTUAL SIZE
nginx latest 07f8e8c5e660 2 weeks ago 188.3 MB
nitinsreeram/my-node-app latest 2ac5d95f10cc 4 hours ago 123 MB

And you can just push this image to any QA/Staging/Production environment and it just runs.

For the development environment how ever in addition to Dockerfile, you can write docker-compose file to compose all the images require to run the project from databases to image-processors in one file and you can start or stop all at once that makes it so easy to control things.

Docker compose file with node and its dependency would look like.

version: '2'services:api:build: .working_dir: /appcommand: npm run start:devvolumes:- .:/app- ./logs:/var/logs/appports:- "8000:80"- "9229:9229"links:- mongodbenv_file: .envenvironment:- USERmongodb:image: mongo:3.4ports:- "27017:27017"env_file: .env

This file has node and mongo DB configured with all the ports and environment that they are going to run on.

To start use command

docker-compose up

And to stop use command

docker-compose down

As you can see setting it up for the first time is the hard part, But once everything is in its place it is so easy to start/build/deploy the project.

That’s all folks.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store