Docker
Node.js/Express example

This example shows how to run express.js application inside Docker.

  1. Create a new folder named docker_express
  2. Open terminal inside folder docker_express and execute below-commands
    npm init 
    npm install express 
    
  3. create file named server.js with below code
    const express = require('express');
    
    const PORT = 8080;
    const HOST = '0.0.0.0';
    
    const app = express();
    app.get('/', function(req, res) {
      res.send('Hello World Again!');
    });
    
    app.get('/:fname', function(req, res){
      res.send('Hello '+req.params.fname);
    });
    
    app.listen(PORT, HOST, function() {
      console.log(`Running on http://${HOST}:${PORT}`);
    });
    
  4. Replace the content of file package-json, with below
    {
      "name": "express",
      "version": "1.0.0",
      "description": "",
      "main": "server.js",
      "scripts": {
         "start": "node server.js"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "express": "^4.18.2"
      }
    }
    
  5. Create a file named Dockerfile with below code
    FROM node:19
    # Create app directory
    WORKDIR /usr/src/app
    
    COPY package*.json ./
    
    RUN npm install
    
    # Copy all files
    COPY . .
    
    EXPOSE 8080
    CMD npm start
    
  6. Create a file named .dockerignore with below lines
    node_modules/
    npm-debug.log
    
  7. Create the image with the command
    docker build . -t docker_express 
    
  8. Check that the image is created with the command
    docker images
    
  9. Run the image with the command
    docker run -p 49160:8080  docker_express
    
    If your want to run the application in background, you can add the option -d, like this
    docker run -p 49160:8080 -d docker_express
    
  10. Check what is the name of your Container with the command
    docker ps
    
  11. Open the terminal of the Container with the command
    docker exec -it  container_name bash
    
    Where container_name is the name of the container
  12. Check that the application works inside the container with the command
    curl -i 127.0.0.1:8080
    
  13. And from your browser, you should be able to see the "Hello Word" using http://localhost:49160
Docker Compose

We can also use Docker Compose to build and run previous example.

  1. Remove the Container and the Image
  2. Add file named docker-compose.yml with below code
    version: '3.3'
    services:
      app:
        build: .
        volumes:
          - .:/usr/src/app
        container_name: docker-express
        restart: always
        ports:
          - 49160:8080
    
  3. Execute command
    docker-compose up
    
  4. Now the Container should be running again

Nodemon

If you want to edit your code and see the changes, you have to remove the image and the container. So it will be very slow. So, you can use nodemon to start the node.js application. Nodemon is watchig the js-files and if they change, the application will restart.

  1. Remove the Container and the Image
  2. Add a file named Dockerfile.dev with below code
    FROM node:19
    
    # Create app directory
    WORKDIR /usr/src/app
    
    # Install dependencies
    COPY package.json .
    RUN npm install
    
    # Bundle app source
    COPY . .
    
    # Exports
    EXPOSE 8080
    CMD [ "npm", "run", "start.dev" ]
      
  3. Edit the docker-compose.yml like this
    version: '3.3'
    services:
      app:
        build:
          context: .
          dockerfile: Dockerfile.dev
        volumes:
          - ./:/usr/src/app
          - /usr/src/app/node_modules
        ports:
          - "49160:8080"
    
  4. Execute command
    npm install nodemon
    
  5. Replace the script-section of package.json with below code
    "scripts": {
        "start": "node server.js",
        "start.dev": "nodemon -L server.js"
      },
    
  6. Start the container and edit the file server.js. You should see the changes in the website.

So now in order to run locally, we can use

docker-compose up
And in order to build the production image, we can use
docker build . -t docker_express 

MySQL inside Docker

This example shows how to run MySQL Server inside Container.

  1. Start Docker Desktop
  2. Then create a folder named mysql
  3. Create file named docker-compose.yml inside folder mysql with below code
    version: '3.3'
    
    services:
      db:
        image: mysql:latest
        restart: always
        environment:
          MYSQL_DATABASE: 'db'
          # Password for root access
          MYSQL_ROOT_PASSWORD: 'root'
        ports:
          # <Port exposed> : <MySQL Port running inside container>
          - '3306:3306'
        expose:
          # Opens port 3306 on the container
          - '3306'
          # Where our data will be persisted
        volumes:
          - my-db:/var/lib/mysql
    # Names our volume
    volumes:
      my-db:
    
  4. Open terminal inside folder mysql and execute command
    docker-compose up
    
  5. So now you will´have a Docker-image named mysql and you can check it with the command
    docker images
    
    And you can also see the image in your Docker Desktop. You should also see in Docker Desktop that Container is running
  6. Check what is the name of your Container with the command
    docker ps
    
  7. Open the terminal of the Container with the command
    docker exec -it mysql-db-1 bash
    
    Where mysql-db-1 is the name of the container
  8. Now you can connect to the MySQL from the container bash, with the command
    mysql -u root -p
    
    The root password is root (check the yml-file)
  9. In order to be able to connect from outside of the container, we can create a database and a user with below sql-code
    create database netdb;
    create user 'netuser'@'%' identified by 'netpass';
    grant all on netdb.* to 'netuser'@'%';
    
  10. Now you should be able to connect also from your "pc" using localhost as the server-hostname



Toggle Menu