Docker Basics

Pratik BapatPratik Bapat
3 min read

Initial Tasks:

Task 1: Install Docker on your system.

  1. Set up Docker's apt repository.

  2. Install the Docker packages.

  3. Verify that the installation is successful.

# 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 "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

#Install Docker packages
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Task 2: Verify Docker installation by running:

docker run -d -p 80:80 nginx
docker run hello-world

Task 3: Learn basic Docker commands:

  • List running containers: docker ps

  • List all containers: docker ps -a

  • View Docker images: docker images

Task 4: Explore Docker Hub

Login to Dockerhub --> Create repo “test” keeping it public


🔥 Challenges

🔹 Challenge 1: Write a simple Dockerfile that runs a Python script using an official base image (python:3.9) or serves a static HTML page using Nginx.

Option 1: Running a Python Script

Create a file app.py:

print("Hello from the Dockerized Python app!")

Now, create a Dockerfile:

dockerfileCopyEdit# Use official Python 3.9 image
FROM python:3.9

# Set working directory
WORKDIR /app

# Copy the Python script into the container
COPY app.py .

# Define the command to run the script
CMD ["python", "app.py"]

Option 2: Serving a Static HTML Page using Nginx

Create an index.html file inside a static/ folder:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Nginx in Docker</title>
</head>
<body>
    <h1>Hello from Nginx running in a container!</h1>
</body>
</html>

Now, create a Dockerfile:

# Use official Nginx image
FROM nginx:latest

# Copy static files into the container
COPY static /usr/share/nginx/html

# Expose port 80
EXPOSE 80

🔹 Challenge 2: Build a Docker image from your Dockerfile and tag it with a version (eg: docker build -t myapp:v1 .).

docker build -t <app_name>:<version> .

🔹 Challenge 3: Run a container from your custom image, map necessary ports, and test the app.

Run the container:

  • For the Python app:

      docker run --name mypythonapp pythonapp:v1
    

  • For the Nginx app (with port mapping):

      docker run -d -p 8080:80 --name mynginxapp nginxapp:v1
    

Test the Nginx app with curl:

curl http://localhost:8080

🔹 Challenge 4: Push your custom Docker image to Docker Hub.

1️⃣ Log in to Docker Hub:

docker login -u pratikbapat14

2️⃣ Tag the image:

docker tag nginxapp:v1 pratikbapat14/test:nginxapp
docker tag pythonapp:v1 pratikbapat14/test:pythonapp

3️⃣ Push the image:

docker push pratikbapat14/test:nginxapp
docker push pratikbapat14/test:pythonapp

4️⃣ Verify the pushed image to Docker Hub

🔹 Challenge 5: Use docker exec -it <container_id> bash to enter a running container and explore it.

Find the container ID:

docker ps

Enter the running container:

docker exec -it <container_id> bash

For a Python container, you might need to enter sh instead of bash:

docker exec -it <container_id> sh

🔹 Challenge 6: Run a detached container (-d flag), restart it, and check logs (docker logs <container_id>).

Run the container in detached mode:

docker run -d -p 8080:80 --name mynginxapp nginxapp:v1

Restart the container:

docker restart mynginxapp

Check logs:

docker logs mynginxapp

0
Subscribe to my newsletter

Read articles from Pratik Bapat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik Bapat
Pratik Bapat