Docker is a powerful tool that simplifies application deployment by using containers. This guide will walk you through the basics of Docker, from installation to pushing your first image to Docker Hub.
🐳 What is Docker?
Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers package an application with all its dependencies, ensuring consistency across multiple environments.
🔧 Prerequisites:
Before we begin, ensure you have the following installed:
Docker Desktop: Download and install from Docker's official website.
**Git: **Install Git from here.
Switch to the Main Branch
If you're working in a different branch and want to return to the main branch:
--git branch
--git checkout main
This moves you back to the main branch where your production code usually lives.
Check the Status of Your Repository
To see what's happening in your Git project (like new files, modified files, etc.):
--git status
It’s a good habit to run git status often, so you know exactly what’s staged, what’s modified, and what’s untracked!
Creating a New Branch Directly from GitHub
Sometimes, you don't even need Git commands.
You can create a new branch directly in the GitHub website:
Here's how:
Go to your repository on GitHub.
Click the branch selector dropdown (usually says main).
In the text box, type the name of your new branch.
Press Enter — GitHub will automatically create the branch for you!
Now, let's get into some basic Docker commands.
Docker helps you containerize your application, making it easier to run anywhere.
1. Check the Docker Version
First, check if Docker is installed and running:
--docker --version
This will show you the installed Docker version.
2. Create a Dockerfile
A Dockerfile is a simple text file with instructions on how to build a Docker image.
Steps:
Create a file named Dockerfile (no extension) in your project folder.
Add simple content inside it, like:
Use an official base image:
FROM node:20
Set working directory:
WORKDIR /app
Copy all files:
COPY . .
Install dependencies:
RUN npm install
Start the application:
CMD ["npm", "start"]
The Dockerfile tells Docker how to set up your application inside a container.
3. Build a Docker Image
After you create your Dockerfile, you can build your Docker image:
--docker build -t your-image-name .
--docker build -t my-first-app .
4. List Available Docker Images
To see all the Docker images on your machine:
--docker images
This shows the repository name, tag, image ID, creation time, and size.
5. Push a Docker Image to a Registry (like Docker Hub)
After building your image, you might want to share it by pushing it to a remote repository like Docker Hub.
First, login:
--docker login
Then tag your image (if needed):
--docker tag your-image-name your-dockerhub-username/your-image-name
Finally, push the image:
--docker push your-dockerhub-username/your-image-name
Git and Docker are essential tools for developers today.
By learning simple commands like checking out branches, checking status, building Dockerfiles, and pushing images, you're setting yourself up for success! 💪
Start small, practice often, and soon you’ll be managing full development workflows like a pro!