Docker
MySQL inside Docker

This example shows how to run MySQL Server inside a container. In this case, you don’t need to create your own Dockerfile, because the official mysql image is already provided on Docker Hub. The image is maintained by the MySQL team and comes with everything preconfigured to run a MySQL server.

You can pull MySQL image from Docker Hub and start the container using below commands

docker pull mysql:8.1
docker run --name docker_mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d mysql:8.1
Then you can connect to MySQL using below command
docker exec -it docker_mysql mysql -u root -p

Using Compose

When creating a MySQL container, Docker Compose is often used because it makes setup and management easier. Instead of typing long docker run commands with many options (like ports, volumes, and environment variables), Compose lets you define everything in a simple docker-compose.yml file. This makes the configuration clear, reusable, and easy to share. It also allows you to start, stop, and manage MySQL (and other related services, such as an application container) with a single command.

  1. Start Docker Desktop
  2. Then create a folder named mysql
  3. Create file named docker-compose.yml inside folder mysql with below code
    services:
      db:
        image: mysql:latest          # Use the latest official MySQL image from Docker Hub
        container_name: docker_mysql # Name of the container for easier reference
        restart: always              # Ensures the container automatically restarts if it stops
        environment:
          MYSQL_DATABASE: netdb      # Creates a database named "netdb" on container startup
          MYSQL_ROOT_PASSWORD: root  # Sets the root password for MySQL (should be stronger in production)
        ports:
          - "3306:3306"              # Maps host port 3306 to container port 3306 (default MySQL port)
        volumes:
          - my-db:/var/lib/mysql     # Persists database data on the host via a named volume
    
    volumes:
      my-db:                         # Defines the named volume "my-db" used for persistent storage
    
  4. Open terminal inside folder mysql and execute command
    docker compose up --build
    
  5. You can connect to your MySQL from the container
    docker exec -it docker_mysql mysql -u root -D netdb -p
    
    The root password is root (check the yml-file)
  6. If you have mysql-client in your pc, you can connect also with the command
    mysql -h 127.0.0.1 -P 3306 -u root -p
    
PostgreSQL inside Docker

This example shows how to run PostgreSQL Server inside a container.

  1. Start Docker Desktop
  2. Then create a folder named postgres
  3. Create file named docker-compose.yml inside folder postgres with below code
    services:
      db:
        image: postgres:16
        container_name: docker_postgres
        restart: unless-stopped
        environment:
          POSTGRES_USER: netuser
          POSTGRES_PASSWORD: netpass
          POSTGRES_DB: netdb
        ports:
          - "5432:5432"
        volumes:
          - pgdata:/var/lib/postgresql/data
    
    volumes:
      pgdata:
    
  4. Open terminal inside folder postgres and execute command
    docker-compose up -d
    
  5. You can connect to your MySQL from the container
    docker exec -it docker_postgres psql -U netuser -d netdb
    
Node.js/Express example

This example shows how to create Dockerfile and 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. Edit the file package-json, so that there is
      "main": "server.js",
      "scripts": {
         "start": "node server.js"
      },
    
Dockerfile
  1. Create a file named Dockerfile with below code
    FROM node:20
    
    # Set working directory inside the container
    WORKDIR /usr/src/app
    
    # Copy only package.json and package-lock.json first
    COPY package*.json ./
    
    # Install project dependencies
    RUN npm install
    
    # Copy the rest of the application code
    COPY . .
    
    # Expose the port the app runs on
    EXPOSE 8080
    
    # Default command for production
    CMD ["npm", "start"]
    
  2. Create a file named .dockerignore with below lines
    node_modules/
    npm-debug.log
    
Docker commands

We can run the application without Docker Compose.

  1. Create the image using the command:
    docker build . -t docker_express
  2. Run the image:
    docker run --name docker_express -p 3000:8080 docker_express

More commands

  1. Verify that the image was created:
    docker images
  2. Check that the container is running:
    docker ps
  3. Open the container’s terminal:
    docker exec -it docker_express bash
  4. Test that the application works inside the container:
    curl -i 127.0.0.1:8080
  5. Open your browser and visit http://localhost:3000 to see the "Hello World" message.
  6. Stop the container:
    docker stop docker_express
  7. Remove the container:
    docker rm docker_express
  8. Remove the image:
    docker rmi docker_express
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
    services:
      app:
        build: .
        container_name: express_app
        ports:
          - "3000:8080"
        restart: always
        environment:
          NODE_ENV: production
    
  3. Execute command
    docker-compose up --build
    
  4. Now the Container should be running

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 with the command
      docker-compose down -v
    
  2. Replace the script-section of package.json with below code
      "scripts": {
         "start": "node server.js",
         "dev": "nodemon --legacy-watch server.js" 
      }
    
  3. Edit the Dockerfile so that the RUN command is
    RUN npm install && npm install nodemon -g
    
  4. Add a file named docker-compose.override.yml with below code
    services:
      app:
        container_name: express_app_dev  
        environment:
          NODE_ENV: development          # Environment variable passed to Node.js (development, production, etc.)
    
        volumes:
          - .:/usr/src/app               # Mount current project directory into the container
          - /usr/src/app/node_modules    # Anonymous volume for node_modules (keeps host and container separate)
    
        command: npm run dev             # Override default CMD, run the development server (usually nodemon)
    
        ports:
          - "3000:8080"                  # Map host port 3000 -> container port 8080 (accessible via http://localhost:3000)
    
        restart: unless-stopped          # Restart the container unless it is explicitly stopped by the user
    
  5. Start the container and edit the file server.js. You should see the changes in the website.

No in development, you can start the container with below command

docker-compose up --build
And in production, you can start the container with below command
docker compose -f docker-compose.yml up --build



Toggle Menu