From point A to point B
Point A: you already create, list and remove containers, but your applications live locked away: nobody can reach them from outside and their data disappears when you remove the container. Point B: by the end of this chapter you will publish ports, keep data persistent, pass configuration with environment variables and inspect any container to troubleshoot it.
With this, a container stops being an experiment and starts to look like a real service.
Tags: choosing the exact version
If you run an image without a tag, Docker uses latest. It is convenient, but latest changes over time: what works today may break the day a new version comes out.
docker run redis # same as redis:latest
docker run redis:7.4 # a specific version: the same result today and a year from now
The tags available for each image are listed on its Docker Hub page. In production, always pin the version.
-i and -t: standard input and terminal
By default, a container does not read from the keyboard. Check it with bash, which is interactive by nature:
docker run ubuntu bash # no input: bash ends instantly
docker run -i ubuntu bash # bash reads what you type (try ls), but you see no prompt
docker run -it ubuntu bash # input + terminal: you see the root@...:/# prompt
-i(interactive): keeps standard input (STDIN) open, so the application can read what you type.-t(tty): allocates a pseudo-terminal, so you see messages and the prompt as in a normal console.
You will almost always use them together: -it.
Publishing ports with -p
Each container gets a private IP on an internal Docker network, such as 172.17.0.2. On Linux you can reach it from the host itself, but nobody outside can. To expose an application, you publish a host port and connect it to the container port:
# -p HOST_PORT:CONTAINER_PORT
# mywebapp is your application, listening on port 5000
docker run -d --name webapp-1 -p 80:5000 mywebapp
docker run -d --name webapp-2 -p 8001:5000 mywebapp
The first number is the host port; the second, the container port. Diagram: The House of Routing.
- You can repeat the container port: several copies of the same application can listen on 5000, each published on a different host port.
- You cannot repeat the host port: if port 80 is already in use, Docker replies
port is already allocated.
-p 127.0.0.1:8080:80.Persistent data with -v
Everything a container writes lives in its own layer. If you remove it, that data goes with it. For a database, that is unacceptable.
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=changeme mysql:8.4
docker rm -f mysql # ...and every database is gone
The fix is to mount a host folder on the path where the application stores its data. MySQL writes to /var/lib/mysql:
# -v HOST_PATH:CONTAINER_PATH
docker run -d --name mysql \
-e MYSQL_ROOT_PASSWORD=changeme \
-v /opt/datadir:/var/lib/mysql \
mysql:8.4
The files live on the host; the container only uses them. Diagram: The House of Routing.
Now you can remove the container and create another with the same folder: the data is still there. This is called a bind mount. If the folder does not exist, Docker creates it.
docker volume create), which do not depend on a specific host path. You will see them in the storage chapter.Configuring with environment variables: -e
Many images are configured with environment variables instead of edited files. MySQL is a good example: MYSQL_ROOT_PASSWORD is mandatory. Without it, the container stops with an error as soon as it starts.
docker run -d -e MYSQL_ROOT_PASSWORD=changeme mysql:8.4
docker run -d -e APP_COLOR=blue mywebapp # your application reads APP_COLOR
That is why a plain docker run mysql is not enough: each image's Docker Hub page tells you which variables it needs.
-e is visible in docker inspect and in your shell history. It is fine for practice; in production, secret management is used.Troubleshooting: docker inspect and docker logs
docker inspect: the full spec sheet
It returns, in JSON, everything Docker knows about the container: image, state, variables, mounts, network and ports.
docker inspect webapp-1
docker inspect webapp-1 | grep IPAddress
docker inspect -f '{{.NetworkSettings.IPAddress}}' webapp-1 # just the IP
docker logs: what the application says
It shows what the main process writes to its output. It is the first thing to check when a container will not start or exits with an error.
docker logs mysql # the whole log
docker logs -f webapp-1 # live, like tail -f
docker logs --tail 20 mysql # only the last 20 lines
Lab: Jenkins with a port, a volume and its own name
Put it all together in a real service. Jenkins listens on the container's port 8080 and stores its configuration in /var/jenkins_home.
On Linux or macOS
mkdir -p $HOME/jenkins-data
docker run -d --name myjenkins01 \
-p 9090:8080 \
-v $HOME/jenkins-data:/var/jenkins_home \
jenkins/jenkins:lts
On Windows (PowerShell)
docker run -d --name myjenkins01 `
-p 9090:8080 `
-v C:\Users\YOUR_USER\jenkins-data:/var/jenkins_home `
jenkins/jenkins:lts
Then:
docker logs myjenkins01 # copy the initial password
docker inspect -f '{{json .Mounts}}' myjenkins01 # check the mount
docker rm -f myjenkins01 # remove it...
# ...run the same docker run again: your configuration is still there
Open http://localhost:9090. If Jenkins does not ask you to set it up again after you recreate the container, the volume works.
/var/jenkins_home: if you mount a folder on another path, Jenkins does not use it. And use a tag that exists on Docker Hub, such as lts.docker run cheat sheet
| Option | What it is for | Example |
|---|---|---|
IMAGE:TAG | Choose the exact version | redis:7.4 |
-i / -t | Read from the keyboard / show a terminal | -it |
-p | Publish a port: host to container | -p 8080:80 |
-v | Mount a host folder | -v /opt/datadir:/var/lib/mysql |
-e | Pass an environment variable | -e MYSQL_ROOT_PASSWORD=... |
--name / -d | Own name / background | --name web -d |
docker inspect | Full spec sheet in JSON | -f '{{.NetworkSettings.IPAddress}}' |
docker logs | Main process output | -f, --tail 20 |
Challenge
Answer before you open the solution. Getting it wrong here is the cheapest way to learn.
1. In -p 8080:80, which one is the host port?
8080. The first number is always the host; the second, the container.
2. You want two copies of an application that listens on port 5000. Can you publish both on host port 80?
No. A host port can point to only one container. Use different ports, for example -p 80:5000 and -p 8001:5000.
3. You run docker run -d mysql and the container stops after a few seconds. What do you check first, and what is missing?
Check docker logs. The mandatory variable MYSQL_ROOT_PASSWORD is missing; pass it with -e.
4. You remove a MySQL container that had -v /opt/datadir:/var/lib/mysql. What happens to the data?
It stays in /opt/datadir on the host. A new container with the same mount finds it again.
What you take away
- Pin the image tag:
latestchanges without warning. -iopens standard input;-tgives you a terminal. Together:-it.-p HOST:CONTAINER: the host port cannot be repeated.-v HOST:CONTAINERmoves data out of the container so it survives.-econfigures the application; check on Docker Hub which variables each image needs.- When something fails:
docker logsfirst,docker inspectnext.
In the next chapter you move from using images to building your own: the Dockerfile.
Official resources
- docker container rundocs.docker.com
- Port publishing and mappingdocs.docker.com
- Bind mountsdocs.docker.com
- docker inspectdocs.docker.com
- docker container logsdocs.docker.com
- mysql (official image)hub.docker.com
- jenkins/jenkinshub.docker.com
Source and credits. Chapter written from Jesús Espinoza's study notes on KodeKloud's Docker for Absolute Beginners course (December 2024). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details were checked against the official Docker documentation.