Bird
Raised Fist0
Dockerdevops~10 mins

Database containers for local development in Docker - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Database containers for local development
Write Docker run command for DB container
↓
Docker pulls DB image if not present
↓
Docker creates and starts container
↓
DB container runs locally
↓
Connect app to DB container via port
↓
Develop and test locally with DB
This flow shows how to start a database container locally using Docker, then connect your app to it for development and testing.
Execution Sample
Docker
docker run --name local-postgres -e POSTGRES_PASSWORD=pass123 -p 5432:5432 -d postgres
Starts a PostgreSQL database container locally with password and port mapping.
Process Table
StepActionDocker OutputContainer StateNotes
1Run docker command to start DB containerUnable to find image 'postgres:latest' locally latest: Pulling from library/postgres Digest: sha256:... Status: Downloaded newer image for postgres:latest Container created but startingDocker pulls image because not found locally
2Docker creates and starts containera1b2c3d4e5f6g7h8i9j0RunningContainer ID shown, DB server starting inside container
3Container listens on port 5432No outputRunningPort 5432 on host mapped to container's 5432
4Connect app to DB at localhost:5432No outputRunningApp can now connect to DB using password 'pass123'
5Stop container when donelocal-postgresStoppedContainer stopped, DB no longer accessible
6ExitN/AN/ADevelopment session ends
💡 Container stopped or user ends session, DB container no longer running
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5
Container StateNot createdImage pulled, container createdRunningRunning and listening on portStopped
DB AccessibilityNoNoYesYesNo
Key Moments - 3 Insights
Why does Docker pull the image before creating the container?
Docker needs the database image locally to create a container. If it's not found, it downloads it first as shown in execution_table step 1.
How does the app connect to the database inside the container?
The port mapping (-p 5432:5432) makes the container's DB port accessible on localhost:5432, so the app connects there as in step 4.
What happens if you stop the container?
Stopping the container stops the database server, so the app can no longer connect, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the container state after step 2?
ACreated but not running
BStopped
CRunning
DNot created
💡 Hint
Check the 'Container State' column in row for step 2.
At which step does the database become accessible to the app?
AStep 1
BStep 3
CStep 5
DStep 6
💡 Hint
Look for 'DB Accessibility' becoming 'Yes' in variable_tracker after steps.
If you omit the -p 5432:5432 option, what changes in the execution?
AApp cannot connect to DB on localhost
BContainer will not start
CDB password will be ignored
DImage will not be pulled
💡 Hint
Port mapping controls access from host to container; see step 3 notes.
Concept Snapshot
Use 'docker run' with DB image to start a local DB container.
Map container port to host port with '-p' for app access.
Set environment variables like POSTGRES_PASSWORD for DB setup.
Container runs isolated but accessible via mapped ports.
Stop container to end local DB session.
Full Transcript
This visual execution shows how to use Docker to run a database container locally for development. First, you run a docker command that pulls the database image if not present. Then Docker creates and starts the container, which runs the database server inside. The container port is mapped to the host port so your app can connect to the database at localhost. You can develop and test your app using this local database. When done, stopping the container stops the database server and closes access. Key points include image pulling, container creation, port mapping, and container lifecycle.

Practice

(1/5)
1. What is the main benefit of using a database container for local development?
easy
A. You can run a database without installing it on your computer
B. It makes your computer run faster
C. It automatically writes your application code
D. It replaces the need for a database in production

Solution

  1. Step 1: Understand what a database container does

    A database container runs a database inside a Docker container, so you don't need to install the database software directly on your computer.
  2. Step 2: Identify the main benefit for local development

    This means you can start and stop the database easily without changing your computer setup.
  3. Final Answer:

    You can run a database without installing it on your computer -> Option A
  4. Quick Check:

    Database container = no local install needed [OK]
Hint: Containers run databases isolated, no install needed [OK]
Common Mistakes:
  • Thinking containers speed up the computer
  • Confusing containers with code generators
  • Believing containers replace production databases
2. Which Docker command correctly starts a PostgreSQL database container exposing port 5432?
easy
A. docker build -p 5432 postgres
B. docker start -p 5432 postgres
C. docker create postgres -p 5432:5432
D. docker run -d -p 5432:5432 postgres

