Each weekend I want to try out small projects using different tech stacks/technologies. This week let’s deploy a Linux box on DigitalOcean and set it up so we can run multiple Docker containers for future weekend projects.
1. Setup Docker and Nginx
Install docker:
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-18-04
Check if docker is running
|
|
press q
to exit
Install nginx
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04
if you run
|
|
and it shows Status: inactive
- that means the firewall is inactive. Enable the firewall by running
|
|
now run sudo ufw status
again.
Check if nginx is running using systemctl status nginx
press q
to exit
2. Docker container for Nodejs projects
Create a small Nodejs project
Create a Hello World
project from the nodejs
folder:
|
|
Create a package.json
(nano package.json
) with these content:
|
|
Install npm apt install npm
then run npm install
Create a file named appjs
(nano app.js
):
|
|
Allow traffic to port 8080:
sudo ufw allow 8080
Create a Docker image/container for the Nodejs project
Create Dockerfile nano Dockerfile
- add in the content
|
|
Create a .dockerignore
file (nano .dockerignore
) to specify which files and directories in the project directory should not be copied over to the container.
|
|
Build the application image using the docker build command
docker build -t cuongt/nodejs-20-alpine .
Run docker images
to view the available images
We now can create a container with this image - run:
|
|
Run docker ps -a
to view all the containers
docker stop/start container_name
to stop/start
dockr rm container_name
to remove a container
We are now done with setting a container for our Nodejs project, let’s create another container for Python.
3. Docker container for Python projects
Create a sample Python project
Create a folder for Python from root
folder:
mkdir python
cd python
Create an app.py
(nano app.py
) with these content:
|
|
Create a uwsgi.ini
file. This file will contain the uWSGI configurations for our application. uWSGI is a deployment option for Nginx that is both a protocol and an application server; the application server can serve uWSGI, FastCGI, and HTTP protocols.
nano uwsgi.ini
|
|
Create a requirements file so we can run later:
nano requirements.txt
- add Flask to the requirements:
Flask>=2.0.2
Setting Up Docker
Create a Dockerfile nano Dockerfile
:
|
|
Build the Python docker image and container
With these 2 commands:
|
|
We now finished setting up the Python project and its Docker container. Let create server blocks for both Nodejs and Python containers.
4. Create server blocks
Create a new file called nodejs.cuongt.com.conf
in the /etc/nginx/sites-available/
directory and add the following content:
|
|
Create a new file called python.cuongt.com.conf
in the /etc/nginx/sites-available/
directory and add the following content:
|
|
Once you have the two config files ready cd to the /etc/nginx/sites-enabled
directory, and run the following commands:
|
|
Restart Nginx for it to take effects of new changes:
systemctl restart nginx
The 2 domain names nodejs.cuongt.com
and python.cuongt.com
are pointed to the droplet using A records with the droplet IP.
5. Use Docker Compose to manage our containers
Create a docker-compose.yaml
file:
|
|
Enable Docker to start on reboot:
|
|
6. Setup SSL certificates
Add SSL Certificates with this link: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-22-04