To watch the youtube video explaining this topic, click here.
Introduction
Docker is a tool for containerizing projects so that any required dependencies (libraries) can be installed in isolation within the Docker container and doesn't have to be installed on the host computer.
This is good practice because it:
In this blogpost we'll look at Dockerizing a simple project. Installing Docker
Your computer must have Docker installed to use it, of course. Docker installation varies depending on your operating system, so google for a guide.
This previous tutorial shows how to install Docker on older versions of Ubuntu linux. On newer versions, of Ubuntu Linux, you can install docker using the following command:
sudo apt install docker.io
And test it with:
sudo docker run hello-world
Setting Up A Test Project
The best projects to Dockerize are backend servers, so let's take a very basic Hello World server.
This repository has the bare minimum code required to run a Node.js server.
git clone https://github.com/srcmake/node-bare-minimum-server.git
If you have node/npm installed locally, then you can try running this project locally. cd node-bare-minimum-server npm install node server.js And go to localhost:3000 in your browser to ensure the server gives a response. (If you don't have node/npm installed, then don't worry because we'll be dockerizing the project in the next section to not need to have them installed locally.) Dockerizing The Project
To dockerize the project, we must add a Docker file to our project.
touch Dockerfile
There are a few things we need to specify in our Dockerfile:
Open the Dockerfile and paste the following code inside of it:
Additionally, we're going to specify a Docker Ignore file, which works similarly to a Git Ignore file; it will specify files in our project folder that we don't need copied into the Docker container. (It's good practice to make the Docker container as small as possible.) For example, we don't need to copy our local node_modules folder since we want to rebuild it inside the container, anyway. Create the Docker Ignore file:
touch .dockerignore
Open .dockerignore and add the files/folders we don't need copied over into it:
Now our codebase is Dockerized. How do we run the server locally? First we need to build a Docker image for our project, and then we'll run that image. To build the Docker image:
sudo docker build -t node-min-server .
We can see the image we just built in our list of docker images.
sudo docker images
To run the Docker image we just built:
sudo docker run -p 8000:3000 -d node-min-server
Note that the internal Docker container is using port 3000, but we're specifying our local computer to connect to the Docker container on our computer's port 8000. (This is just for clarity, feel free to use the same ports.) We can now see our server running on port 8000 on our computer, without needing Node or NPM installed. Check at localhost:8000 in your browser. And that's it. The next steps would be to commit the Dockerfile (and docker ignore file) to our project's repository and update our README for the new deployment steps. Conclusion
Dockerization of a project is pretty simple and has many benefits. It's useful when working with multiple people and setting up CI/CD. All it takes to Dockerize a project is creating a Dockerfile and using a few new commands to work locally.
The next blogpost is going to be on using docker compose, which helps create more complex local systems. We'll also be doing a tutorial on CircleCI, and Docker will help with our CI steps.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
References
1. https://nodejs.org/fr/docs/guides/nodejs-docker-webapp/ Comments are closed.
|
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|