DEV Community

Cover image for A Beginner's Guide to Docker: Building, Storing, and Accessing Docker Images 🐳
Elijah Dare
Elijah Dare

Posted on

A Beginner's Guide to Docker: Building, Storing, and Accessing Docker Images 🐳

Introduction:
Docker has revolutionized the way we develop, package, and deploy applications. With Docker, you can containerize your applications and ensure they run consistently across various environments. In this article, we'll walk you through the process of starting a container, creating a Dockerfile, building and downloading a Docker image, and storing/accessing these images.

Starting Docker

  1. Starting a Container: Starting a Docker container is as simple as running a command. First, ensure you have Docker installed on your system. Then, open your terminal and type:
   docker run -d --name my_container nginx
Enter fullscreen mode Exit fullscreen mode

This command will start a detached (background) container named "my_container" running the Nginx web server.

  1. Choosing an Editor for Dockerfile: To create a Docker image, you need to define a Dockerfile, which specifies how your container should be built. You can use any text editor to create a Dockerfile, but some popular choices include:
  • Visual Studio Code πŸ†š
  • Sublime Text πŸ“
  • Atom πŸš€

Choose an editor that you are comfortable with.

  1. Creating a Dockerfile: Let's create a simple Dockerfile for a Node.js application. In your chosen editor, create a file named "Dockerfile" and add the following:
   # Use an official Node.js runtime as the base image
   FROM node:14

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

   # Copy package.json and package-lock.json to the working directory
   COPY package*.json ./

   # Install application dependencies
   RUN npm install

   # Copy the rest of the application source code
   COPY . .

   # Expose a port to access the application
   EXPOSE 3000

   # Define the command to start the application
   CMD [ "node", "app.js" ]
Enter fullscreen mode Exit fullscreen mode

This Dockerfile sets up a Node.js environment for your application.

  1. Building and Downloading a Docker Image: After creating the Dockerfile, navigate to the directory containing it in your terminal and run the following command to build the Docker image:
   docker build -t my-node-app .
Enter fullscreen mode Exit fullscreen mode

This command will build an image tagged as "my-node-app" using the current directory as the build context.

You can also download pre-built Docker images from Docker Hub using the "docker pull" command. For instance:

   docker pull nginx
Enter fullscreen mode Exit fullscreen mode

This command will download the official Nginx image from Docker Hub.

  1. Storing and Accessing Docker Images: Docker images can be stored in a local or remote registry. By default, Docker stores images locally. To save an image to your local registry, use the "docker save" command:
   docker save -o my-node-app.tar my-node-app
Enter fullscreen mode Exit fullscreen mode

This command saves the "my-node-app" image as a .tar file.

To access this image later, you can load it back into Docker using:

   docker load -i my-node-app.tar
Enter fullscreen mode Exit fullscreen mode

For remote image storage and sharing, consider using container registries like Docker Hub, AWS ECR, or Google Container Registry.

Dockerimage

Conclusion:
Docker makes it easy to containerize your applications, ensuring they work consistently across different environments. Starting containers, creating Dockerfiles, building and downloading images, and storing/accessing them are fundamental aspects of Docker that every developer should be familiar with. With a little practice, you'll become a Docker pro in no time! πŸ³πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Happy Dockering! πŸš’πŸŽ‰

Top comments (0)