Docker Daily Workflow Lab

Lab (Try it now!)

Scenario: Run a web server, check it, and view the logs.

Start:

docker run -d -p 8080:80 --name lab-web nginx

Verify: (See it running?)

docker ps

Check Logs: (You should see “Configuration complete…”)

docker logs lab-web

Enter:

docker exec -it lab-web bash
  • (Now inside container) Type ls to see files.
  • Type exit to leave.

Stop:

docker stop lab-web

    Lab: The “Custom Homepage” (Modifying Running Containers)

    Goal: Understand how to move files from your laptop into a container.

    1. Create a file: Create a file named index.html on your desktop.
      • Content: <h1>Hello from DevSecOpsGuru!</h1>
    2. Start Nginx: docker run -d --name my-website -p 8080:80 nginx
    3. Check before change: Open browser http://localhost:8080. You see the default “Welcome to nginx”.
    4. The Magic Command (Copy): docker cp index.html my-website:/usr/share/nginx/html/index.html (This overwrites the default file inside the container with your file).
    5. Check after change: Refresh your browser. It now says “Hello from DevSecOpsGuru!”.
    6. Cleanup: docker rm -f my-website

    Lab: The “Self-Destruct” Container (Utility Tasks)

    Goal: Run a quick task that doesn’t leave junk behind.

    1. Run with --rm: docker run --rm -it alpine echo "I am here for 5 seconds"
    2. Check: Immediately run docker ps -a.
    3. Result: You will not see the container. It deleted itself after finishing the echo command.
      • Why? Great for one-time scripts or testing commands without filling your disk with garbage.

    Lab: The “Persistent Database” (Data Safety)

    Goal: Prove that data survives even if the container is deleted.

    1. Create a Volume: docker volume create my-db-data
    2. Start Database (MySQL/Postgres) with Volume:docker run -d --name risky-db -e POSTGRES_PASSWORD=mysecret -v my-db-data:/var/lib/postgresql/data postgres
      • -v my-db-data:/var/lib/postgresql/data: Maps the volume to where Postgres stores data.
    3. Write Data: docker exec -it risky-db psql -U postgres -c "CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50));"
    4. Destroy the Container (Simulate a crash): docker rm -f risky-db (The container is gone. Is the data lost?)
    5. Resurrect: Start a new container with the same volume. docker run -d --name safe-db -e POSTGRES_PASSWORD=mysecret -v my-db-data:/var/lib/postgresql/data postgres
    6. Verify Data: docker exec -it safe-db psql -U postgres -c "\dt" (You will still see the ‘users’ table! The data survived inside the Volume).

    Lab: The “Environment Configuration” (12-Factor App Principle)

    Goal: Change application behavior without changing code.

    1. Run Python App: docker run --rm -e MY_NAME="Amit" python:3.9-slim python -c "import os; print('Hello ' + os.environ.get('MY_NAME'))"
    2. Result: Output will be Hello Amit.
    3. Change Config: docker run --rm -e MY_NAME="Priya" python:3.9-slim python -c "import os; print('Hello ' + os.environ.get('MY_NAME'))"
    4. Result: Output will be Hello Priya.
      • Architect Note: This is how we configure apps for Dev, QA, and Prod using the same image.

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Scroll to Top