This section outlines the steps to configure and use Docker within the development environment of helixml/docs.

Prerequisites

Ensure the following prerequisites are met:

  • Docker installed on your development machine.

    Verify installation with:

    docker --version
    
  • Basic knowledge of Docker’s command-line interface and how containers work.

Setting Up Docker for Development

1. Creating a Dockerfile

The Dockerfile is a blueprint for your Docker image. Create a Dockerfile in the root of your project directory.

Example:

# Use the official Node.js image as a base
FROM node:14

# Set the working directory inside the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json for dependency installation
COPY package*.json ./

# Install application dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the application port
EXPOSE 8080

# Start the application
CMD ["npm", "start"]

This example uses Node.js but can be tailored for other setups based on project requirements.

2. Creating a Docker Compose File

For a multi-container setup, define services in a docker-compose.yml file.

Example:

version: '3'

services:
  app:
    build: .
    ports:
      - "8080:8080"
    volumes:
      - .:/usr/src/app
    environment:
      NODE_ENV: development

  database:
    image: mongo
    ports:
      - "27017:27017"

This configuration defines two services: app and database. The app service builds from the current directory and exposes port 8080, while the database uses a MongoDB image.

3. Building Your Docker Image

Run the following command to build the Docker image based on your Dockerfile.

docker build -t myapp:dev .

4. Starting the Development Environment

Use Docker Compose to start your containers.

docker-compose up

This command will read the docker-compose.yml file and start the defined services. You should see logs from both your application and MongoDB.

5. Accessing the Application

After running the containers, you can access your application by navigating to http://localhost:8080 in your web browser.

6. Development Workflow

During development, it is crucial to have hot reloading enabled. This can be achieved by utilizing volumes in Docker, which allow real-time code changes to reflect in the container.

In the provided docker-compose.yml, the following line enables a volume that maps the current directory to the container’s working directory:

volumes:
  - .:/usr/src/app

7. Stopping the Development Environment

To stop the running containers, use:

docker-compose down

This will stop and remove all containers defined in the docker-compose.yml.

8. Removing Unused Images and Containers

Over time, image layers and containers can accumulate, consuming disk space. To clean up unused images and containers, execute:

docker system prune

Optionally, you can add the -a flag to remove all unused images, not just dangling ones.

Conclusion

Through this guide, the steps to effectively configure Docker for a development environment within helixml/docs have been outlined, enabling a seamless development experience.

[Source: helixml/docs]