←  back to blog
A look at development inside a Docker container

A look at development inside a Docker container

Developing inside a Docker container involves setting up your development environment within a containerized environment. This means you don’t need to install all the necessary tools and dependencies directly on your local machine. Instead, you create a Docker container that encapsulates everything you need for your development workflow. This includes your code editor, programming languages, frameworks, libraries, and any other tools or utilities you require.

The key difference between a traditional local development environment and developing inside a Docker container is the level of isolation and consistency. With a local environment, you’re relying on the specific configuration of your machine, which can vary from one system to another. This can lead to compatibility issues, especially when working in teams or deploying applications to different environments. On the other hand, a Docker container provides a self-contained and reproducible environment that remains consistent across different systems.

It’s worth noting that developing inside a Docker container is different from using Docker containers solely for deployment or testing purposes. The latter involves creating containers for running your application in production or staging environments, whereas developing inside a container means you’re actually writing and editing code within the containerized environment itself.

Why develop inside a Docker container?

There are several compelling reasons to consider developing inside a Docker container:

  1. Consistent Development Environment: This eliminates the “works on my machine” problem, and makes it easier to collaborate with team members or share your work with others.
  2. Reproducible Builds: Docker containers are based on immutable images, which means that you can reliably reproduce the same environment every time you create a new container. This is particularly useful when working on complex projects with multiple dependencies. Or when you need to switch between different versions of libraries or frameworks.
  3. Simplified Set Up and Onboarding: Setting up a development environment can be a time-consuming and error-prone process, especially for complex projects with many dependencies. By using Docker containers, you can streamline the set-up process and ensure that new team members or contributors can quickly get up and running with the correct environment.
  4. Isolation and Resource Management: Docker containers provide isolation between your development environment and the host system, preventing conflicts or dependencies from interfering with other applications or processes running on your machine. Additionally, you can easily manage and limit the resources (CPU, memory, etc.) allocated to your development container.
  5. Portability and Scalability: Docker containers are designed to be portable and scalable. You can easily move your development environment between different machines or deploy it to a cloud platform, ensuring that your code runs consistently in different environments.

Use cases for developing inside a Docker container

While developing inside a Docker container can benefit various projects, it’s particularly useful in the following scenarios:

  1. Complex Projects with Multiple Dependencies: If your project relies on multiple libraries, frameworks, or tools with specific version requirements, developing inside a Docker container can help ensure that all dependencies are properly isolated and compatible.
  2. Cross-Platform Development: When working on applications that need to run on multiple platforms (e.g., Windows, macOS, Linux), developing inside a Docker container can provide a consistent environment across different operating systems.
  3. Microservices Architecture: In a microservices-based architecture, each service may have its own set of dependencies and requirements. Developing each service inside a separate Docker container can help manage these dependencies more effectively.
  4. Collaborative Development: When working on a team or open-source project, developing inside Docker containers can ensure that everyone is working with the same environment, reducing compatibility issues and making it easier to collaborate.
  5. Reproducible Builds and Deployments: If you need to ensure that your application builds and runs consistently across different environments (e.g., development, staging, production), developing inside a Docker container can help achieve this by providing a reproducible environment.

When not to use a Docker container for development

While developing inside a Docker container offers many benefits, there are certain situations where it may not be the most suitable approach. Here are some scenarios where you might consider alternative development environments:

  1. Simple or Lightweight Projects: For small or lightweight projects with minimal dependencies, like static site generators, command lines, or utility scripts, the overhead of setting up and managing Docker containers may not be justified. In such cases, a traditional local development environment may be more appropriate.
  2. Limited Resources: If you’re working on a resource-constrained machine with limited CPU, memory, or storage, running a Docker container for development may introduce performance issues or resource constraints. In these situations, a lightweight local development environment may be a better choice.
  3. Specific Hardware or System Dependencies: Some projects may require direct access to specific hardware devices or system services that aren’t easily accessible from within a Docker container. In such cases, a local development environment may be more suitable.
  4. Incompatible Tools or Plugins: While Docker containers aim to provide a consistent environment, some development tools or plugins may not work as expected within a containerized environment. If you rely heavily on such tools or plugins, a local development environment may be a better fit.

It’s important to carefully evaluate your project’s requirements, team dynamics, and development workflows to determine if developing inside a Docker container is the right choice. In some cases, a traditional local development environment or a remote development environment may be more appropriate.

How to develop inside a Docker container

While the benefits of developing inside a Docker container are compelling, getting started can seem daunting, especially for those new to containerization. Fortunately, there are tools and best practices that can simplify the process.

For example, Microsoft’s Visual Studio Code (VS Code) has gained immense popularity among developers, and its devcontainers extension makes it easy to set up a container-based development environment. Devcontainers allow you to define your development environment using a configuration file, which can be shared with your team or project contributors.

To get started with devcontainers:

  1. Install the Remote - Containers extension in VS Code.
  2. Create a .devcontainer folder in your project’s root directory.
  3. Add a devcontainer.json file to the .devcontainer folder, which will define your development environment’s configuration.
  4. Open your project in VS Code, and you’ll be prompted to reopen it in a container.

