Docker: For development setup

Summary

Docker can expedite local development environment setup. If done right, setting up development environment can be fast, really fast, really really fast.

The Premise

In one of the recent projects, we had a requirement to convert webm video to mp4. We used ffmpeg binary to do this. If using ffmpeg binary, install(on whatever OS) ffmpeg and run something like

ffmpeg -i input.webm output.mp4

In Action

Docker makes it easy to solve some of these issues. There are many ways you can go about it, of that, one of the ways is.

  1. Build the container
  2. Push the build to remote repository
  3. Others pull the docker image.

Instant gratification

We will cover how to go about this in the implementation, but for instant gratification, if you have docker installed, run the following

docker run -it --rm ch4nd4n/docker-node-hello node

Implementation

For sake brevity, let’s cite this with a very simple node example(instead of ffmpeg).

touch index.js
// index.js
console.log('Hello');
node index.js
  1. Create index.js
  2. Build docker image
  3. Run the docker container

1. Create Dockerfile

echo "FROM node:alpine" > Dockerfile

2. Create javascript file

echo "console.log('hello')" > index.js

3. Build Docker

docker build -t tmp-node-app .

4. Run Docker container

docker run -v $PWD:/home/node --rm tmp-node-app node /home/node/index.js

Sprucing it up with docker-compose

Let’s use fastify to create a very simple server.

  1. Update Dockerfile
  2. Add docker-compose.yml
FROM node:lts-alpine
version: "3"
services:
fastify:
build:
context: .
container_name: fastify
working_dir: /home/app
volumes:
- .:/home/app
ports:
- "3000:3000"
command: sh -c "npm install && npm run dev"
const fastify = require('fastify')({ logger: true })// Declare a route
fastify.get('/', async (request, reply) => {
return { hello: 'world' }
})
// Run the server!
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
fastify.log.info(`server listening on ${fastify.server.address().port} or is it?`)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
{
"name": "docker-node-hello",
"version": "1.0.0",
"description": "Docker fastify gratification",
"main": "index.js",
"dependencies": {
"fastify": "3.9.2"
},
"devDependencies": {
"nodemon": "^2.0.6"
},
"scripts": {
"dev": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
docker-compose up
redis:
image: redis:alpine
const redis = require('redis');
const client = redis.createClient('redis://redis:6379');
// add current date as a key when app starts
client.set('foo', new Date());
fastify.get('/', (request, reply) => {
client.get('foo', (err, redRply) => {
console.log({ redRply })
return reply.send({
msg: redRply
})
});
});
curl http://localhost:3000/

We are business accelerator working with startups / entrepreneurs in building the product & launching the companies.

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