Docker 101
What's Docker
Docker is a way to package software so it can run on any hardware.
Key components of docker
- dockerfile - a blueprint (which contains code) for building a docker image
- image - a template for running docker containers
- container - a running process
How does the process of docker work
For instance, if you have a node js app, how can you dockerize it to install and run on any machine (Linux, Windows, or MacOS)?
You do this by creating a dockerfile in the root of the project.
These are the instructions for creating a dockerfile:
How to build an image
Run a container (locally)
Before running a docker container on your local machine, it’s good practice to implement port forwarding from a docker container to your local machine:
Note: you should have docker installed locally
Note that any state or data you have created inside a container will be lost when you stop the container. However, there can be situations where you want to share data across multiple containers. A preferred way to do that is with Volumes:
A volume is a dedicated folder on the host machine. Inside this folder, a container can create files that can be remounted into future containers or multiple containers at the same time.
This is how you create a volume:
You can also execute commands in your container by clicking on the CLI button.
However, one of the best things you can do to keep your containers healthy is to write simple maintainable microservices. Good tip: 1 process per container. For multiple processes, it’s better to use multiple containers. Docker Compose can help you with that.
Docker Compose
It’s a tool for running multiple docker containers at the same time.
For instance, your nodejs app needs to access a MySQL database. Additionally, you can volume to persist the database across multiple containers. You can manage all of that with Docker Compose by creating a docker-compose.yml file at the root of the project.
To run all the containers together use this command:
That's a wrap!
Reference:
- https://www.youtube.com/watch?v=gAkwW2tuIqE&ab_channel=Fireship
Comments
Post a Comment