Docker Exercise 5
Docker Exercise 5
Create a new C# Minimal API project (use at least dotnet 8) and use this Program.cs file:
In this exercise you will have to perform a lot of steps without any explanation. Your goal is to find your own conclusions on how data is stored inside and outside containers and how volumes work. It is highly recommended that you document your findings after each step.
Instructions
- Dockerize this application using a
Dockerfile
. You will need these commands:dotnet restore <path to .csproj file>
dotnet build <path to .csproj file> -c Release -o <output path>
dotnet publish <path to .csproj file> -c Release -o <output path> /p:UseAppHost=false
- Create an image using the tag
volumes-exercise
- Start a container using this image using
docker run -d -p 8080:8080 --name volumescontainer volumes-exercise
- Try using the provided API to generate and access files within the container
- Use
docker exec -it volumescontainer bash
to connect to a bash shell inside the container. Analyse which files are generated. - Stop and restart the container using
docker stop volumescontainer
anddocker start volumescontainer
- Analyse if the files are still there.
- Stop and remove the container.
- Start a new container from the image using
docker run -d -p 8080:8080 --rm --name volumescontainer volumes-exercise
- Analyse if the files are still there.
- Stop the container (this should also remove the container since you used the
--rm
flag) - Define a volume inside the container, adding this line to the Dockerfile:
VOLUME ["/app/persistent"]
- Rebuild the image
- Inspect the image and check if the volume is defined
- Start a new container from the image using
docker run -d -p 8080:8080 --rm --name volumescontainer volumes-exercise
- Note: In recent Images provided by Microsoft, the App runs under the user app, yet if you create a volume as described above, the created folder will be owned by the root user. Therefore the application won’t have write access to this folder. To change the ownership of that folder add these lines to the Dockerfile:
- Again use the API and check if the files are created accordingly.
- Use Docker desktop to analyse if a volume has been created.
- Stop the container (thus removing it).
- Check if the volume is still there
- Start a new container and analyse if the files are still there.
- Stop the container (thus removing it).
- Start a new container from the image using
docker run -d -p 8081:8080 -v myvolume:/app/persistent --rm --name volumescontainer volumes-exercise
- Generate data
- Stop and remove the container
- Check if the volume is still there
- Start a new container from the image using
docker run -d -p 8081:8080 -v myvolume:/app/persistent --rm --name volumescontainer volumes-exercise
- Check if the files are still there.