A Beginner’s Guide to Docker: What It Is and How to Use It for Open Source

What is Docker?

Docker is an open-source platform that allows developers to automate the deployment, scaling, and management of applications in lightweight, portable containers. It enables you to package software (code, libraries, system tools, runtime, etc.) into standardized units called containers, which can run consistently across different computing environments.

The main advantage of Docker is that it helps in avoiding the “it works on my machine” problem 😆 , ensuring the same environment is replicated regardless of where the container runs — be it a developer’s laptop 💻 , cloud ☁️, or on-premises servers 🖧 .

Key Concepts in Docker:

  1. Image: A blueprint or snapshot of a container that includes everything needed to run an application (OS, libraries, application code). Images are immutable.

  2. Container: A running instance of an image. Containers are isolated but share the host machine's OS kernel.

  3. Dockerfile: A text file with instructions on how to build a Docker image. It defines the environment, dependencies, and steps to run your application.

  4. Docker Hub: A cloud-based registry where Docker users can push and pull Docker images. It’s widely used for sharing images across open-source projects.


How Docker Fits into Open Source

Docker is a game-changer for open source development because it allows contributors to replicate an exact environment without worrying about compatibility issues. It enhances collaboration by creating a portable and consistent development environment that can be easily shared.

For a opensource developer, it makes it easy for setting up the local environment without putting unnecessary load on our local machine. It can be compared to a virtual machine but is very different from a VM, as docker only uses a Base image for setting up a project.


Difference in Docker and Virtual Machine.

The image displayed above shows a infrastructure of our local machine.

All our operating systems run on kernal level, we have Host OS, which could be Windows OS, Mac OS or our Linux OS. One can also have a dual booted system or can load multiple OS and distros of OS, which will be loaded on Kernel level. Hence they take up large amount of space.

Virtual Machines:

A Virtual Machine (VM) is like a complete computer running inside your existing computer. It creates a virtual version of hardware, such as the processor, memory, and storage. Each VM has its own operating system (like Windows, Linux, etc.), completely separate from the one on your actual machine (called the host). This makes each VM act like a fully independent computer. However, because VMs need to run an entire OS, they take up a lot of resources like memory and storage, and they can slow down your system.

Since each VM is running its own copy of an OS, it has more overhead, meaning it takes longer to start, uses more RAM, and requires more storage space.


Docker Containers:

On the other hand, Docker is much lighter and works in a different way. Instead of creating a full virtual machine, Docker lets you run applications in containers. These containers don’t need their own full operating system. Instead, they share the same OS with your computer, but they run independently, as if they are isolated from each other.

Docker uses images (essentially templates for applications) that package everything the application needs to run. These images are much smaller than VMs because they don’t include an entire OS. For example, an image might include only a few files from a basic operating system along with the application code itself. Since Docker containers share the same OS kernel, they use fewer resources and start up much faster than VMs.

In short, Docker is great for running applications in a lightweight, isolated environment without the need for an entire operating system. This makes it ideal for modern software development, especially for things like microservices or cloud applications, where you want speed and efficiency.

Let’s look at a few ways Docker empowers open-source projects:

1. Environment Consistency Across Developers

Different developers often have different operating systems, package versions, or settings. Docker ensures that everyone on the project works in the same environment, thereby eliminating the "it works on my machine" problem.

2. Containerized Application Distribution

Open-source projects often require complex environments with multiple services, such as databases, web servers, etc. Docker enables you to define these services in a Docker Compose file, making it easy to spin up the entire stack with one command.

3. Automated Testing and CI/CD

Open-source projects benefit from automated testing. With Docker, you can set up continuous integration pipelines that automatically test the code in a consistent environment every time there’s a change in the repository. Tools like GitHub Actions, Travis CI, or GitLab CI can work seamlessly with Docker.


Setting Up Docker for Open Source Projects

Step 1: Install Docker

For Linux:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

For macOS:

Install Docker Desktop from Docker’s official site.

For Windows:

Install Docker Desktop from Docker’s official site. Ensure Windows Subsystem for Linux (WSL) is enabled for better Linux compatibility.

Step 2: Verify Installation

Once Docker is installed, you can verify if Docker is running by using the following command:

docker --version

You should see the Docker version printed on your terminal.


Basic Docker Commands

Here are some essential Docker commands to get started:

1. docker pull: Download a Docker image from a repository (e.g., Docker Hub).

docker pull ubuntu:latest

2. docker run: Run a Docker container from an image.

docker run -it ubuntu:latest

The -it flag allows you to interact with the container.

3. docker build: Build an image from a Dockerfile.

docker build -t myapp .

The -t flag tags your image with a name, and the . indicates the current directory where the Dockerfile resides.

4. docker ps: List running containers.

docker ps

After running the command ‘docker ps‘, the following output will appear if you have some containers running.

docker

5. docker stop: Stop a running container.

to stop the running containers simply type the following command.
take the container_id from the output of 4th step.

docker stop <container_id>

6. docker rm: Remove a stopped container.

The following command is used to remove a container.
It is suggested to not delete the container locally unless it is cloned and pushed on docker hub.

docker rm <container_id>

Setting Up Git for Open Source Contributions

For contributing to open-source projects, you'll likely be working with Git, a distributed version control system. Let’s quickly cover the basic Git commands required to start working with Docker-based projects.

1. git clone: Clone a repository.

Simply copy the URL of the project you want are interested in.

git clone https://github.com/<user>/<repo>.git

2.Run through the project,

Set up the project locally with its dependancy and try running it locally.

3.fix issues.

Find issues from the issues section of the project on github, and try to trace the issues in your local code and fix the issues.
once you fix the issue.

4. git add: Add changes to staging.

Add the changes you made to the forked repository to implement the changes you made.

git add .

5. git commit: Commit the staged changes.

git commit -m "Add Docker configuration"

6. git push: Push changes to the remote repository.

git push origin my-feature-branch

Conclusion of this blog.

Docker has revolutionized the way software is developed and deployed, especially in the open-source community. By creating a consistent and shareable environment, Docker eliminates compatibility issues and allows developers to work more effectively. Whether you’re contributing to an open-source project or maintaining your own, Docker is a powerful tool that will make your life easier.

If you’re new to Docker, start by familiarizing yourself with the commands listed above, and don’t forget to push your Dockerized applications to platforms like Docker Hub to share them with the open-source world!


Free resources for reader: