Skip to main content

Docker Daily Workflow Lab

· 3 min read

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.

  • Create a file: Create a file named index.html on your desktop.

  • Content: Hello from DevSecOpsGuru!

  • Start Nginx: docker run -d --name my-website -p 8080:80 nginx

  • Check before change: Open browser http://localhost:8080. You see the default "Welcome to nginx".

  • 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).

  • Check after change: Refresh your browser. It now says "Hello from DevSecOpsGuru!".

  • Cleanup: docker rm -f my-website

Lab: The "Self-Destruct" Container (Utility Tasks)​

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

  • Run with --rm: docker run --rm -it alpine echo "I am here for 5 seconds"

  • Check: Immediately run docker ps -a.

  • 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.

  • Create a Volume: docker volume create my-db-data

  • 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.

  • Write Data: docker exec -it risky-db psql -U postgres -c "CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50));"

  • Destroy the Container (Simulate a crash): docker rm -f risky-db (The container is gone. Is the data lost?)

  • 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

  • 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.

  • 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'))"

  • Result: Output will be Hello Amit.

  • Change Config: docker run --rm -e MY_NAME="Priya" python:3.9-slim python -c "import os; print('Hello ' + os.environ.get('MY_NAME'))"

  • Result: Output will be Hello Priya.

  • Architect Note: This is how we configure apps for Dev, QA, and Prod using the same image.