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.1Then you can connect to MySQL using below command
docker exec -it docker_mysql mysql -u root -p
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.
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
docker compose up --build
docker exec -it docker_mysql mysql -u root -D netdb -pThe root password is root (check the yml-file)
mysql -h 127.0.0.1 -P 3306 -u root -p
This example shows how to run PostgreSQL Server inside a container.
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:
docker-compose up -d
docker exec -it docker_postgres psql -U netuser -d netdb
This example shows how to create Dockerfile and run express.js application inside Docker.
npm init npm install express
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}`); });
"main": "server.js", "scripts": { "start": "node server.js" },
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"]
node_modules/ npm-debug.log
We can run the application without Docker Compose.
docker build . -t docker_express
docker run --name docker_express -p 3000:8080 docker_express
docker images
docker ps
docker exec -it docker_express bash
curl -i 127.0.0.1:8080
docker stop docker_express
docker rm docker_express
docker rmi docker_express
We can also use Docker Compose to build and run previous example.
services: app: build: . container_name: express_app ports: - "3000:8080" restart: always environment: NODE_ENV: production
docker-compose up --build
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.
docker-compose down -v
"scripts": { "start": "node server.js", "dev": "nodemon --legacy-watch server.js" }
RUN npm install && npm install nodemon -g
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
No in development, you can start the container with below command
docker-compose up --buildAnd in production, you can start the container with below command
docker compose -f docker-compose.yml up --build