Your Dockerfile already builds an image with Go, the shop's modules and its code, compiled into the program /app/shop. 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. A program in the folder you work in is run with ./ in front: a bare name is looked for only in the folders of PATH. For another app, a small server named api on port 5000, it would look like this:
EXPOSE 5000
CMD ["./api", "serve"]
A container has its own network. Inside it, 127.0.0.1 is the container itself, so a server in a container must listen on 0.0.0.0, every address; in Go, an address like ":5000" does just that. To reach it 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: Go 1.23, the modules and the shop, compiled into /app/shop 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 ./shop JSON list form: ./shop is the main process of the container, so the stop signal of docker stop reaches it In the container: ./shop starts the program go build made in /app, and its http.Server listens on 0.0.0.0:8000 $ 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 -> ./shop -> newApp(): the shop answers
Your task: add two lines inside the backticks of dockerfile: EXPOSE 8000, and a CMD that runs the program ./shop as a JSON list. Then, below it, declare the string variable runCommand with the command that starts a container from the image shop, publishing port 8000.
Assignment
Follow the Coding Tutorial to add the EXPOSE and CMD lines to the Dockerfile and write the docker run command.
Hint
Inside the backticks, the double quotes of the CMD line need no escaping. The check reads runCommand, so the program compiles only once you declare it. In -p, your computer's port comes first, then the container's.
Free plan: 30 tutor messages. PRO is unlimited.
