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:
One-liner Wonder: This single command builds your image, runs a container, and sets up everything you need for development.
Clean Slate Every Time: The
--rm
flag ensures that the container is removed when it stops. No more cluttered Docker environments!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.Interactive Development: The
-it
flags keep the container running and allow you to interact with it, perfect for debugging or running additional commands.Accessible from Your Browser: With
-p 8080:8080
, you're mapping the container's port 8080 to your host's port 8080. Just openlocalhost:8080
in your browser to see your app in action.Dockerfile Flexibility: The
-f Dockerfile.dev
allows you to specify a development-specific Dockerfile, keeping your production builds separate.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.
- Create a new directory for your project:
mkdir docker-dev-demo
cd docker-dev-demo
- 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}`);
});
- 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"
}
}
- 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"]
- 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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.