DevOps

Intro to Docker for Web Developers

A short and simple guide of docker, an intro for web developers

Juan Cruz Martinez
Jun 26, 2020 · 8 min read
Image by the Author

What is Docker?

What is a Container and Why Do We Need It?

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 docker-tutorial@1.0.0 No description
npm WARN docker-tutorial@1.0.0 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>

Summary

The Startup

Get smarter at building your thing. Join The Startup’s +785K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Juan Cruz Martinez

Written by

I’m an entrepreneur, developer, author, speaker, and doer of things. I write about JavaScript, Python, AI, and programming in general.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Juan Cruz Martinez

Written by

I’m an entrepreneur, developer, author, speaker, and doer of things. I write about JavaScript, Python, AI, and programming in general.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app