Docker: A game changer for code deployment

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-alpineis the parent image, which is a lightweight Node.js environment.FROM node:18-alpineBase image: A base image is an image with no parent and is typically built from scratch. In the example below,
scratchis 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
FROM: Specifies the base image to be used for the Docker image.
WORKDIR: Defines the directory where the application code resides inside the container
COPY: Copies all the files from the current directory on your local machine into the docker container
RUN: Executes commands like installing dependencies or configuring the environment inside the container while building the image.
CMD: Specifies the default command to run when a container starts.
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
-poption when running the container to bind the container’s port to the host.ENV: Sets environment variables in the container.
ENTRYPOINT: Defines the main process that should run in the container.
ADD: Copies files from the host machine to the container like the
COPYcommand, but it can also handle URLs and tar file extractions.VOLUME: Ensures that data inside the container is persisted even when the container is stopped or removed.
USER: Sets the user for running commands inside the container. By default, Docker containers run as root.
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:
Docker: You can download it from the official Docker website.
Node.js: You can download it from the official Node.js website.
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:
FROM node:16-alpineuses a lightweight Node.js image to build the app.WORKDIR /appsets the working directory inside the container to/app.COPY package*.json ./copiespackage.jsonandpackage-lock.jsonto the container to install dependencies.RUN npm installinstalls the app dependencies.COPY . .copies the rest of the application code to the container.RUN npm run buildbuilds the production-ready React app.EXPOSE 3000exposes port 3000 so the app can be accessed.CMD ["npm", "start"]sets the command to run the app withnpm 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:
Web applications, including frontend applications and backend applications.
Microservices
Databases and storage systems
Application Programming Interface (API) Servers
Message brokers and queues like RabbitMQ, Kafka, and ActiveMQ
Batch processing Applications
Continuous Integration/Continuous Development (CI/CD) Pipelines
Legacy Applications
Development Environments
Artificial intelligence and machine learning
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.




