DEV Community

loading...
Cover image for Docker 101 - For Beginners

Docker 101 - For Beginners

willholmes profile image Will Holmes ・3 min read

What is it?

Docker is a containerization platform that bundles your applications and all their dependencies into something called a 'Docker container' so that your application can run agnostic of what environment it is in. This also ensures that your application can run quickly and reliably from each environment.

https://www.docker.com/sites/default/files/d8/styles/large/public/2018-11/container-what-is-container.png?itok=vle7kjDj
Credit: https://www.docker.com/resources/what-container 👆

When should I use it?

  • 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.

When should I not use it?

  • 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.

How is it used?

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!"]
Enter fullscreen mode Exit fullscreen mode

Save the above file as 'Dockerfile'

Then run:

docker build -t hello .
docker run --rm hello
Enter fullscreen mode Exit fullscreen mode

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:

Hello world!
Enter fullscreen mode Exit fullscreen mode

🎉 Congratulations! 🎉 You've just built your first Docker container.

Conclusion

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/

Documentation: https://docs.docker.com/engine/reference/commandline/docker/

Discussion (1)

Collapse
oraclesean profile image
Oracle Sean ♠️

"Data persistence is challenging" is a myth. The -v and --mount options address data persistence with no more effort than it takes to map ports.

Forem Open with the Forem app