Here’s an example devcontainer.json file for a Node.js project:

language icon json
{
	"name": "Node.js",
	"dockerFile": "Dockerfile",
	"appPort": 3000,
	"extensions": ["dbaeumer.vscode-eslint"]
}

This configuration specifies the name of the development environment, the Dockerfile to use for building the container, the port to expose (if applicable), and any VS Code extensions to install within the container.

Best practices and getting started

Now that you understand the benefits and use cases of developing inside a Docker container, let’s explore some best practices and steps to get you started:

  • Choose a Base Image: Start by selecting a base Docker image that aligns with your project’s requirements. Popular choices include official images for programming languages (e.g., Python, Node.js, OpenJDK) or lightweight base images like Alpine.
  • Create a Dockerfile: Write a Dockerfile that defines your development environment. This file will specify the base image, install dependencies, copy your code, and set up any necessary configurations or commands.
language icon dockerfile
# Use a base image
FROM node:14
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port (if applicable)
EXPOSE 3000
# Start the development server
CMD ["npm", "run", "dev"]
  • Build the Docker Image: Use the docker build command to build your Docker image based on the Dockerfile. The following command creates the container image using the previous Dockerfile to package the application:
language icon bash
docker build -t my-dev-env .
  • Run the Docker Container: Start your development container using the docker run command, mapping any necessary volumes or ports. The following command creates a new container using the my-dev-env image running on port 3000:
language icon bash
docker run -it -v $(pwd):/app -p 3000:3000 my-dev-env
  • Optimize for Performance: While Docker containers provide isolation, there may be performance implications depending on your use case. Consider mounting volumes for improved file system performance and leveraging Docker’s built-in caching mechanisms to optimize build times.
  • Leverage Multi-Stage Builds: Multi-stage builds allow you to create smaller and more efficient Docker images by separating the build and runtime stages. This can improve build times and reduce the overall image size.
  • Develop Inside the Container: With your development container running, you can now open your code editor and start coding inside the container. Any changes you make to the code will be reflected in the mounted volume, ensuring that your work is persisted outside the container.
  • Leverage Docker Compose: For more complex setups involving multiple containers (e.g., a database, cache, or other services), consider using Docker Compose to define and manage your multi-container development environment. For example, the following docker-compose.yml file demonstrates a basic setup with a web application and a MySQL database:
language icon yml
version: '3'

services:
    web:
        build: .
        ports:
            - '5000:5000'
        environment:
            - MYSQL_HOST=db
            - MYSQL_USER=myuser
            - MYSQL_PASSWORD=mypassword
            - MYSQL_DB=mydb
        depends_on:
            - db

    db:
        image: mysql:5.7
        environment:
            - MYSQL_ROOT_PASSWORD=rootpassword
            - MYSQL_DATABASE=mydb
            - MYSQL_USER=myuser
            - MYSQL_PASSWORD=mypassword
        volumes:
            - db-data:/var/lib/mysql

volumes:
    db-data:

To use this Docker Compose file, save it as docker-compose.yml in your project directory. Then, you can run the following commands:

    # Build and start the services
    docker-compose up -d

    # Stop and remove the services
    docker-compose down
  • Version Control and Collaboration: Treat your Dockerfile and any associated configuration files as part of your project’s source code. Version control these files and share them with your team to ensure a consistent development environment across all contributors.

Cloud development environments: an alternative approach

While developing inside a Docker container offers many benefits, there’s an alternative approach that some developers prefer: cloud development environments.

A cloud development environment works similarly to developing with Docker as we described today. However, the workload, container, or development environment is moved to the cloud. For instance, cloud development environments like Gitpod can reuse Docker images as the basis for the development environment, and can run sophisticated multi-container tooling like Docker Compose.

You get similar benefits to developing with Docker for standardization and closing the gap between development and production, but with additional performance benefits of running on high performance hardware and strict security controls provided by the cloud. In an enterprise or even regulated environment context, cloud development environments also bring additional standardization and control which adds to the benefits.

Takeaways

Developing inside a Docker container offers numerous benefits, including a consistent and reproducible development environment, simplified set up and onboarding, isolation, and portability. While it may require some initial set up and a learning curve, the advantages of this approach can significantly improve your development workflow. Especially for complex projects, cross-platform development, or collaborative environments.

By following container best practices—such as creating a well-defined Dockerfile, leveraging Docker Compose for multi-container setups, and optimizing for performance—you can unlock the full potential of developing inside Docker containers. Remember to version control your Docker configuration files and share them with your team to ensure a seamless development experience across all contributors.

Whether you’re working on a personal project or collaborating with a team, consider exploring the world of developing inside Docker containers. It may be just the solution you’ve been looking for to streamline your development process, while also ensuring consistent and reproducible environments across different machines and platforms.

This post was written by David Snatch. David is a cloud architect focused on implementing secure continuous delivery pipelines using Terraform, Kubernetes, and any other awesome tech that helps customers deliver results.

Standardize and automate your development environments today