1. Installing Docker
Before we can begin, Docker must be installed on your machine. If it isn't, follow these steps:Step 1: Update your existing list of packages: sudo apt update
Step 2: Install prerequisite packages that let apt use packages over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Step 3: Add the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Step 4: Add the Docker repository to APT sources:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Step 5: Update the package database with Docker packages from the newly added repo:
sudo apt update
Step 6: Confirm you're about to install from the Docker repo instead of the default Ubuntu repo:
apt-cache policy docker-ce
Step 7: Finally, install Docker:
sudo apt install docker-ce
2. Building and Running Your Node.js Project
Once Docker is installed, navigate to your project's root directory (where the Dockerfile is located), and run the following commands:Step 1: Build your Docker image:
docker build -t my-nodejs-app .
The -t flag is used to name and optionally tag the Docker image in the 'name:tag' format. The . at the end specifies that the build context is the current directory.
Step 2: Run your Docker image:
docker run -p 8080:8080 -d my-nodejs-app
The -p flag maps the port from the Docker container to your host. The -d flag runs the container in the background (detached mode).
Please note, you should replace "8080" with the port your app runs on if it differs. Also, replace my-nodejs-app with your preferred Docker image name.
3. Solving Common Docker Issues
You may encounter a "Permission Denied" error while attempting to connect to the Docker daemon socket. This is because the Docker daemon runs as the root user, and you might be trying to connect to it as a non-root user. There are two ways to solve this issue:Run Docker as root using the sudo command before the Docker command. However, this method isn't recommended as it gives Docker unrestricted access to your system.
The better method is to add your user to the docker group.
First, create the Docker group if it doesn't exist:
sudo groupadd docker
Then, add your user to the Docker group:
sudo usermod -aG docker ${USER}
After this, you might need to log out and log back in, or reboot your machine for these changes to take effect. This method allows your user to run Docker commands without sudo.
sudo groupadd docker
Then, add your user to the Docker group:
sudo usermod -aG docker ${USER}
After this, you might need to log out and log back in, or reboot your machine for these changes to take effect. This method allows your user to run Docker commands without sudo.
4. Viewing Console Log Messages
You can view the logs of your Docker container with the docker logs command.Step1: To get the ID or the name of your Docker container, list all running Docker containers:
docker ps
Step2: With the container ID or name at hand, view its logs:
docker logs <container-id>
or
docker logs <container-name>
Replace <container-id> or <container-name> with the actual ID or name of your Docker container.
Step3: To follow the log output in real time, use the -f or --follow option, similar to tail -f:
docker logs -f <container-id>
Step2: With the container ID or name at hand, view its logs:
docker logs <container-id>
or
docker logs <container-name>
Replace <container-id> or <container-name> with the actual ID or name of your Docker container.
Step3: To follow the log output in real time, use the -f or --follow option, similar to tail -f:
docker logs -f <container-id>
5. Stopping a Running Docker Container
To manage your running containers better, you might need to stop them at times. Here is how you can do it:Step 1: Get the ID or the name of your running Docker container:
docker ps
Step 2: Once you have the container ID or name, stop that container:
docker stop <container-id>
or
docker stop <container-name>
Replace <container-id> or <container-name> with the actual ID or name of your Docker container.
Replace <container-id> or <container-name> with the actual ID or name of your Docker container.
6. Conclusion
By following these steps, you should now be able to run your Node.js application in a Docker container on your local Ubuntu machine, view its logs, and manage its lifecycle with ease.
