Push Docker Image to DigitalOcean Registry

In this article we are going to learn Install Docker on DigitalOcean Ubuntu Droplet, How to Create Docker Image for Node JS Application, Push Docker Image to DigitalOcean Registry | How to Deploy Docker Image to DigitalOcean.

#1:Install Docker on DigitalOcean Ubuntu Droplet

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 -

Add the Docker repository to APT sources:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

First, update your system packages:

sudo apt-get update

Install Docker

sudo apt-get install docker-ce

Check status of your docker running or not:

sudo systemctl status docker
Push Docker Image to DigitalOcean Registry 1
status

If you want to avoid typing sudo whenever you run the docker command, add your username :

sudo usermod -aG docker $USER

If you have any error or issue then run this below command:

sudo chmod 666 /var/run/docker.sock

#2:How to Create Docker Image for Node JS Application

Lets create the directory named nodejsdocker to add node js files to test.

sudo mkdir nodejsdocker

Navigate to nodejsdocker directory

cd nodejsdocker

Create the package.json file where you will specify all dependencies of your Node JS application

sudo nano package.json

paste the below lines into it

{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "First Last <[email protected]>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

Create Next create the server.js page to test Node JS application with express framework

sudo nano server.js

Paste the below lines in it

'use strict';

const express = require('express');

// Constants
const PORT = 3000;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);


cat .dockerignore

node_modules
npm-debug.log

Before creating Docker Image for Node JS application install the docker using below link if not installed.

How to Install Docker on Ubuntu 19.10/18.04/16.04 LTS

How to Install Docker on Windows 10

For other OS follow Docker Official Guide

Next create the Dockerfile with below command in Project root directory

sudo nano Dockerfile

Paste the below Dockerfile instructions in it

FROM node:12

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 3000
CMD [ "node", "server.js" ]

Now build the Docker Image using below command

docker build -t nodejsdocker .

you will get output like this

Push Docker Image to DigitalOcean Registry 2
build image

once build is successful , you can see list of docker images using below command

docker images
Push Docker Image to DigitalOcean Registry 3
docker image

You can see in this nodejsdocker:latest is you image that we pushed 33 seconds ago

#3:Push Docker Image to DigitalOcean Registry

Login to DigitalOcean account, If you don’t have DigitalOcean Account,

Create Account in Digital Ocean and Get $100 Free For 60 Days | $100 FREE DigitalOcean Credit

Once logged in DigitalOcean Control Panel, Goto -> Container Registry.

Push Docker Image to DigitalOcean Registry 4

Firstly go to container registry and enter unique name and choose a plan

Push Docker Image to DigitalOcean Registry 5
creating registry

Now click create registry then click on getting start

Push Docker Image to DigitalOcean Registry 6
getting start

Then if you go to connecting to the registry you will get the page like this and here click on create a new API token

Push Docker Image to DigitalOcean Registry 7
create token

Then create new token and save in notepad because it won’t be show again if you go back or exit

Push Docker Image to DigitalOcean Registry 8
API

Run the below command to Login to DigitalOcean Registry using API token

docker login registry.digitalocean.com

Then it will be asked for a username and password . Paste the token as the username and the password to authenticate.

Push Docker Image to DigitalOcean Registry 9
login

Now tag the Docker Image and Push Docker Image to DigitalOcean Registry

Syntax:

docker tag your-image-name registry.digitalocean.com/your-registry-name/your-image-name

Example:

docker tag nodejsdocker:latest registry.digitalocean.com/nodejsapp/nodejsdocker:latest

Now Push Docker Image to DigitalOcean Registry

docker push registry.digitalocean.com/your-registry-name/your-image-name
Push Docker Image to DigitalOcean Registry 10
image pushed

Now lets check in our container registry image successfully pushed or not, Docker Image is pushed to DigitalOcean Container Registry

Push Docker Image to DigitalOcean Registry 11
nodejsdocker

Conclusion:

We have covered Install Docker on DigitalOcean Ubuntu Droplet, How to Create Docker Image for Node JS Application, Push Docker Image to DigitalOcean Registry.

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap