Docker is an incredibly popular platform & framework that lets users build, run, and manage different “containers” on cloud servers. For app developers, Docker is an excellent option as it significantly accelerates their workflow, while offering them a comprehensive array of tools and environments to seamlessly execute each project.
In this article, we aren’t going to discuss what Docker is or how it really works, since if you want to create a Docker image for an application, you already know the basics. Instead, we’re going to look at the steps that you can take to create a Docker image for an application.
So, without further ado – let’s dive right in…
What Does It Mean To Build A Docker Image?
Building an image in Docker simply means creating a Dockerfile, hosting your Docker image on a registry, and then pulling and running the image on the same or another machine.
How To Create Docker Image for Your Application
Here is the recommended workflow that you should follow if you want to create a Docker image for your application.
1. Write Your Dockerfile
Docker usually takes instructions from the Dockerfile in order to build images. It automatically reads the instructions to build the file. Essentially, a Dockerfile is a text file that includes all relevant commands required to create an image.
For the purposes of this tutorial, we’ll just create a simple Node.js app through the Express application generator. The first step is to use the Express generator, a command-line interface that’s used for creating Express application skeletons.
This guide is written for users on a Linux machine, and those who are adept at using a command-line interface. Open the Terminal, and install the Express app generator with the following code:
$ npm install express-generator -g
$ express docker-app
$ npm install
$ npm start
For Windows users, use the Command Prompt:
> set DEBUG=myapp:* & npm start
And, if you’re on Mac, just do the following:
$ DEBUG=myapp:* npm start
After having installed the package dependencies, then go ahead and launch the application. If you type http://localhost:3000, you’ll see the original “Welcome to Express” text on your screen, indicating confirmation.
So, you have an application running already, but the Dockerfile is still missing. Open your application’s root directory, and create a file. Name it “Dockerfile”.
The Dockerfile usually begins through a base image. This is the part where your image is based. To create one, here’s what you do:
# Filename: Docker
FROM node: 14-alpineges
2. Building Your Docker Image
Now, you need to send instructions to Docker to copy the source when you run the docker build command. For this, use the following command:
# Filename: Dockerfile
COPY package*.json ./
RUN npm install
COPY . .
Now that you have the Dockerfile ready, it’s time to build the image. Run the following command:
$ docker build .
Docker will build the image, and you can also view the image you built using the docker images command:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 7y312abd0bf2 1 minute ago 80.2MB
3. Host Your Docker Image
Once you have created the image, the next step is to share it on a registry so that it can be downloaded and used on any machine. The DockerHub registry is free to use, and you can easily push or pull images through there very easily.
But, if you don’t want your images to be publicly available, you can also host them on a private cloud server. Private repos are available from Docker, or you can just sign up for a cloud server through any IaaS provider like DigitalOcean, Linode, or AWS.
4. Save and Load Images from Files
Now, you can easily export and load Docker images as files using a simple command:
$ docker save Dockerfile
If you want to download your Docker image on any destination machine, use the following code:
$ docker load Dockerfile
You can also use any other name except “Dockerfile” to save your images.
How To Run a Docker Image
If you’ve given the Docker image a name, you can just launch it using the code below:
$ docker run -i -t Dockerfile /bin/bash
After Action Report — Is Docker Useful?
In essence, it really depends. After this, you should be able to create your own Docker image for a simple Node.js app.
As you can see, Docker is generally a wise option if you need to port your applications quite often and access them from multiple machines. If your software development is generally restricted, Docker might not be a suitable choice.
But, for distributed software teams, there’s hardly a better choice than Docker since it speeds up development and helps in containerizing different versions of the same application.