DEV Community

Ajeet Singh Raina
Ajeet Singh Raina

Posted on

Develop locally using Docker Desktop and Deploy to OpenShift

OpenShift is a powerful container application platform that provides developers with a wide range of tools and services for building, deploying, and managing applications. Docker Desktop, on the other hand, is a popular tool for developing and testing containerized applications locally. In this blog post, we will explore how you can use Docker Desktop to develop your application and then deploy it to OpenShift.

Prerequisites

Before we begin, make sure you have the following installed on your machine:

  • Docker Desktop
  • OpenShift CLI (oc)

You will also need access to an OpenShift cluster. If you do not have access to an OpenShift cluster, you can sign up for a free account on the OpenShift Developer Sandbox.

Developing your application in Docker Desktop

To develop your application in Docker Desktop, you will need to create a Dockerfile and a Docker Compose file.If you're looking out for a quick way of creating Docker assets, do check out docker init.

Docker init is a CLI command that was introduced in Docker Desktop 4.18 to simplify the process of initializing a new project to run in a Docker container. When you run the docker init command in your project directory, it will guide you through the creation of the necessary files for your project with sensible defaults. These files include:

  • .dockerignore
  • Dockerfile
  • docker-compose.yaml

The docker init command also allows you to choose the application platform that your project uses and the relative directory of your main package. Additionally, it can generate Docker assets for your project using the Docker Scout CLI and provides an adminless Mac install flow that does not require admin privileges.

docker init

Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml

Let's get started!

? What application platform does your project use? Node
? What version of Node do you want to use? 19.7.0
? Which package manager do you want to use?  [Use arrows to move, type to filter]
> npm - (detected)
  yarn
  pnpm
Enter fullscreen mode Exit fullscreen mode

The docker init command also allows you to choose the application platform that your project uses and the relative directory of your main package.

Choose Node from the list. Choose the default 19.7.0 Node version.

? What application platform does your project use? Node
? What version of Node do you want to use? 19.7.0
? What command do you want to use to start the app? npm start
? What port does your server listen on? 3000
Enter fullscreen mode Exit fullscreen mode

The generated Dockerfile look something like this:

# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/

ARG NODE_VERSION=19.7.0

FROM node:${NODE_VERSION}-alpine

# Use production node environment by default.
ENV NODE_ENV production


WORKDIR /usr/src/app

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.npm to speed up subsequent builds.
# Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into
# into this layer.
RUN --mount=type=bind,source=package.json,target=package.json \
    --mount=type=bind,source=package-lock.json,target=package-lock.json \
    --mount=type=cache,target=/root/.npm \
    npm ci --omit=dev

# Run the application as a non-root user.
USER node

# Copy the rest of the source files into the image.
COPY . .

# Expose the port that the application listens on.
EXPOSE 3000

# Run the application.
CMD npm start
Enter fullscreen mode Exit fullscreen mode

Here's the Docker Compose file that the tool creates:

services:
  server:
    build:
      context: .
    environment:
      NODE_ENV: production
    ports:
      - 3000:3000

Enter fullscreen mode Exit fullscreen mode

Dockerfile

Based on your preference, you can modify Dockerfile. Here is an example Dockerfile that you can use as a starting point:

FROM node:19.7.0

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD [ "npm", "start" ]
Enter fullscreen mode Exit fullscreen mode

This Dockerfile uses the official Node.js 19 image as a base and installs the dependencies for your application. It also exposes port 3000 and sets the npm start command as the default command to run when the container starts.

Docker Compose file

Here is an example Docker Compose file that you can use to run your application locally:

version: "3"

services:
  app:
    build: .
    ports:
      - "3000:3000"
    volumes:
      - .:/app
Enter fullscreen mode Exit fullscreen mode

This Docker Compose file defines a single service called app that builds your Docker image and maps port 3000 to your host machine. It also mounts your local directory to the /app directory inside the container, allowing you to make changes to your code and see the changes reflected immediately.

To start your application, run the following command in your terminal:

docker-compose up
Enter fullscreen mode Exit fullscreen mode

This will build your Docker image and start your application. You can then access your application by navigating to http://localhost:3000 in your web browser.

Deploying your application to OpenShift

Once you are satisfied with your application, you can deploy it to OpenShift using the following steps:

Pushing your Docker image to a registry

First, you will need to push your Docker image to a container registry that is accessible from your OpenShift cluster. You can use a public registry like Docker Hub, or you can set up a private registry within your OpenShift cluster.

To push your Docker image to a registry, run the following command:

docker push <registry>/<image-name>:<tag>
Enter fullscreen mode Exit fullscreen mode

Replace with the URL of your container registry, with the name of your Docker image, and with the version tag of your Docker image.

Creating a new project in OpenShift

Next, you will need to create a new project in your OpenShift cluster. You can use the oc new-project command to create a new project. For example:

oc new-project my-project
Enter fullscreen mode Exit fullscreen mode

Replace my-project with the name of your project.

Creating a new deployment in OpenShift

To create a new deployment in your OpenShift project, you can use the oc new-app command. For example:

oc new-app <registry>/<image-name>:<tag
Enter fullscreen mode Exit fullscreen mode

Replace /: with the same values that you used when pushing your Docker image to the container registry.

This command will create a new deployment in your OpenShift project based on your Docker image. It will also create a new pod with a running container based on your image.

Exposing your application to the internet

To expose your application to the internet, you can use the oc expose command. For example:

oc expose service/<deployment-name>
Enter fullscreen mode Exit fullscreen mode

Replace with the name of your deployment.

This command will create a new route in your OpenShift project that maps to your service. You can then access your application by navigating to the URL provided by the oc get routes command.

Conclusion

In this blog post, we have explored how you can use Docker Desktop to develop your application and then deploy it to OpenShift. By following these steps, you can speed up your development process and ensure that your application works correctly in both local and production environments.

Here is a recap of the key steps:

  • Develop your application in Docker Desktop using a Dockerfile and a Docker Compose file.
  • Push your Docker image to a container registry that is accessible from your OpenShift cluster.
  • Create a new project in OpenShift using the oc new-project command.
  • Create a new deployment in OpenShift using the oc new-app command.
  • Expose your application to the internet using the oc expose command.

Top comments (0)