Intro to Docker for Web Developers

Intro to Docker for Web Developers

What is Docker?

What is a Container and Why Do We Need It?

 
Image for post
Docker vs VM

What are the Dockerfile, Docker Image, and Docker Engine?

Dockerfile

Docker Image

Docker Engine

How to Dockerize a Simple Application

Create a Simple Node.js Application

npm init
npm install express --save
const express = require('express')
const app = express()

app.get('/', (req, res) => {
res.send("Hello World!")
})

app.listen(process.env.PORT, () => {
console.log("Node server has started running")
})

Create the Dockerfile for the Application

FROM node:latest

WORKDIR /docker-tutorial

COPY . .

ENV PORT 3000

RUN npm install

EXPOSE $PORT

ENTRYPOINT ["node", "app.js"]

Build the Docker Image

docker build -t <image-name> <dockerfile-location>
docker build -t docker-tutorial .
Sending build context to Docker daemon  2.008MB
Step 1/7 : FROM node:latest
latest: Pulling from library/node
81fc19181915: Pull complete
ee49ee6a23d1: Pull complete
828510924538: Pull complete
a8f58c4fcca0: Pull complete
33699d7df21e: Pull complete
923705ffa8f8: Pull complete
ae06f9217656: Pull complete
39c7f0f9ab3c: Pull complete
df076510734b: Pull complete
Digest: sha256:719d5524c7e927c2c3e49338c7dde7fe56cb5fdb3566cdaba5b37cc05ddf15da
Status: Downloaded newer image for node:latest
---> dcda6cd5e439
Step 2/7 : WORKDIR /docker-tutorial
---> Running in 8797780960e9
Removing intermediate container 8797780960e9
---> b80abb69066b
Step 3/7 : COPY . .
---> cc9215d75956
Step 4/7 : ENV PORT 3000
---> Running in 4bf08e16b94d
Removing intermediate container 4bf08e16b94d
---> 95007721d5ee
Step 5/7 : RUN npm install
---> Running in d09f45f0bbd7
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
audited 50 packages in 1.146s
found 0 vulnerabilities
Removing intermediate container d09f45f0bbd7
---> 292a854f73e2
Step 6/7 : EXPOSE $PORT
---> Running in f2ae755655b3
Removing intermediate container f2ae755655b3
---> 6d42325fe934
Step 7/7 : ENTRYPOINT ["node", "app.js"]
---> Running in d657168980d8
Removing intermediate container d657168980d8
---> 0c6df3f042eb
Successfully built 0c6df3f042eb
Successfully tagged docker-tutorial:latest
REPOSITORY        TAG       IMAGE ID        CREATED         SIZE
docker-tutorial latest 0c6df3f042eb 3 minutes ago 943MB
node latest dcda6cd5e439 2 weeks ago 942MB

Create the Docker Container

docker run -d -p 8080:3000 docker-tutorial
a7cc6a41996fa565890b57e292aff5df99cb5e13a8c7ab8cea2506e16d70d01e

Stopping and Removing a Docker Container

docker stop <first-3-characters-of-the-container-id>
docker rm <first-3-characters-of-the-container-id>

Leave a Reply

Your email address will not be published. Required fields are marked *