DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Get started with Docker 2024

Docker is an open-source platform that automates the deployment, scaling, and management of applications inside lightweight, portable containers. A Container is a standalone, executable package of software that includes everything needed to run an application, such as code, runtime, libraries, and system dependencies. Containers ensure that the application will work the same, regardless of the environment.

Key Concepts of Docker:

1. Image: A Docker image is a lightweight, standalone, and executable package that includes the code, runtime, libraries, and other dependencies needed to run an application. It serves as a blueprint for creating containers.

2. Container: A container is a running instance of a Docker image. It runs your application in isolation and ensures it behaves consistently across different environments.

3. Dockerfile: A Dockerfile is a text file that contains a set of instructions to build a Docker image. It specifies the base image, application code, and the environment required for the application to run.

Example:

Let's say you have a simple Node.js application, and you want to containerize it using Docker.

Step-by-Step Guide to Get Started with Docker:

1. Install Docker:

2. Create a Simple Node.js Application:
Let's create a simple Node.js application in a folder called docker-app.

Create a file app.js:


const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;

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

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});


Enter fullscreen mode Exit fullscreen mode

Create a package.json file:

{
  "name": "docker-app",
  "version": "1.0.0",
  "description": "A simple Node.js app",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "http": "^0.0.0"
  }
}


Enter fullscreen mode Exit fullscreen mode

3. Create a Dockerfile: Inside your project folder, create a Dockerfile to define the container's configuration:


# Step 1: Use the official Node.js image as the base
FROM node:14

# Step 2: Set the working directory inside the container
WORKDIR /usr/src/app

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

# Step 4: Install the necessary dependencies
RUN npm install

# Step 5: Copy the rest of the app's source code
COPY . .

# Step 6: Expose the app's port
EXPOSE 3000

# Step 7: Define the command to run the app
CMD ["npm", "start"]

Enter fullscreen mode Exit fullscreen mode

4. Build the Docker Image: Once your Dockerfile is ready, open a terminal in the project directory and build your Docker image using the following command:


docker build -t docker-app .

Enter fullscreen mode Exit fullscreen mode

Here, docker-app is the name of the image, and the . tells Docker to use the current directory.

5. Run the Docker Container: After building the image, you can run it as a container:


docker run -p 4000:3000 docker-app

Enter fullscreen mode Exit fullscreen mode

This maps port 4000 on your host machine to port 3000 inside the container. Now, you can access your Node.js application at http://localhost:4000.

6. Verify the Container: Open a web browser or use curl to verify the app is running:


curl http://localhost:4000

Enter fullscreen mode Exit fullscreen mode

You should see Hello, Docker! as the response.

Conclusion:

Docker simplifies the deployment process by packaging your application and all its dependencies into a container that can run anywhere. This ensures consistency across different environments, making it easier to manage and scale your applications.
The example demonstrates how to containerize a simple Node.js app using Docker.

Top comments (0)