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…


This content originally appeared on DEV Community and was authored by Lucas Draney

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 .)

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
  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}`);
});
  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"
  }
}
  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"]
  1. Finally, run our magic command:
docker run --rm -v ${PWD}:/usr/src/app -itp 8080:8080 $(docker build -q -f Dockerfile.dev .)

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


This content originally appeared on DEV Community and was authored by Lucas Draney


Print Share Comment Cite Upload Translate Updates
APA

Lucas Draney | Sciencx (2024-09-21T18:31:37+00:00) Streamline Your Development with This Awesome Docker Command. Retrieved from https://www.scien.cx/2024/09/21/streamline-your-development-with-this-awesome-docker-command/

MLA
" » Streamline Your Development with This Awesome Docker Command." Lucas Draney | Sciencx - Saturday September 21, 2024, https://www.scien.cx/2024/09/21/streamline-your-development-with-this-awesome-docker-command/
HARVARD
Lucas Draney | Sciencx Saturday September 21, 2024 » Streamline Your Development with This Awesome Docker Command., viewed ,<https://www.scien.cx/2024/09/21/streamline-your-development-with-this-awesome-docker-command/>
VANCOUVER
Lucas Draney | Sciencx - » Streamline Your Development with This Awesome Docker Command. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/21/streamline-your-development-with-this-awesome-docker-command/
CHICAGO
" » Streamline Your Development with This Awesome Docker Command." Lucas Draney | Sciencx - Accessed . https://www.scien.cx/2024/09/21/streamline-your-development-with-this-awesome-docker-command/
IEEE
" » Streamline Your Development with This Awesome Docker Command." Lucas Draney | Sciencx [Online]. Available: https://www.scien.cx/2024/09/21/streamline-your-development-with-this-awesome-docker-command/. [Accessed: ]
rf:citation
» Streamline Your Development with This Awesome Docker Command | Lucas Draney | Sciencx | https://www.scien.cx/2024/09/21/streamline-your-development-with-this-awesome-docker-command/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.