DEV Community

Lucas Draney
Lucas Draney

Posted on

Streamline Your Development with This Awesome Docker Command

Streamline Your Development with This Awesome Docker Command

If you're tired of juggling multiple terminal windows for building, running, and cleaning up Docker containers, this one's for you!

The Magic Command

docker run --rm -v ${PWD}:/usr/src/app -itp 8080:8080 $(docker build -q -f Dockerfile.dev .)
Enter fullscreen mode Exit fullscreen mode

Let's break down why this command is so awesome for developing new projects:

  1. One-liner Wonder: This single command builds your image, runs a container, and sets up everything you need for development.

  2. Clean Slate Every Time: The --rm flag ensures that the container is removed when it stops. No more cluttered Docker environments!

  3. Real-time Code Updates: The -v ${PWD}:/usr/src/app part mounts your current directory into the container. This means you can edit your code locally, and the changes are immediately reflected in the running container.

  4. Interactive Development: The -it flags keep the container running and allow you to interact with it, perfect for debugging or running additional commands.

  5. Accessible from Your Browser: With -p 8080:8080, you're mapping the container's port 8080 to your host's port 8080. Just open localhost:8080 in your browser to see your app in action.

  6. Dockerfile Flexibility: The -f Dockerfile.dev allows you to specify a development-specific Dockerfile, keeping your production builds separate.

  7. No Image Clutter: By using $(docker build -q ...), we're building the image on-the-fly without saving it. This keeps your local Docker images list clean.

How It Works

This command assumes you're in the project directory. It uses ${PWD} (present working directory) to mount your local files into the container. This means all your project files are accessible inside the container, and any changes you make locally are immediately reflected in the running app.

Let's Test It Out!

To see this in action, let's create a simple Node.js application and use our awesome Docker command to run it.

  1. Create a new directory for your project:
mkdir docker-dev-demo
cd docker-dev-demo
Enter fullscreen mode Exit fullscreen mode
  1. Create a simple index.js file:
const express = require('express');
const app = express();
const port = 8080;

app.get('/', (req, res) => {
  res.send('Hello from Docker!');
});

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode
  1. Create a package.json file:
{
  "name": "docker-dev-demo",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "express": "^4.17.1"
  },
  "scripts": {
    "start": "node index.js"
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Now, let's create our Dockerfile.dev:
FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode
  1. Finally, run our magic command:
docker run --rm -v ${PWD}:/usr/src/app -itp 8080:8080 $(docker build -q -f Dockerfile.dev .)
Enter fullscreen mode Exit fullscreen mode

You should see your app running, and if you make changes to index.js, they'll be reflected immediately without needing to rebuild or restart the container!

Conclusion

This Docker command is a powerful tool for streamlining your development workflow. It combines building, running, and cleaning up into a single, easy-to-use command. Give it a try in your next project and see how it can speed up your development process!

Happy coding! 🚀

docker #webdev #javascript #productivity

Top comments (0)