Bird
Raised Fist0
Dockerdevops~5 mins

Database containers for local development in Docker - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a database container in local development?
A database container is a lightweight, isolated environment running a database server using container technology like Docker. It helps developers run databases locally without installing them directly on their machines.
Click to reveal answer
beginner
Why use database containers instead of installing databases directly?
Database containers are easy to start, stop, and reset. They avoid conflicts with other software and keep your system clean. You can also share the exact database setup with your team.
Click to reveal answer
intermediate
Which Docker command starts a PostgreSQL database container named 'mydb' with password 'mypassword'?
docker run --name mydb -e POSTGRES_PASSWORD=mypassword -p 5432:5432 -d postgres
Click to reveal answer
intermediate
How do you persist data in a database container so it is not lost when the container stops?
Use Docker volumes to store database files outside the container. For example, add '-v mydbdata:/var/lib/postgresql/data' to the docker run command to keep data safe.
Click to reveal answer
beginner
What port mapping is needed to access a MySQL container running on port 3306 from your local machine?
You map the container's port 3306 to a local port, usually 3306, using '-p 3306:3306' in the docker run command. This lets your local apps connect to the database container.
Click to reveal answer
What is the main benefit of using a database container for local development?
AIt removes the need for a database password
BIt makes the database run faster than on a server
CIt isolates the database environment from your system
DIt automatically backs up your data to the cloud
Which Docker option sets environment variables like database passwords?
A-e
B-p
C-v
D--name
How do you keep database data safe when restarting a container?
AData is always safe inside the container
BRestart the container with the --reset flag
CUse the --no-delete option
DUse Docker volumes to store data outside the container
What does the '-p 5432:5432' option do in a Docker run command for PostgreSQL?
ALimits the container memory to 5432MB
BMaps local port 5432 to container port 5432
CNames the container 5432
DSets the password to 5432
Which command starts a MySQL container named 'mysql-dev' with root password 'rootpass'?
Adocker run --name mysql-dev -e MYSQL_ROOT_PASSWORD=rootpass -p 3306:3306 -d mysql
Bdocker start mysql-dev -p 3306
Cdocker build mysql-dev --password rootpass
Ddocker create mysql-dev -e MYSQL_PASSWORD=rootpass
Explain how to set up a PostgreSQL database container for local development including data persistence.
Think about the options -e, -p, and -v in docker run.
You got /4 concepts.
    Describe the advantages of using database containers over installing databases directly on your computer.
    Consider how containers keep things separate and easy to manage.
    You got /4 concepts.

      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