Practice: Docker essentials
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Most Docker trouble in the first weeks comes from a handful of slips in one docker run line: the two sides of -p swapped, a missing -v, an unquoted password, a name that is already taken, a tag that does not exist. Each has a clear message and a one-line fix.
Mistake 1: -p the wrong way round, and a name already in use
Swap the two sides of -p and MySQL still starts, but nothing answers. Below: the broken command, the fix, and a name conflict on a second run.
Example 1 · Swapped -p, then a name conflict
Runs in: a terminal with Docker installed
# Broken: the two sides of -p are swappeddocker run -d --name shoplite-db \ -e MYSQL_ROOT_PASSWORD='root-dev-pass' -e MYSQL_DATABASE=shoplite \ -e MYSQL_USER=shoplite -e MYSQL_PASSWORD='shoplite-dev-pass' \ -v shoplite-data:/var/lib/mysql -p 3306:3307 mysql:8.4mysql -h 127.0.0.1 -P 3307 -u shoplite -p# Fixed: remove the old container (the volume stays), host port firstdocker rm -f shoplite-dbdocker run -d --name shoplite-db ...same -e and -v flags... -p 3307:3306 mysql:8.4# Running the fixed command a second timedocker run -d --name shoplite-db ...same flags... -p 3307:3306 mysql:8.43b8e5f0a1c2d... Enter password: ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3307' (10061) shoplite-db 9d4a7c6b5e3f... docker: Error response from daemon: Conflict. The container name "/shoplite-db" is already in use by container "9d4a7c6b5e3f...". You have to remove (or rename) that container to be able to reuse that name. See 'docker run --help'.
What this code does
- With
-p 3306:3307the container starts, but nothing is published on 3307, so the client gets "Can't connect" (10061 on Windows, 111 on Linux). docker rm -fremoves the bad container; the volume and its data stay, so the fixeddocker runfinds the same database.- Running the same
docker runtwice fails with a Conflict: the nameshoplite-dbalready belongs to the container created a moment ago.
-p is always host:container. For MySQL the right side must be 3306, the port mysqld listens on inside. -p 3306:3307 starts without any error, but it forwards to a container port where nothing listens, and 3307 on your laptop is not published at all.
A name conflict happens when a container with that name still exists, even a stopped one. Docker refuses to create a second one. Remove the old container with docker rm -f shoplite-db: the data in the named volume shoplite-data stays, so nothing is lost.
Fix the error
Nothing answers on 3307
The client connects to 127.0.0.1:3307. Pick the replacement for line 2.
The container runs, but the connection fails.
Error
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3307' (111)
Mistake 2: unquoted values and a tag that does not exist
The shell reads your command before Docker does. Spaces and a dollar sign in a password, and a tag that does not exist, each break the run in a different way.
Example 2 · Spaces, $ and a wrong tag
Runs in: a terminal with Docker installed
# Broken 1: a password with spaces and no quotesdocker run -d --name shoplite-db -e MYSQL_ROOT_PASSWORD=shop lite 2026 mysql:8.4# Broken 2: double quotes let bash expand $2 inside the passworddocker run --rm mysql:8.4 echo "upi$2026"# Broken 3: a tag that does not existdocker run -d --name shoplite-db ... mysql:8.4.99# Fixed: single quotes keep every character; pin a tag that existsdocker run --rm mysql:8.4 echo 'upi$2026'docker run -d --name shoplite-db -e MYSQL_ROOT_PASSWORD='shop lite 2026' ... mysql:8.4Unable to find image 'lite:latest' locally docker: Error response from daemon: pull access denied for lite, repository does not exist or may require 'docker login': denied: requested access to the resource is denied. See 'docker run --help'. upi026 Unable to find image 'mysql:8.4.99' locally docker: Error response from daemon: manifest for mysql:8.4.99 not found: manifest unknown: manifest unknown. See 'docker run --help'. upi$2026 6e2d9b8c4a1f...
What this code does
- The unquoted spaces turned
liteinto the image name, so Docker tried to pull an image calledliteand failed. echoinside themysql:8.4image shows what the container would receive:upi026with double quotes,upi$2026with single quotes.- "manifest unknown" means the registry has no image with that tag; nothing is wrong with your network or login.
In -e MYSQL_ROOT_PASSWORD=shop lite 2026 mysql:8.4, Docker receives only shop as the password, and takes lite as the image name. Inside double quotes the shell also replaces $2 with an (empty) variable, so "upi$2026" becomes upi026.
The fix for both: put such values in single quotes, which keep every character, or move them into an env file. A wrong tag such as mysql:8.4.99 fails with "manifest unknown"; check the tag on the image's page and pin one that exists, such as mysql:8.4.
Find the bug
Click the broken line
Each docker run fails or sets the wrong password. Click the guilty line.
Docker tries to pull an image called lite.
Round 1 of 3Mistake 3: no volume, and what rm really deletes
Without -v, MySQL's data lands in a throwaway volume. This script shows what survives stop, rm, rm -v and volume rm.
Example 3 · What survives stop, rm, rm -v and volume rm
Runs in: a terminal with Docker installed
# put a marker in the databasedocker exec shoplite-db mysql -u root -p'root-dev-pass' -e "CREATE DATABASE survival_test;"docker stop shoplite-db && docker start shoplite-db # 1. stop + startdocker rm -f shoplite-db # 2. remove the containerdocker run -d --name shoplite-db ...same flags, same -v shoplite-data:/var/lib/mysql...docker exec shoplite-db mysql -u root -p'root-dev-pass' -e "SHOW DATABASES LIKE 'survival%';"docker rm -f -v shoplite-db # 3. rm -v: anonymous volumes onlydocker volume ls --filter name=shoplite-datadocker volume rm shoplite-data # 4. now the data is really gonemysql: [Warning] Using a password on the command line interface can be insecure. shoplite-db shoplite-db shoplite-db 8f1c2b3a4d5e... mysql: [Warning] Using a password on the command line interface can be insecure. Database (survival%) survival_test shoplite-db DRIVER VOLUME NAME local shoplite-data shoplite-data
What this code does
- The marker database
survival_testis still there after stop, start, rm and a newdocker run, because it lives inshoplite-data, not in the container. docker rm -f -vremoved the container butdocker volume lsstill listsshoplite-data:-vdeletes anonymous volumes only.docker volume rm shoplite-datais the one step that deletes the data; it works here because no container uses the volume any more.
Without -v shoplite-data:/var/lib/mysql, MySQL still writes to a volume: the image declares one, so Docker creates an anonymous volume with a long random name. The next container gets a fresh one, and the app sees an empty database even though the old files still sit somewhere in docker volume ls.
With a named volume, docker stop, docker start and docker rm never touch the data. docker rm -v deletes only anonymous volumes of that container, never a named one. Only docker volume rm shoplite-data (or a prune of unused volumes) deletes the data, and Docker refuses while a container still uses the volume.
Predict the output
Is the data still there?
shoplite-db uses the named volume shoplite-data. Pick the result.
Round 1 of 3
Stop, then start.
Reading docker ps -a: why did each container stop?
Six containers, six stories. Read STATUS first, then the logs, then OOMKilled.
Example 4 · Six containers, six stories
Runs in: a terminal with Docker installed
docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"docker logs --tail 5 old-dbdocker inspect shoplite-api --format "oom={{.State.OOMKilled}}"NAMES IMAGE STATUS
shoplite-db mysql:8.4 Up 3 hours (healthy)
shoplite-api shoplite-api:1.2.0 Exited (137) 12 minutes ago
hello-test shoplite-hello:0.1 Exited (143) 2 hours ago
old-db mysql:8.4 Exited (1) 3 days ago
jshell-tmp eclipse-temurin:25-jdk Exited (0) 5 hours ago
init-try mysql:8.4 Created
2026-10-13 10:02:11+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following as an environment variable:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
oom=trueWhat this code does
shoplite-dbis running and healthy;jshell-tmpfinished normally (0);hello-testwas stopped withdocker stop(143);init-trywas created but never started.old-dbexited with 1, and its log says why: it was started withoutMYSQL_ROOT_PASSWORDon an empty volume.shoplite-apishows 137 andoom=true: the kernel killed it for passing its memory limit, so the fix is more memory or a smaller heap, not a code change.
docker ps -a lists stopped containers too, and the STATUS column is the first clue. Up ... (healthy) is running and passing its health check; Created was never started; Exited (code) gives the exit code.
Read the code with the rules from the Shell into a container lesson: 0 is a normal finish, 1 is an app error (read docker logs), 143 is SIGTERM from docker stop, and 137 is SIGKILL, where docker inspect --format "{{.State.OOMKilled}}" tells you whether memory was the reason.
Finish up
Mark complete & continue
Next: Project: ShopLite on a MySQL container