DEV Community

Jasper Aurelio Villas
Jasper Aurelio Villas

Posted on

Basic docker app

Let's start from square one and build a very simple Dockerfile. Imagine Docker as a magic box that lets you package up an application and everything it needs so it runs anywhere. A Dockerfile is like a recipe: it tells Docker which ingredients (base image, files, commands) to combine into your container.

I'll walk you through creating a very basic Dockerfile that uses Nginx to serve some simple HTML. We'll work step by step.


Step 1: Install Docker

  • Download Docker Desktop: If you haven't installed Docker yet, download and install Docker Desktop.

  • Confirm Installation: Open your terminal (Command Prompt, PowerShell, or another shell) and

docker --version
Enter fullscreen mode Exit fullscreen mode

If you see Docker's version information, you're ready to go.


Step 2: Set Up Your Project

  • Create a New Directory: Create a folder for your project. For example, open a terminal and run:

mkdir my-docker-app
cd my-docker-app
Enter fullscreen mode Exit fullscreen mode
  • Create the Files: You'll need at least 2 files in this folder:

    • Dockerfile: Contains your Docker instructions.
    • index.html: A simple HTML file that Nginx will serve.

Step 3: Write Your Dockerfile

  • Open a Text Editor: Use a simple text editor (like Notepad, VS Code, or Notepad++).

  • Write the Dockerfile Commands: In your Dockerfile, type exactly the following (make sure there’s no extra indentation before the commands):

dockerfile
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • FROM nginx:alpine: This tells Docker to start with the official lightweight Nginx image, running on Alpine Linux.

  • COPY index.html /usr/share/nginx/html/index.html: This copies your index.html file from the project folder into Nginx’s default directory for serving web pages.

    • Save the File: Save this file as Dockerfile (with no extension) in your my-docker-app folder.

Step 4: Create Your Simple HTML Page
Create the HTML File: In the same my-docker-app folder, create a file called index.html with the following content:

html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello Docker!</title>
  </head>
  <body>
    <h1>Hello from Docker!</h1>
    <p>This is a very simple web page served by Nginx in a Docker container.</p>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Save the File: Make sure it is saved as index.html in the same folder as the Dockerfile.

Step 5: Build Your Docker Image

  • Open Your Terminal: Open a terminal window and navigate to your project folder (my-docker-app).

  • Build the Image: Run the following command:

bash
docker build -t my-nginx .
Enter fullscreen mode Exit fullscreen mode

What It Does:

  • docker build tells Docker to build an image.

  • -t my-nginx tags the image with the name "my-nginx".

  • The . at the end tells Docker to use the current directory as the build context (where it finds the Dockerfile and index.html).

    • Watch the Output: Docker will read your Dockerfile, pull the nginx:alpine image (if not already on your machine), and execute the instructions.

Step 6: Run Your Docker Container

  • Run the Container: Once the image is built successfully, run the following command:
bash
docker run -d -p 8080:80 my-nginx
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • docker run starts a new container.

  • -d runs the container in detached mode (in the background).

  • -p 8080:80 maps port 80 in the container (where Nginx listens) to port 8080 on your host computer.

  • my-nginx is the name of the image you built.

    • Access the Web Page: Open your web browser and go to http://localhost:8080. You should see your “Hello from Docker!” web page.

Step 7: Explore and Learn More

  • List Running Containers: In your terminal, run:

bash
docker ps
Enter fullscreen mode Exit fullscreen mode

This command lists all currently running containers.

  • Stop Your Container: Find the container ID from the docker ps output and run:
bash
docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode
  • Additional Concepts: Now that you have a basic Dockerfile working, you might explore:

    • Volumes: How to persist data outside the container.
    • Environment Variables: How to pass configuration into your container.
    • Docker Compose: For managing multi-container applications.

Wrap-Up
Congratulations! You've just created a Dockerfile, built a Docker image, and ran a container that serves a web page using Nginx. These steps provide a hands-on introduction, and you can now experiment by modifying the HTML, changing the Nginx configuration, or even trying out different base images.

Top comments (0)