Skip to main content

Command Palette

Search for a command to run...

Docker: A game changer for code deployment

Updated
7 min read
Docker: A game changer for code deployment
J

Hi🙋🏽‍♀️, I'm Jadesola, a software developer based in Nigeria 🛠️. Driven by a passion for solving problems with code, I'm currently refining my skills as a front-end developer while delving into the world of back-end development. I am dedicated to sharing my knowledge and experience as I grow in the tech world. Join me on my journey and let's grow together!

Have you ever experienced the problem of being unable to run an application that runs perfectly on your system on another system? Or perhaps you have tried to set up someone else’s code on your system but faced a multitude of challenges? This issue of environmental inconsistencies is popularly known as the “It works on my computer” syndrome, which occurs more frequently when code is moved to the production environment.

In case you are wondering, “What is the production environment?” let’s dive into that first:

Production environment refers to the live environment where your application is made available to users. Typically, applications are built locally on the developer’s machine and moved to the live environment. This represents the final stage of the software development life cycle, where all the code, features, and configurations are put in place to serve the users.

The production environment is crucial because it simulates the behavior of an application under real-life circumstances like heavy network traffic, etc.

It also allows you to monitor the application performance, uptime, and errors. This allows you to identify and resolve any errors encountered during the live usage of the application, ensuring minimal disruption to the user experience

Additionally, the production environment is where applications can be scaled to accommodate a larger user base and handle more network traffic. Security audits and monitoring tools are often implemented during the production stage to ensure the application is secure and compliant with necessary regulations.

The need for docker

Now, back to the issue I mentioned at the beginning of this article: environmental inconsistencies, which is just one of the many challenges faced in traditional development environments. Beyond this, developers often grapple with other significant problems, such as dependency conflicts, and difficulty scaling applications amidst many other shortcomings.

Fortunately, there’s a powerful solution that addresses these challenges: Docker.

What is Docker?

Docker is an open-source platform that automates the deployment of applications in lightweight, portable containers. In other words, Docker uses containers to run applications in an isolated environment.

Containers encapsulate everything needed to run an application, including the code, runtime, libraries, and system tools.

Docker Essentials

  • Dockerfile: A script that contains a series of instructions to create a Docker image. It specifies the base image, application code, dependencies, and configuration.

  • Containers: A runnable instance of an image. Containers are isolated environments for running applications.

  • Images: A read-only template that contains instructions for creating containers. Images can be built from a Dockerfile.

  • Parent image: A parent image is the original Docker image that is used as a template for other images. In the example below, node:16-alpine is the parent image, which is a lightweight Node.js environment.

      FROM node:18-alpine
    
  • Base image: A base image is an image with no parent and is typically built from scratch. In the example below, scratch is the base image, and you would need to manually install everything you need (libraries, binaries, etc.) for your application to run.

      FROM scratch
    

Dockerfile Commands

  1. FROM: Specifies the base image to be used for the Docker image.

  2. WORKDIR: Defines the directory where the application code resides inside the container

  3. COPY: Copies all the files from the current directory on your local machine into the docker container

  4. RUN: Executes commands like installing dependencies or configuring the environment inside the container while building the image.

  5. CMD: Specifies the default command to run when a container starts.

  6. EXPOSE: Informs Docker of the specific port that the container will listen to at runtime. However, it does not make the port accessible outside the container. You’d need to use the -p option when running the container to bind the container’s port to the host.

  7. ENV: Sets environment variables in the container.

  8. ENTRYPOINT: Defines the main process that should run in the container.

  9. ADD: Copies files from the host machine to the container like the COPY command, but it can also handle URLs and tar file extractions.

  10. VOLUME: Ensures that data inside the container is persisted even when the container is stopped or removed.

  11. USER: Sets the user for running commands inside the container. By default, Docker containers run as root.

  12. HEALTHCHECK: Checks the health of the container by monitoring the status of the application running in the container and ensuring it’s working properly.

