Your Dockerfile already builds an image with Node.js, the shop's packages and its code in /app. But an image only says what is inside it. It must also say how the app starts: EXPOSE notes the port the app listens on, and CMD is the command a container runs when it starts.
Write CMD as a JSON list: each word in double quotes, separated by commas, inside [ ]. Docker then starts the program directly, not through a shell, so the stop signal of docker stop reaches it. That is also why the shop starts with node and not with npm start: npm would be one more program between Docker and the shop. Node.js runs JavaScript, so it runs app.js, the file tsc compiles from the shop's TypeScript. For another team's app, a small Python server on port 5000, it would look like this:
EXPOSE 5000
CMD ["python", "server.py"]
A container has its own network, so its port 8000 is not your computer's port 8000. To reach the app from your computer, docker run -p publishes a port, your computer's port first, then the container's: -p 9000:5000 sends your computer's port 9000 to the container's port 5000.
When your code is done, the check prints:
$ docker build -t shop . The image: Node.js 22, the packages and the shop in /app EXPOSE 8000: the app listens on port 8000 in the container (a note for whoever runs the image, it opens nothing by itself) CMD: when the container starts, it runs node app.js JSON list form: node is the main process of the container, so the stop signal of docker stop reaches it In the container: node runs app.js from /app, and the shop listens on port 8000, the default of its PORT setting $ docker run -p 8000:8000 shop -p 8000:8000: port 8000 of your computer goes to port 8000 of the container http://localhost:8000/ -> port 8000 of the container -> node -> app.js: the shop answers
Your task: add two lines to the dockerfile array: EXPOSE 8000, and a CMD that runs node app.js as a JSON list. Then, below it, store the command that starts a container from the image shop, with port 8000 published, in the string runCommand.
Assignment
Follow the Coding Tutorial to add the EXPOSE and CMD lines to the Dockerfile and write the docker run command.
Hint
The CMD line has double quotes inside, so write that string between single quotes. In -p, your computer's port comes first, then the container's.
Free plan: 30 tutor messages. PRO is unlimited.
