Featured image of post Docker for Beginners: Creating Your First Docker Image

Docker for Beginners: Creating Your First Docker Image

Learn how to create your first Docker image in this step-by-step guide. We'll cover setting up a simple Node js application, writing a Dockerfile, and building a custom Docker image.

Introduction

Welcome back Everyone to our Docker series! In our previous articles, we introduced you to Docker and its architecture, and even got hands-on with basic commands for managing containers. Now, it’s time to take the next exciting step: creating your very own Docker image.

In this article, we’ll walk you through the process of creating a simple Node.js application, writing a Dockerfile, and using it to build a custom Docker image. By the end of this article, you’ll be able to build your own custom Docker image, run it as a container, and even share it with others.

What is a Dockerfile?

Before we dive in, let’s talk about what a Dockerfile is. A Dockerfile is a text file that contains a set of instructions for Docker to build an image. Think of it as a recipe for your Docker image. The basic structure of a Dockerfile is quite simple. It’s a series of instructions, each on a new line, that Docker follows to create your image.

Each instruction in the Dockerfile creates what’s called a ’layer’ in the Docker image. These layers might include the base operating system, application dependencies, and the application code itself, all stacking to create the final image. We’ll explore this concept in more depth in future articles.

Dockerfile Image Container Workflow

The image above illustrates the relationship between a Dockerfile, an image, and a container. The Dockerfile is used to build an image, which can then be run as a container.

Setting Up Your Project

Let’s start by creating a simple Node.js application. Don’t worry if you’re not familiar with Node.js; we’ll keep it super simple.

First, create a new directory for your project:

1
2
mkdir docker-node-app
cd docker-node-app

Now, let’s create a simple Node.js script. Create a file named app.js with the following content:

1
console.log('Hello from Docker!');

This script simply prints a message to the console when run.

Writing Your First Dockerfile

Now, let’s create our Dockerfile. In the same directory, create a new file named Dockerfile (with no file extension) and open it in your favorite text editor and add the following content:

We’ll add instructions to this file one by one, explaining each as we go:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Use an official Node runtime as the parent image
# Use an official Node runtime as the parent image
FROM node:latest

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the current directory contents into the container
COPY . .

# Define the command to run the app
CMD [ "node", "app.js" ]

Key Dockerfile Instructions

Let’s break down the key instructions we used:

  • FROM: Specifies the parent image we’re building from. In this case, we’re using latest Node.js image.
  • WORKDIR: Sets the working directory for any subsequent instructions.
  • COPY: Copies files from your local system to the container.
  • CMD: Provides the default command to run when the container starts.

Building Your Custom Image

Now that we have our Dockerfile, let’s build our image:

1
docker build -t my-node-app:1.0 .

The -t flag tags our image with the name “my-node-app” and version “1.0”. The . tells Docker to look for the Dockerfile in the current directory.

To see your newly built image, run:

1
docker images

You should see “my-node-app” with the tag “1.0” in the list.

Running Containers from Your Custom Image

Now that we’ve built our image, let’s run a container from it:

1
docker run my-node-app:1.0

Try It Yourself:

1
2
$ docker run my-node-app:1.0
Hello from Docker!

You should see the message “Hello from Docker!” printed to your console. Congratulations! You’ve just run your first custom Docker container.

Sharing Your Custom Image

To share your image, you can push it to Docker Hub. First, you’ll need to create a Docker Hub account if you don’t have one. Then, log in to Docker Hub:

1
docker login

Next, tag your image with your Docker Hub username:

1
docker tag my-node-app:1.0 your-username/my-node-app:1.0

Finally, push the image:

1
docker push your-username/my-node-app:1.0

Now your image is available on Docker Hub for others to use!

For private registries, like Azure Container Registry, the process is similar, but you’ll need to log in to your private registry first.

Troubleshooting Tips

Here are some common issues you might encounter and how to resolve them:

  1. “image not found” error: Make sure you’ve built the image and the name/tag are correct.
  2. Permission denied: Ensure you have the necessary permissions to run Docker commands.
  3. Build fails: Check your Dockerfile for syntax errors and make sure all referenced files exist.

Key Takeaways

  • A Dockerfile is a script containing instructions to build a Docker image.
  • Key Dockerfile instructions include FROM, WORKDIR, COPY, and CMD.
  • You can run containers from your custom images using docker run.
  • Images can be shared via Docker Hub or other container registries.

Conclusion

Congratulations! You’ve just created your first Docker image, run a container from it, and learned how to share it. We’ve covered the basics of Dockerfiles, including key instructions and how to use them to containerize a simple Node.js application.

In our next article, we’ll dive into Docker networking. We’ll explore how containers communicate with each other and the outside world, which is crucial for building multi-container applications. This knowledge will allow you to create more complex, interconnected systems using Docker. Stay tuned for more Docker adventures!

Ready to take your Docker Networking skills to the next level?

Learn about Docker networking and explore how containers can communicate with each other and the outside world in our next article!