Solution

  1. Step 1: Identify the correct command to run a container with port mapping

    The command to start a container and map ports is docker run -d -p hostPort:containerPort image. Here, -d runs it in the background.
  2. Step 2: Check each option

    docker run -d -p 5432:5432 postgres uses docker run -d -p 5432:5432 postgres, which is correct. docker start -p 5432 postgres uses docker start which does not support -p. docker create postgres -p 5432:5432 uses docker create which creates but does not start the container. docker build -p 5432 postgres uses docker build which builds images, not runs containers.
  3. Final Answer:

    docker run -d -p 5432:5432 postgres -> Option D
  4. Quick Check:

    Run + port mapping = docker run -d -p [OK]
Hint: Use 'docker run -d -p host:container image' to start with ports [OK]
Common Mistakes:
  • Using 'docker start' with port flags
  • Confusing 'docker create' with 'docker run'
  • Using 'docker build' to run containers
3. What will be the output of the command docker ps after running docker run -d -p 3306:3306 --name mysql-db mysql?
medium
A. No containers running
B. An error saying port 3306 is already in use
C. A running container named mysql-db with port 3306 mapped
D. A stopped container named mysql-db

Solution

  1. Step 1: Understand what the command does

    The command runs a MySQL container in detached mode (-d), names it mysql-db, and maps port 3306 on the host to port 3306 in the container.
  2. Step 2: Predict the output of docker ps

    docker ps lists running containers. Since the container runs detached and should start successfully, it will show a running container named mysql-db with port 3306 mapped.
  3. Final Answer:

    A running container named mysql-db with port 3306 mapped -> Option C
  4. Quick Check:

    Detached run + docker ps = running container shown [OK]
Hint: Detached run shows container in 'docker ps' list [OK]
Common Mistakes:
  • Assuming 'docker ps' shows stopped containers
  • Expecting errors without port conflicts
  • Confusing 'docker ps' with 'docker ps -a'
4. You tried to start a PostgreSQL container with docker run -d -p 5432:5432 postgres but got an error that port 5432 is already in use. What should you do to fix this?
medium
A. Stop the service using port 5432 or map to a different host port
B. Change the container port to 5433
C. Remove the postgres image and try again
D. Use docker build instead of docker run

Solution

  1. Step 1: Understand the port conflict error

    The error means something on your computer is already using port 5432, so Docker cannot map the container's port to it.
  2. Step 2: Fix the conflict by stopping the service or changing host port

    You can stop the program using port 5432 or map the container port to a different host port like 5433 using -p 5433:5432.
  3. Final Answer:

    Stop the service using port 5432 or map to a different host port -> Option A
  4. Quick Check:

    Port conflict fix = stop service or change host port [OK]
Hint: Change host port or stop conflicting service to fix port error [OK]
Common Mistakes:
  • Changing container port instead of host port
  • Removing image does not fix port conflicts
  • Using 'docker build' instead of 'docker run'
5. You want to reset your local MySQL database container to start fresh without losing your data volume. Which Docker command sequence is correct?
hard
A. docker rm mysql-db && docker run -d -p 3306:3306 --name mysql-db mysql
B. docker stop mysql-db && docker rm mysql-db && docker run -d -p 3306:3306 --name mysql-db mysql
C. docker restart mysql-db && docker run mysql
D. docker stop mysql-db && docker run -d -p 3306:3306 --name mysql-db mysql

Solution

  1. Step 1: Stop and remove the existing container

    To reset the container, first stop it with docker stop mysql-db and then remove it with docker rm mysql-db. This removes the container but not the data volume.
  2. Step 2: Run a new container with the same name and port mapping

    Start a new container with docker run -d -p 3306:3306 --name mysql-db mysql to get a fresh instance connected to the existing data volume.
  3. Final Answer:

    docker stop mysql-db && docker rm mysql-db && docker run -d -p 3306:3306 --name mysql-db mysql -> Option B
  4. Quick Check:

    Stop + remove + run = reset container without losing volume [OK]
Hint: Stop and remove container before rerunning to reset it [OK]
Common Mistakes:
  • Restarting container does not reset it
  • Removing container without stopping first
  • Running new container without removing old one
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard