DEV Community

Cover image for Deploying Next.js apps to a VPS using Github actions and Docker

Deploying Next.js apps to a VPS using Github actions and Docker

Lewis kori on December 17, 2021

Recently, I had to deploy a project to a DigitalOcean droplet. One of the features I really wanted for this particular project was a Continuous Del...
Collapse
 
aeshevch profile image
Александр Шевченко

Hi Lewis, thanks for this tutorial!
I didn't understand one point - how to find out the value of containername?

Collapse
 
lewiskori profile image
Lewis kori • Edited

Hi Александр,

I'm super happy you found the article useful.

You as the developer assign the containers a name of your choosing.
This is done by running

docker run --name {container_name} -dit -p 3000:3000 {image_name}
Enter fullscreen mode Exit fullscreen mode

where container_name and image_name are variables.

More about the docker run command here :)

Should you wish to see a list of all running containers you can then run

docker ps
Enter fullscreen mode Exit fullscreen mode

Sometimes the container may have exited early and stopped. Probably due to a bad build. You can check these stopped containers by running

docker ps -a
Enter fullscreen mode Exit fullscreen mode

There is a Names column where you can see all the containers with their names.

Alternatively, if you use vscode, the docker extension is particularly helpful for debugging, listing containers by name and running quick docker commands.

Collapse
 
aeshevch profile image
Александр Шевченко

Thank you for the detailed answer!

So it turns out that the name of the container is set in the last line of the yml file, right?

I do so (my docker.yml), but I get errors at the deployment stage:

======CMD======
docker login ghcr.io -u $GITHUB_USERNAME -p $GITHUB_TOKEN
docker ps
docker ps -a
docker pull ghcr.io/***/sakhpasflot/sakhpasflot:latest
docker stop dockerContainer
docker system prune -f
docker run --name dockerContainer -dit -p 3000:3000 ghcr.io/***/sakhpasflot/sakhpasflot:latest

======END======
err: WARNING! Using -*** the CLI is insecure. Use --password-stdin.
out: Login Succeeded
err: WARNING! Your password will be stored unencrypted in /***/.docker/config.json.
err: Configure a credential helper to remove this warning. See
err: https://docs.docker.com/engine/reference/commandline/login/#credentials-store
err: invalid reference format: repository name must be lowercase
err: Error response from daemon: No such container: dockerContainer
out: Total reclaimed space: 0B
err: docker: invalid reference format: repository name must be lowercase.
err: See 'docker run --help'.
2021/12/19 10:18:03 Process exited with status 125
Enter fullscreen mode Exit fullscreen mode

I apologize if I ask stupid questions :)
This is my first experience in setting up ci/cd

Thread Thread
 
aeshevch profile image
Александр Шевченко

Looks like i fixed it

Buuut.. I don't see any changes on the server
The project should have been uploaded to the folder specified in WORKDIR in Dockerfile, right?

Thread Thread
 
lewiskori profile image
Lewis kori • Edited

Awesome!
No the workdir is for the docker container, not your server.

Check your server_ip:3000 in the browser
Or ssh into the server and run the docker ps commands

The project is uploaded to github container registry, your server only downloads the image from there and builds a container from it.
The project code doesn't even need to be in the server

Thread Thread
 
aeshevch profile image
Александр Шевченко • Edited

Apparently I don't understand some important point. Now, the frontend of my project on the server is running from the directory /var/www/www-root/data/www/732273-cc00798.tmweb.ru/frontend

I thought I should specify this directory somewhere

Usually, I start project manually via ssh:

cd /var/www/www-root/data/www/732273-cc00798.tmweb.ru/frontend
git pull origin master
npm run build
npm start
Enter fullscreen mode Exit fullscreen mode

Then i can open 732273-cc00798.tmweb.ru:3000/ and see working website

And now I want to automate this process.

I thought that if I do everything according to your instructions, then after committing to the master branch, the server will update and restart on its own.

Maybe I misunderstood? 😥

Collapse
 
derick1530 profile image
Derick Zihalirwa

Hey

Collapse
 
lewiskori profile image
Lewis kori

HI

Collapse
 
blogvile profile image
blogvile

This is a great article thanks for sharing this informative information. Movierulz

Collapse
 
lewiskori profile image
Lewis kori

Most welcome.
Happy you found it useful!