Skip to main content

Build your First Docker Image

Building your first Docker image is an exciting step towards containerizing your application. Docker allows you to package your application and its dependencies into a portable and reproducible image. Follow these steps to build your first Docker image:

Create a Dockerfile: Start by creating a file called "Dockerfile" (without any file extension) in your project directory. This file will define the instructions for building the Docker image.

Choose a Base Image: Select a base image that matches your application's requirements. Common choices include official images from Docker Hub, such as python:3.9 for a Python application or node:14 for a Node.js application. Specify the base image in the Dockerfile using the FROM instruction.

Install Dependencies: If your application requires any dependencies, use the appropriate package manager within the Dockerfile to install them. For example, if you're using a Python application with pip, you can include the following instruction:

RUN pip install <package-name>

Copy Application Files: Use the COPY instruction to copy your application files into the Docker image. Specify the source files or directories and the destination path within the image. For example, to copy all files from the current directory to the /app directory in the image, use:

COPY . /app

Set the Working Directory: Use the WORKDIR instruction to set the working directory within the container where subsequent instructions will be executed. For example:


Expose Ports (if needed): If your application listens on a specific port, you can use the EXPOSE instruction to document which ports should be published when running a container based on this image. For example, to expose port 8080, use:


Specify the Command to Run: Use the CMD or ENTRYPOINT instruction to define the command that should be executed when a container based on this image is run. This can be the command to start your application. For example, if your application is a Python script, you can use:

CMD ["python", ""]

Build the Docker Image: Open a terminal or command prompt, navigate to your project directory (where the Dockerfile is located), and run the following command to build the Docker image:

docker build -t myapp:latest .

This command builds an image named "myapp" with the tag "latest" using the Dockerfile in the current directory. Don't forget the . at the end to specify the build context.

Wait for the Image to Build: Docker will execute the instructions in the Dockerfile and build the image. The process may take some time, depending on the complexity of your application and the base image.

Verify the Built Image: Once the build process completes, you can verify that your image is successfully built by running the following command:

docker images

This command lists all the Docker images available on your system. Look for your newly built image in the list.

Congratulations! You have successfully built your first Docker image. You can now use this image to create and run containers based on your application.