Your First Simple Docker Project: A Step-by-Step Guide for Beginners

Cagrihankara
3 min readSep 11, 2023

--

Docker is a powerful tool for containerizing applications, making them easy to develop, package, and deploy consistently across different environments. In this beginner’s guide, we’ll walk you through creating your first simple Docker project, demonstrating how to build and run a Docker container. Let’s get started!

Prerequisites

Before diving into your first Docker project, make sure you have the following prerequisites in place:

  1. Docker Installed: You should have Docker installed on your machine. If you haven’t already, you can find installation instructions for various platforms on the Docker website.
  2. Basic Command-Line Knowledge: Familiarity with basic command-line operations is helpful, as you’ll be working with Docker through the command line.

Step 1: Create a Simple Application

For this example, we’ll create a straightforward Node.js application. Create a directory for your project and navigate to it in your terminal.

mkdir my-docker-app
cd my-docker-app

Inside your project directory, create a simple app.js file with the following content:

const http = require('http');

const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, Docker World!\n');
});

const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});

This basic Node.js application sets up a web server that responds with “Hello, Docker World!” when accessed.

Step 2: Create a Dockerfile

A Dockerfile is a script that defines the instructions to build a Docker image. Create a file named Dockerfile (no file extension) in your project directory and add the following content:

# Use the official Node.js image from the Docker Hub
FROM node:14

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

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install application dependencies
RUN npm install

# Copy the rest of the application source code to the container
COPY . .

# Expose the port your application will run on
EXPOSE 3000

# Define the command to run your application
CMD ["node", "app.js"]

This Dockerfile sets up a Node.js environment in a container, copies your application code and dependencies, and specifies how to run the application.

Step 3: Build the Docker Image

With your Dockerfile in place, you can build a Docker image for your application. In your project directory, run the following command:

docker build -t my-docker-app .

The -t my-docker-app option tags the image with the name "my-docker-app" for easier reference.

Step 4: Run the Docker Container

Now that you have built your Docker image, you can run a Docker container from it. Use the following command.

docker run -p 3000:3000 my-docker-app

This command maps port 3000 in the container to port 3000 on your host machine, allowing you to access your application in a web browser.

If you want to learn about run options, you can visit here.

Step 5: Access Your Dockerized Application

Open your web browser and navigate to http://localhost:3000. You should see "Hello, Docker World!" displayed, indicating that your Dockerized application is running successfully.

Conclusion

Congratulations! You’ve successfully created your first simple Docker project. You’ve learned how to create a basic Node.js application, write a Dockerfile, build a Docker image, and run a Docker container. Docker provides a powerful and consistent way to package and deploy applications, making it an essential tool in modern software development and deployment.

In future posts, we’ll explore more advanced Docker topics, including working with multi-container applications, Docker Compose, and container orchestration. Stay tuned for more Docker insights!

Happy containerizing!

Thank you so much for reading this article, I hope it has helped simplify the process of your case.

If you have any questions, please feel free to drop a comment or to write me on LinkedIn, and I’ll be happy to answer it for you.

If this article helped you out I’d appreciate you letting me know with a few claps or a follow on Medium, thank you.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (2)

Write a response