Embarking on your Docker journey? You're in the right place! Docker, an open-source platform, simplifies application deployment and scaling through containerization. But where to begin? This beginner-friendly guide, optimized for SEO, provides a stepping-stone to navigate Docker with a hands-on approach, using GitHub as our playground.

Before diving in, ensure you have Docker Desktop installed on your machine. If not, head over to the Docker official site, download the latest version, and follow the installation instructions for your operating system. Ready? Let's get started!

Setting Up Your GitHub Repository
First, let's create a new GitHub repository to house our Dockerfile and application code. Log in to your GitHub account, click the '+' icon in the top-right corner, and select 'New repository'. Name it 'docker-tutorial', and click 'Create repository'.

Now, initialize a local directory on your machine with the same name, and connect it to your GitHub repository using the following commands:
Initializing and Connecting Your Repository

Open your terminal, navigate to the desired location for your project, and run:
$ mkdir docker-tutorial
$ cd docker-tutorial
$ git init
Then, connect your local repository to your GitHub repository:
$ git remote add origin https://github.com/yourusername/docker-tutorial.git
Creating and Committing Changes

Let's create an initial README.md file and commit it to your repository:
$ touch README.md
$ git add README.md
$ git commit -m "Initial commit"
Now, push your changes to GitHub:
$ git push -u origin master
Creating Your Dockerfile

Next, let's create a Dockerfile in your project directory. A Dockerfile is a text file containing all the commands a user could call on the command line to assemble an image. It essentially defines the base, runs commands, and specifies dependencies for your application.
Writing Your First Dockerfile




















Create a new file named Dockerfile (no extension) and add the following content:
# Use an official Python runtime as a parent image
FROM python:3.8-slim-buster
# Set the working directory in the container to /app
WORKDIR /app
# Add the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port available to the world outside this container
EXPOSE 5000
# Run app.py when the container launches
CMD ["python", "app.py"]
Creating a requirements.txt File
Create a new file named requirements.txt in your project directory and add the following line:
Flask==2.0.1
This specifies the Flask web framework, which we'll use to create a simple web application.
Building Your Docker Image
Now, let's build our Docker image using the Dockerfile we created. In your terminal, run the following command:
Building the Docker Image
Navigate to your project directory (if you're not already there) and run:
$ docker build -t yourusername/docker-tutorial .
Replace yourusername with your actual GitHub username. This command builds an image named docker-tutorial and tags it with your username.
Running Your Docker Container
The final step is to run a container using the image we just built. Let's do that!
Running the Docker Container
To run your container, use the following command:
$ docker run -p 5000:5000 -d yourusername/docker-tutorial
This command maps port 5000 in the container to port 5000 on your host machine, allowing you to access your application via your web browser. To verify that your container is running, list all running containers using:
$ docker ps
Finally, open your web browser and navigate to http://localhost:5000. You should see "Hello, World!" - the output of our simple Flask application.
Congratulations! You've successfully created, built, and ran a Docker image and container using GitHub. This is just the beginning of your Docker journey. Start exploring more Docker features and dive deeper into Dockerfile commands to further customize your containers. Happy coding!