So why and what is Docker? Well, Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.
Today we will be Dockerizing your NodeJS Application, ie, everything in your front-end and back-end, excluding your Database. We will be doing so by creating a Dockerfile, .dockerignore, installing Docker Desktop and be using DockerHub.
Quick Note: My process will be via Windows OS. For windows, Docker requires WSL 2 but I suggest a workaround due to potential mishaps in your preexisting environments when updating. Other OS will be similar steps in AFTER the installation phase but also slight change in syntax for your shell commands. If you can’t install Docker Desktop then look into installing Docker Engine for the as the substitute for Docker Desktop, ie, https://docs.docker.com/engine/install/ubuntu/
First, let’s install Docker Desktop for your OS. Just do a quick google search for it OR,
https://docs.docker.com/docker-for-windows/install/
After installing, complete the tutorial they give you. That should give you some bare understanding of how to use Docker and which terminal to use. For windows, we are going to use Windows Power Shell.
Now, lets move onto the coding! Go to the root directory of your project. Create both a Dockerfile and a .dockerignore there. Do not end the Dockerfile with a .yml, it is literally Dockerfile as the name.
The Dockerfile will create an Docker Image of your NodeJS application. As you can see in the image above, most of the code is just configurations and running commands. Just adjust the PORT and/or CMD correlating to your app.
Next, in your .dockerignore, place whatever you like to ignore such as node_modules, etc. Note: that in my example, I am not ignoring my bundle.js and that’s why I do not have commands in my Dockerfile to build my bundle.js
Now, go to https://hub.docker.com/ to make a docker hub account.
After your account creation you are now ready to build your image, push it to docker hub for usage anywhere and create your container with the working application!
Open Windows Power Shell or which ever terminal your Docker Desktop or Docker engine asks and navigate to the directory of your Dockerfile. Run the following commands:
> docker build -t dockerhub_username/app_name .To show the list of your images run:
> docker imagesNow to start and run your container:
> docker run -p 8080:8080 -d dockerhub_username/app_name
Now if you go to http://localhost:8080/, you will see your app working locally.
Some tips and tricks. So originally, my server is listening to port 8080. However, when you run docker, it does not matter what your original pre-dockered code. Such that if you instead ran:
> docker run -p 4000:8080 -d dockerhub_username/app_name
Now http://localhost:4000/ would be how you would access your site but localhost:8000 would not work anymore.Also, running:
> docker run -p 80:8080 -d dockerhub_username/app_name
Would just need http://localhost/ to rendertldr; the port you specify before the colon is the port your server is connecting to on YOUR PC and the port after is connecting to the VM inside the container.
Now lastly, you want to be able to use your docker image you create to build your container anywhere and also by anyone. So just like on github you do
> docker push dockerhub_username/app_name
Literally its just that. Your app will be pushed to your repository on docker hub. Now your app can be used by your teammates even if they have different OS and be easily deployed in an instant!
Some other useful commands and tricks:
View all your containers:
> docker psTo stop docker container:
> docker stop <container name>
> docker start <container name>SSH into a docker container to edit or add code, environments, etc:
> docker exec -it <container name> /bin/bashTo edit code inside the container, you will need to install vim or your choice of an editor
> apt-get update
> apt-get install vimFor more command lines visit:
https://docs.docker.com/engine/reference/commandline/build/