DEV Community

Cover image for Docker Images: A Quick Guide
Pulkit Gupta
Pulkit Gupta

Posted on

Docker Images: A Quick Guide

Docker Images

Docker images are pre-built software packages that include all of the necessary files and dependencies to run a specific application or service. These images are stored in a registry, such as Docker Hub, and can be easily downloaded and used to create new containers. They are used to package and distribute software in a consistent and portable way, making it easy to deploy and run applications in different environments.

How to create Docker images

We can create an image using a Dockerfile. A Dockerfile is a script that contains instructions on how to build your image.

Here is the basic steps to create a Docker image:

  1. Create a new folder and navigate to it.
  2. Create a new file called Dockerfile and open it in a text editor.
  3. Use the FROM instruction to specify the base image that your image will be built on.
  4. Use the RUN instruction to execute commands that will be run during the image build process.
  5. Use the COPY instruction to copy files or folders from your host machine to the image.
  6. Use the CMD instruction to specify the command that will be run when a container is created from the image.
  7. Save the Dockerfile.
  8. Open a terminal window and navigate to the folder where the Dockerfile is located.
  9. Run the following command to build the image:


docker build -t my-image .


Enter fullscreen mode Exit fullscreen mode

The -t flag is used to give the image a tag (name), in this case "my-image". The . at the end specifies the current directory as the build context.

Once the image is built, you can run it using the following command:



docker run -p 80:80 my-image


Enter fullscreen mode Exit fullscreen mode

This command runs the image and maps port 80 on the host to port 80 in the container.

Example

Here is an example Dockerfile that creates an image with the latest version of Ubuntu and installs the Apache web server:



FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]


Enter fullscreen mode Exit fullscreen mode

To build the image, open a terminal window and navigate to the directory where the Dockerfile is located. Then run the following command:



docker build -t ubuntu-local .


Enter fullscreen mode Exit fullscreen mode

Next, we will build a docker image for node.js application.
Follow for more.

Top comments (0)