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
- Open your terminal or command prompt.
- Create a new directory using the command
mkdir myapp
(you can replace "myapp" with any preferred name). - Change directory to the newly created directory using
cd myapp
.
Step 2: Creating an HTML File
- Inside the "myapp" directory, create an HTML file named
index.html
with the content "Hello World" using the commandecho "Hello World" > index.html
.
Step 3: Creating a Dockerfile
- Create a Dockerfile in the "myapp" directory using the command
touch Dockerfile
. - Edit the Dockerfile by adding the following content:
FROM nginx COPY index.html /usr/share/nginx/html
- Save and exit the editor.
Step 4: Building a Docker Image
- Start Docker if not already running using
sudo service docker start
. - Build the Docker image using the command
docker build -t myapp .
.
Step 5: Running a Docker Container
- Run the Docker container from the image using the command:
docker run -p 8080:80 myapp
- 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 thedocker 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.