DOCKER · DOCKER FROM SCRATCH · CHAPTER 3

Docker Run

Ports, volumes and environment variables: from test container to real service.

6 min readCourse: Docker from ScratchLevel: beginner

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
Port mapping: host ports to container ports A user reaches the Docker host at 192.168.1.10. Host port 80 maps to port 5000 of the first webapp container, and host port 8001 maps to port 5000 of a second copy. Each container has a private IP on the internal bridge network. Two containers cannot use the same host port. -p HOST_PORT:CONTAINER_PORT Browser:80 · :8001 Docker host · 192.168.1.10 :80 :8001 :80 ✗ webapp-1172.17.0.2 · port 5000-p 80:5000 webapp-2172.17.0.3 · port 5000-p 8001:5000 port is already allocated one host port, one container

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.
Security. A published port is reachable from outside the host. If you only need it on your machine, publish it on the local interface: -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
Volume mapping keeps data outside the container The mysql container writes to /var/lib/mysql, which is mounted from the host folder /opt/datadir. When the container is removed, the data stays on the host, and a new container mounted on the same folder finds it again. -v /opt/datadir:/var/lib/mysql container: mysql /var/lib/mysqlwhere MySQL writes Docker host /opt/datadirreal files on disk mount docker rm mysql data is still here

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.

A preview. Docker also offers volumes that it manages itself (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.

Careful with passwords. Whatever you pass with -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.

Two corrections to keep in mind. Jenkins's data path inside the container is /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

OptionWhat it is forExample
IMAGE:TAGChoose the exact versionredis:7.4
-i / -tRead from the keyboard / show a terminal-it
-pPublish a port: host to container-p 8080:80
-vMount a host folder-v /opt/datadir:/var/lib/mysql
-ePass an environment variable-e MYSQL_ROOT_PASSWORD=...
--name / -dOwn name / background--name web -d
docker inspectFull spec sheet in JSON-f '{{.NetworkSettings.IPAddress}}'
docker logsMain 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: latest changes without warning.
  • -i opens standard input; -t gives you a terminal. Together: -it.
  • -p HOST:CONTAINER: the host port cannot be repeated.
  • -v HOST:CONTAINER moves data out of the container so it survives.
  • -e configures the application; check on Docker Hub which variables each image needs.
  • When something fails: docker logs first, docker inspect next.

In the next chapter you move from using images to building your own: the Dockerfile.

Official resources

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.

Navigate