Start Docker - Digging
Tags: docker, container, node, express
Published: 2024-01-14 15:29:31

Hello Everyone!

You can find a lot of resources about Docker, and I’m here to share what I’ve learned. Let’s start by going through the basics of Docker, skipping over the details of what Docker is and how it helps in development or production mode.

Here are some keys to know about Docker:
1. Images (Pictures)
2. Containers (Boxes)
3. Dockerfile (Recipe)
4. Docker Commands (Orders)
5. Docker Compose (Teamwork)
6. Docker Registry (Storage)
7. Volumes (Storage Spaces)

I'll go straight to the point and share what I've done with Node.js and Docker. In my project, I built a simple Node.js server using Docker. In the project repository you will find a Dockerfile. It’s like a set of instructions that we can use to build our Docker image. Check it out, and you'll see a dockerignore file as well. This file helps us to exclude certain files and folders while creating our Docker image.

Take a look at the repository:
https://github.com/devkishor8007/docker-digging

And I'll share you how we can run the repository from docker container
To run, we need some sort of commands

```
$ docker build -t docker-digging . 
$ docker run -p 4075:4075 --env-file=.env docker-digging
```
 
![running-server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8qnbjs64qoy6wtreuyi.png)

Once your server is running; 
Open the browser and hint the api url> http://localhost:4075/api/v1/health

![happy-emoji](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ko8fpir3ao63du69tfi1.png)

Awesome!
Everything is working perfectly! If we're not familiar with the commands we're using, and I'll help you out.
 
So, Let's break down each part of above docker container commands
1. `docker build -t docker-digging . : `
`docker build`: Build a new docker image .
`-t docker-digging`: -t means a flag assigns a name to the image. And we named "docker-digging"
`.`: This dot represents the current directory as Docker should use the current directory as the build context. It includes all the files in the current directory when building the image.
So this commands represents build a docker new image from a current directory with a name "docker-digging"

2. `docker run -p 4075:4075 --env-file=.env docker-digging`
`docker run`: This command to run a Docker container from a given image.
`-p 4075:4075`: -p flag maps port 4075 on the host machine to port 4075 inside the Docker container. It allows us to access the service running in the container via http://localhost:4075
`--env-file=.env`: --env-file flag specifies an environment file (.env) that contains environment variables.
`docker-digging`: This is the name of the docker image to be used for running the container.
So, this command represents, "Run a Docker container from the 'docker-digging' image, map port 4075, and use environment variables from the .env file"


 _Resources_
- [Start Docker](https://docs.docker.com/get-started/)
- [Docker CheatSheet](https://docs.docker.com/get-started/docker_cheatsheet.pdf)
- [Awesome Docker](https://github.com/veggiemonk/awesome-docker)


Thank you.
Remember to keep learning and exploring new things.