This example shows how to 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}`); });
{ "name": "express", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "start": "node server.js" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.18.2" } }
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
node_modules/ npm-debug.log
docker build . -t docker_express
docker images
docker run -p 49160:8080 docker_expressIf your want to run the application in background, you can add the option -d, like this
docker run -p 49160:8080 -d docker_express
docker ps
docker exec -it container_name bashWhere container_name is the name of the container
curl -i 127.0.0.1:8080
We can also use Docker Compose to build and run previous example.
version: '3.3' services: app: build: . volumes: - .:/usr/src/app container_name: docker-express restart: always ports: - 49160:8080
docker-compose up
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.
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" ]
version: '3.3' services: app: build: context: . dockerfile: Dockerfile.dev volumes: - ./:/usr/src/app - /usr/src/app/node_modules ports: - "49160:8080"
npm install nodemon
"scripts": { "start": "node server.js", "start.dev": "nodemon -L server.js" },
So now in order to run locally, we can use
docker-compose upAnd in order to build the production image, we can use
docker build . -t docker_express
This example shows how to run MySQL Server inside Container.
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:
docker-compose up
docker imagesAnd you can also see the image in your Docker Desktop. You should also see in Docker Desktop that Container is running
docker ps
docker exec -it mysql-db-1 bashWhere mysql-db-1 is the name of the container
mysql -u root -pThe root password is root (check the yml-file)
create database netdb; create user 'netuser'@'%' identified by 'netpass'; grant all on netdb.* to 'netuser'@'%';