0
0
Dockerdevops~10 mins

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

Choose your learning style9 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.