- When you want to ship your app across multiple environments and get the same expected behavior consistently on them.
- When you want to minimise the risk of ’it works on my machine’ development.
- When you want to enable CI/CD within your development teams. Using docker containers means that your build pipelines can all use the same set of instructions to validate new code changes to your applications.
- When you want to enable application development for developers who might not all use the same operating system but experience the same application performance.
- If building a backend API could you split the API into a collection of serverless functions (AWS Lambda’s, Azure functions, etc…). This would be much more cost-efficient and speedy than Docker. Due to it’s set up but also because Docker will need to run on a dedicated piece of computing power all the time. Whereas serverless only uses compute power when it is needed.
- If you require persistent volumes for data storage, this can be challenging to maintain with Docker.
- An added layer of complexity to host your applications will mean that you need to put more pressure on your hosting machine.
Firstly for your application, you are going to need to write a ‘Dockerfile’ at the root of your project. This will contain a set of instructions to tell the Docker agent how to build your container.
See the example below:
We pull from ’alpine’ which is a lightweight linux image to base our container from.
Then we run a bash command to echo the words ‘Hello world!’
FROM alpine CMD ["echo", "Hello world!"]
Save the above file as ‘Dockerfile’
docker build -t hello . docker run --rm hello
This will build the Docker image firstly and give it a tag name of ‘hello’ and scope the Dockerfile to the root of the directory as we specified the ’.’ character.
Then it will run the container and kill it after.
Which should output:
🎉 Congratulations! 🎉 You’ve just built your first Docker container.
To summarise, Docker is a great way of bundling your applications up for development across multiple environments and enabling multiple developers to work on active app development. It will enable a lot more sophistication in your CI/CD process going forward but, at the cost of a more heavyweight solution (compute required and time spent getting setup). It’s important to think about serverless and if it can fit your needs as opposed to containerization. But hopefully this has given you a brief enough insight into Docker that you can start making informed decisions going forward.
Download and Install Docker from here: https://docs.docker.com/get-docker/