Creating a Simple Docker Application

Before we get started, we need to ensure we have the following essentials installed on our machine:

Step 1: Set Up a React App

If you already have a React app, you can skip this step. If not, let’s create one using create-react-app. The command below creates a new React application in your current directory.

npx create-react-app .

Step 2: Build the application locally

Once the application is created, we need to build it locally before we dockerize it.

npm run build

Step 3: Create a .dockerignore file

Create a .dockerignore file in the root directory of your project to prevent unnecessary files from being copied into the Docker image.

node_modules
build
.dockerignore
Dockerfile
README.md
.git

Step 4: Create a Dockerfile

In the root of your React project, create a file named Dockerfile. This file will contain the instructions Docker uses to build and run your app.

# Specify the base image
FROM node:18-alpine

# Set the working directory
WORKDIR /app

# Copy the package.json and package-lock.json files
COPY package*.json ./

# Install the dependencies
RUN npm install

# Copy the app files
COPY . .

# Build the app
RUN npm run build

# Expose the port
EXPOSE 3000

# Run the app
CMD ["npm", "start"]

Explanation of the Dockerfile:

  1. FROM node:16-alpine uses a lightweight Node.js image to build the app.

  2. WORKDIR /app sets the working directory inside the container to /app.

  3. COPY package*.json ./ copies package.json and package-lock.json to the container to install dependencies.

  4. RUN npm install installs the app dependencies.

  5. COPY . . copies the rest of the application code to the container.

  6. RUN npm run build builds the production-ready React app.

  7. EXPOSE 3000 exposes port 3000 so the app can be accessed.

  8. CMD ["npm", "start"] sets the command to run the app with npm start.

Step 5: Build the Docker Image

This command builds the Docker image using the instructions in the Dockerfile. The -t flag tags the image as my-app-image.

docker build -t my-app-image .

Step 6: Run the Docker Container

Once the image is built, you can run the Docker container using the command below. This command starts the container and maps port 3000 inside the container to port 3000 on your local machine. You can now open a browser and visit http://localhost:3000 to see your Dockerized React app running.

docker run -p 3000:3000 my-app-image

Step 6: Verify Everything is Working

You should now be able to see your React app running in the browser. This simple setup allows you to package your React app in a Docker container, ensuring that it runs the same way on any system.

What type of application can be Dockerized?

Almost any type of application can be Dockerized. Docker is versatile and can containerize a wide range of applications, from simple web apps to complex, multi-service architectures. Here is a list of application types that can be dockerized:

  1. Web applications, including frontend applications and backend applications.

  2. Microservices

  3. Databases and storage systems

  4. Application Programming Interface (API) Servers

  5. Message brokers and queues like RabbitMQ, Kafka, and ActiveMQ

  6. Batch processing Applications

  7. Continuous Integration/Continuous Development (CI/CD) Pipelines

  8. Legacy Applications

  9. Development Environments

  10. Artificial intelligence and machine learning

  11. DevOps Tools and Infrastructure

In general, if an application can run on a Linux-based system (or Windows for Windows containers), it can likely be Dockerized.

Why Docker?

  • Lightweight Architecture: Docker consumes less memory and starts much faster than traditional virtual machines.

  • Portability: Containers can run consistently across different environments (development, testing, production).

  • Isolation: Each container runs in its own environment, preventing conflicts between applications.

  • Scalability: Docker allows you to easily scale applications by running multiple container instances.

  • Efficiency: Containers use fewer resources than traditional virtual machines since they share the host system’s kernel.

Conclusion

Even though this article seems quite extensive, it merely scratches the surface of what Docker has to offer.

For more information, you can visit the Docker documentation.

Docker can seem complex at first, but practicing with real applications will help solidify your understanding. Start small and gradually explore more advanced features!

Thank you for reading! If you found this article helpful and informative, please subscribe and give it a like; it helps support the content and keep you updated with future posts.

More from this blog

Untitled Publication

18 posts