Skip to content
tezvyn:

Build, tag, and run a container with port mapping

Source: interviewEasyHow cards are made

Summary

core build and run CLI fluency.

Key points

docker build -t my-app:1.0 . to build and tag; docker run -d -p 8080:80 my-app:1.0 to run detached with host:container port mapping.

Watch out for

reversing the port order or forgetting the build context dot.

WHAT THIS TESTS It checks fluency with the two commands every Docker user runs constantly, plus the small but easy-to-reverse details around tagging, build context, and port publishing.

A GOOD ANSWER COVERS To build and tag: docker build -t my-app:1.0 . The -t flag assigns the repository:tag name, and the final dot is the build context, the directory sent to the daemon and the root for COPY paths. To run detached with a port mapping: docker run -d -p 8080:80 my-app:1.0. The -d flag runs it in the background and prints the container ID; -p publishes a port using host:container ordering, so 8080 on the host forwards to 80 inside the container. A strong candidate notes that EXPOSE in the Dockerfile only documents the port; -p is what actually publishes it.

COMMON WRONG ANSWERS Reversing the mapping as -p 80:8080. Forgetting the trailing dot, so the build has no context. Believing EXPOSE alone makes the service reachable from the host. Confusing -d with -it.

LIKELY FOLLOW-UPS What does the build context dot do? How is -p different from EXPOSE? How would you publish to a specific host interface, or let Docker pick a random host port?

ONE CONCRETE EXAMPLE After docker build -t my-app:1.0 ., you run docker run -d -p 8080:80 my-app:1.0. The container starts in the background; visiting http://localhost:8080 reaches the web server listening on port 80 inside the container, and docker ps shows the 0.0.0.0:8080->80/tcp mapping.

Interview question

In docker run -d -p 8080:80 my-app:1.0, what does the 8080:80 specify?

  • a.Container port 8080 forwards to host port 80
  • b.Host port 8080 forwards to container port 80Correct
  • c.Two container ports, 8080 and 80, are both opened
  • d.A health-check retry of 80 attempts on port 8080
Why?

The -p flag uses host:container ordering, so host 8080 maps to container 80; reversing this is the classic mistake and the other options misread the syntax entirely.

Just read this? Test yourself on what you have been reading.

Read the original → docs.docker.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on docker — each one lists the topics its interview covers.

See open roles