search
HomeWeb Front-endCSS TutorialA Gentle Introduction to Using a Docker Container as a Dev Environment

A Gentle Introduction to Using a Docker Container as a Dev Environment

Disclaimer: This article employs a degree of playful exaggeration. The author does not claim to speak for Dylan Thomas, nor advocate imposing light themes on unwilling users.

Dylan Thomas famously urged, "Do not go gentle into that good night." While referring to death, this sentiment might equally apply to the complexities of Linux containers. Though Thomas passed in 1953, the internet empowers confident pronouncements on his behalf (with appropriate caveats).

This confidence stems from a blend of overestimation and recent struggles configuring a Docker container as a development environment. The ensuing frustration mirrored a battle against insurmountable odds.

However, suffering is a potent teacher. This experience informs a (relatively) gentle introduction to using Docker containers for development. First, let's address the "why."

Why Use Docker Containers for Development?

Imagine a project encompassing not only code, but the entire development environment, dependencies, and runtimes. This project, portable and self-contained, could be shared with anyone, anywhere, running flawlessly without environment-specific configurations.

This is the essence of Docker containers. A Dockerfile defines the entire runtime environment in a single file, requiring only a method for development within the container.

VS Code Remote - Containers: A Powerful Combination

VS Code's "Remote - Containers" extension allows loading projects into Docker containers and accessing them via VS Code. This seamless integration simplifies the process.

Adding a Container to a Project

Consider a fresh Windows machine with WSL2 and Docker installed. Attempting to run a Node.js project without additional installations would result in an error. Instead of manual Node.js installation, let's configure the project to run within a pre-configured container.

VS Code simplifies this. The "Add Development Container Configuration Files..." command analyzes the project and suggests appropriate container definitions. Selecting a Node.js version (e.g., Node.js 14) adds a .devcontainer folder containing a Dockerfile (defining the Node.js image) and a devcontainer.json file (for project-level configuration).

Selecting "Rebuild and Reopen in Container" restarts VS Code, builds the container, and opens the project within it. The lower-left corner confirms the VS Code connection to the container. The terminal now operates within the Linux container, enabling npm install and npm start.

Port mapping is necessary to access the application (typically running on port 3000) from the browser. The Remote Containers extension's "Remote Explorer" (with a "Port Forwarding" section) allows specifying the port mapping (3000 in this case).

Customization is achieved via the devcontainer.json file. Automating port forwarding and npm install is possible by adding forwardPorts and postCreateCommand properties:

{
  // ...
  "forwardPorts": [3000],
  "postCreateCommand": "npm install",
  // ...
}

VS Code extensions can also be included, avoiding manual installation within the container. Adding extensions like Prettier and ESLint enhances the development experience.

{
  // ...
  "extensions": [
    "dbaeumer.vscode-eslint",
    "esbenp.prettier-vscode",
    "GitHub.github-vscode-theme"
  ]
  // ...
}

The Dockerfile can be customized to add software not included in the base image. For example, adding cowsay:

FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-14
RUN apt-get update && apt-get -y install cowsay
ENV PATH="/usr/games:${PATH}"

Remember: apt-get update refreshes the package list; -y avoids confirmation prompts; && chains commands; \ allows line breaks within commands; and grouping related commands minimizes layers.

After rebuilding, potential issues (e.g., cowsay path) might require adjustments to the PATH environment variable within the Dockerfile.

Advanced Configuration: Customizing the Terminal

To customize the terminal environment, modify the devcontainer.json file. Setting the default shell to /bin/zsh and adjusting VS Code settings is straightforward:

{
  // ...
  "settings": {
    "terminal.integrated.shell.linux": "/bin/zsh",
    "workbench.sideBar.location": "right"
  },
  // ...
}

Copying a customized .zshrc file (adjusting paths as needed) into the container completes the terminal setup. Downloading plugins via git clone within the Dockerfile further refines the environment.

In summary, project configuration resides in devcontainer.json, while environment configuration occurs within the Dockerfile. Consult the official Remote Containers documentation for more advanced techniques.

The above is the detailed content of A Gentle Introduction to Using a Docker Container as a Dev Environment. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use