As either a computer science student or a beginner software developer, you may have encountered the issue “It works on my computer”. This can be alarming after spending a long time writing lots of code and refining whatever project you have been working on. No one wants to share a project after all of that work just to find out that it works differently or doesn’t work at all on the machines of their peers. This a very common and frustrating experience among computer science students and developers alike.
During my internship, I was taught about a tool that helped me approach this issue: Docker. In the beginning, Docker seemed to me like one of those advanced and obscure technologies only used by the best developers. Still, I soon realized that it is a game-changer for all developers who must manage multiple projects with different project partners and environments.
In the article, I will share the basics of Docker, why it is essential for software development, and how you can easily get started with it as a student or a beginner. Whether you are using it for a class project or preparing to join a team in the software development industry, Docker will be a skill that helps you avoid headaches and save time, making you a much more efficient developer.
Why Docker is useful for Computer Science Students:
As a computer science student, you are always learning to use new languages and tools. This can make it difficult to manage the different environments needed for your different projects to run. Docker becomes very useful in these types of situations.
-
Consistent Development Environments
One of Docker's biggest advantages is its ability to create consistent environments that run across different machines. Whether you are working on a web application, a machine learning project, or a database-driven application, Docker will package all that you need (code, dependencies, libraries) into a portable container.
This means that the environment stays the same whether you are developing on your laptop, a teammate's machine, or a server in the cloud. Now you won’t have any more “it works on my machine” issues!
-
Project Isolation
If you are working on multiple projects, whether they are class-related or personal, Docker containers can ensure that the technologies for each project don’t interfere with each other. Each container runs its own isolated environment.
For example, if I am working on a project that requires Ruby 2.6.10 and another project that needs at least Ruby 3.0.0, I could run both projects simultaneously without any conflicts.
-
Efficient Management of Resources
Compared to a virtual machine (VM), Docker containers are lightweight. The containers will share the kernel of their host system, which means that you can run multiple containers on a single computer without the need for the amount of resources required by a VM. This is perfect for students and developers who don’t have the most high-end hardware but still want to run more than one application.
-
Quick and Simple Setup
Imagine you are setting up a project with your classmates. Instead of spending too much time making sure the environment is correctly set up on each person’s machine, you really just have to share the Dockerfile and you will all be running the project in the same environment! This can help a lot as a student or a developer with approaching deadlines.
Now that you know why Docker is a beneficial skill to learn, lets walk through an example.
Running a Simple Web Application with Docker
Docker works with any language or technology, making it able to containerize many different types of applications. Here is a basic example of how you can run an application inside a Docker container, regardless of the programming language you use.
-
Installing Docker
To install Docker go to the link below and follow the steps in the documentation:
https://docs.docker.com/engine/install/
-
Prepare the application
No matter what language you are using in your application, make sure the project has an entry point like “main.py” in Python or “app.js” in Node.js.
Here is an example for a Python app:
# main.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, Docker World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Or for Node.js:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, Docker World!');
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
The entry point concept is similar regardless of the language you choose to code in: create an app that can be accessed via the web browser.
-
Creating a Dockerfile
Now create a Dockerfile, which is the file that tells docker how to build and run your app. This file is important for making sure your app runs the same way on any machine. Make sure the file is called “Dockerfile”.
Here is the Python app example:
# Use the official Python image
FROM python:3.9
# Set the working directory inside the container
WORKDIR /app
# Copy the current directory contents into the container
COPY . /app
# Install any dependencies
RUN pip install flask
# Make port 5000 available outside the container
EXPOSE 5000
# Define the command to run the app
CMD ["python", "main.py"]
And here is the Node.js app example:
# Use the official Node.js image
FROM node:14
# Set the working directory inside the container
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose port 3000
EXPOSE 3000
# Run the application
CMD ["node", "app.js"]
-
Building and Running the Container
Building and running the container is similar no matter the language of your app. Here is how to do it:
-
Build the Docker Image:
docker build -t my-app
-
Run the Docker container
Python app:docker run -p 5000:5000 my-app
Node.js app:Docker run -p 3000:3000 my-app
-
Build the Docker Image:
Now open the browser and go to http://localhost:5000 (for the Python app) or http://localhost:3000 (for the Node.js app). You should now see the “Hello, Docker World!” message on the page.
By getting familiar with Docker, it will not only make development in groups much easier, but you will gain a skill that is widely used in the Software Development industry. There will be so much ease in your workflow if you don’t have to worry about issues regarding specific environments among your group members. So why not give it a try in your next project?