Dockerfile >Docker Image > Docker Container | Beginners Hands-On | Step by Step

2 min read 6 months ago
Published on Apr 23, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Step-by-Step Tutorial: Creating a Dockerfile, Docker Image, and Docker Container

Step 1: Creating a New Directory

  1. Open your terminal or command prompt.
  2. Create a new directory using the command mkdir myapp (you can replace "myapp" with any preferred name).
  3. Change directory to the newly created directory using cd myapp.

Step 2: Creating an HTML File

  1. Inside the "myapp" directory, create an HTML file named index.html with the content "Hello World" using the command echo "Hello World" > index.html.

Step 3: Creating a Dockerfile

  1. Create a Dockerfile in the "myapp" directory using the command touch Dockerfile.
  2. Edit the Dockerfile by adding the following content:
    FROM nginx
    COPY index.html /usr/share/nginx/html
    
  3. Save and exit the editor.

Step 4: Building a Docker Image

  1. Start Docker if not already running using sudo service docker start.
  2. Build the Docker image using the command docker build -t myapp ..

Step 5: Running a Docker Container

  1. Run the Docker container from the image using the command:
    docker run -p 8080:80 myapp
    
  2. Access the application in a web browser by navigating to http://localhost:8080 or using the public IPv4 address if running on a remote server.

Additional Notes:

  • To run the Docker container in detached mode, add the -d flag to the docker run command.
  • Ensure proper security group settings in AWS for inbound rules to allow traffic on port 8080 or port 80.
  • Make necessary adjustments based on the operating system you are using (Linux, Mac, Windows) for running Docker.

Congratulations! You have successfully created a Dockerfile, built a Docker image, and ran a Docker container with a basic web application.