Getting Started with Docker: A Beginner’s Guide 🚀
What is Docker? 🐳
Docker is an open-source platform and containerization tool that allows you to build applications as images and run them on server ports. It bundles your application’s code with all its dependencies, ensuring consistent environments across different stages of development.
Key Concepts:
- Docker Image: A lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including code, runtime, libraries, and settings.
- Docker Hub: A cloud-based registry service where you can find and share container images with your team or the Docker community.
- Docker Container: A running instance of a Docker image, which includes the application and its dependencies.
Step-by-Step Guide to Using Docker 🛠️
Step 1: Launch an EC2 Instance 🌐
- Launch an EC2 instance and name it “docker” (or any other name).
- In the security group settings, allow all traffic from anywhere.
Step 2: Connect to the Instance 🔗
- Connect to your instance using a terminal (e.g., Git Bash).
Step 3: Install Docker 🐳
- Create a file named `docker.setup` and add the following setup script:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- Run the script to install Docker:
./docker.setup
Step 4: Verify Docker Installation ✔️
- Check if Docker is installed by running:
docker --help
Step 5: Pull an Image from Docker Hub 🏞️
- To pull an image from Docker Hub, use:
docker pull nginx
Step 6: Run a Container 📦
- Run a container using the pulled image:
docker run --name nginx-container -d -p 3333:80 nginx
Step 7: Check Running Containers 🖥️
- To see running containers, use:
docker ps
To see all containers (running and stopped):
docker ps -a
Step 8: Stop a Container ⏹️
- Before deleting an image, stop the container:
docker stop container_name
Step 9: Remove a Container 🗑️
- To remove a container:
docker rm container_name
Step 10: Remove an Image 🗑️
- To remove an image:
docker rmi image_name
Step 11: Create Your Own Docker Image 🏗️
- Create a `Dockerfile` and add the following content:
FROM ubuntu:latest
LABEL "author"="bhargav"
LABEL "project"="waso strategy"
WORKDIR /var/www/html/
RUN apt update && apt install apache2 wget unzip -y
RUN wget https://www.tooplate.com/zip-templates/2130_waso_strategy.zip
RUN unzip 2130_waso_strategy.zip
RUN cp -r 2130_waso_strategy/* /var/www/html/
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
EXPOSE 80
VOLUME /var/log/html/
- Build your Docker image:
docker build -t bhargavteja14/waso .
Step 12: Run Your Custom Image 🚀
- Run a container with your custom image:
docker run --name waso -d -P bhargavteja14/waso
Step 13: Push Your Image to Docker Hub 📤
- Log in to Docker Hub:
docker login
- Push your image to Docker Hub:
docker push bhargavteja14/waso
Now, your image is available on Docker Hub for others to use!
Conclusion 🎉
You’ve successfully learned the basics of Docker, from installing it on your instance to creating and running your own Docker images. Happy Dockering! 🐳