Hey everyone! These are the solutions for the quiz of the first week. Thank you for participating!
Q1: What is the main advantage of containers over virtual machines
A1: They are more lightweight by sharing the hostβs kernel
Q2: Given this Dockerfile, whatβs wrong with it?
FROM golang
WORKDIR /app
RUN go build -o main main.go
COPY . .
CMD ["./main"]
A2: Instructions are being run top to bottom. The COPY
instruction is run after the RUN
instruction, so the code to build the application is not available in the container. The build will fail.
Q3: Which command would correctly run a container and map port 3000 inside the container to port 8080 on your host machine?
A3: docker run -p 8080:3000 myapp
That was also a trick question. --port
is wrong, because -p is the shorthand for --publish
.
Q4: When you modify a file in a running container, the changes are automatically saved to the Docker image.
A4: False. The changes are not saved to the Docker image. They are saved to a temporary layer that is discarded after the container is stopped.
Q5: Which Dockerfile instruction does NOT create a new layer?
A5: CMD
Q6: Match the volume type with its use case
A6:
- Named Volumes: Best for persistent data that needs to be managed by Docker
- Bind Mounts: Best for development when you need to see real-time code changes
- Anonymous: Temporary data that only exists while the container runs
Q7: Write a Docker command that would 1. Create a container from image βmyappβ 2. Mount a named volume called βdataβ to β/app/dataβ in the container 3. Run in detached mode 4. Map port 8080 to 8080
A7: docker run -d -v data:/app/data -p 8080:8080 myapp
If you have any questions, feel free to reach out to me on Twitter or by email at [email protected].
Until then, happy coding! π³π
Jonas