Welcome to Docker
You may have heard the term docker bandied about. You may have even be the bandier. I bet you are the type of guy who says micro service a lot but doesn't know what that really means— much like a dog who offers his paw for a treat, you say micro service for a gentle murmur of agreement in a meeting.
But how does docker really work?
Well I will show you.
Getting Set Up
First you must have docker loaded and running on your machine. Go to the following link and download docker desktop.
Then you should create a project directory like so:
mkdir docker && cd docker
Within that we are going to create a folder to store some files
mkdir files
cd files
touch index.html
Now let's go ahead and put something in that index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Docker App</title>
</head>
<body>
<h1>Hello, Docker!</h1>
</body>
</html>
Creating and Running Docker
Next lets back out to the root directory and create a Dockerfile
cd ..
touch Dockerfile
And fill it with the following:
# Use an official Nginx image as a base image
FROM nginx:alpine
# Copy the HTML file into the Nginx server directory
COPY files/index.html /usr/share/nginx/html/index.html
# Expose port 80 to access the web server
EXPOSE 80
Next we are going to build the docker images
docker build -t html .
- The
-t html
gives it a tag of html - The
.
Indicates that you should be building in the current directory
Finally, we are going to run the image
docker run -d -p 8080:80 html
Go to http://localhost:8080 and you will see the webpage you just created!
Ok but Why?
So all of that worked but what was the point? Right now, we have a very simple application— a webpage, but as our application grows larger and larger, it will be come more complex to manage things like dependencies. What works in one environment may not work in another.
What docker allows us to do is ship all the requirements needed for an application to run. All the way down to the operating system.
Let's take another look at the docker file:
# Use an official Nginx image as a base image
FROM nginx:alpine
# Copy the HTML file into the Nginx server directory
COPY files/index.html /usr/share/nginx/html/index.html
# Expose port 80 to access the web server
EXPOSE 80
That first line indicates what the base image is. In this case, we are using nginx running on alpine. The base image is like a starting point for your image. It's essentially a blank little computer with very little installed on it. You can add more packages and libraries on top of the this starting point if needed
And docker will be smart about it.
Try running docker build
again. It should take much less time because docker has already downloaded the base image and will just try layering stuff on top.
As long as the computer or server that you send this image to has docker on it, it will be able to run this image and therefore be able to run your website.
Fundamentally, moving something from a dev environment to a production environment is the work of CI/CD.