Difference between image and container
You need to feel difference between docker "image" and "container".
Make sure you know understand the flow:
You have Dockerfile to build image you need to run containers.
Docker file build contexts
You need to understand the context of each line of dockerfile
- what is the current build context (directory on agent)
- what is the current working directory on image
- what is the current user
- what is the current stage (for multistage build)
- how it affects layer cache
Agent is a machine where docker build
command started. docker build
command has required parameter "build context" which is folder on agent machine.
For example, the COPY <src> <dest>
command <src>
parameter is path relative to the current build context folder.
<dest>
parameter is path relative to the current working directory on image file system.
For example this Dockerfile has the following file systems:
WORKDIR /src
COPY . .
Build context (agent) | Work directory (image) |
- docker-concepts-you-should-know - src - WebApi - WebApi.csproj - Program.cs - Dockerfile |
- src - src - WebApi - WebApi.csproj - Program.cs - Dockerfile |
Build time and Runtime
You are able to run apps while docker build
building image with RUN
statement. And you can run your apps in running container with ENTRYPOINT
or with docker exec
. Normally you might need to run your development tools build time, but your service or utils (like database migration) in runtime.
Build time executions are focused on image content, for example you might want to build binaries from source code. Build time you don't have your private network and volumes, so you can't have access to database.
Layout caching
Each statement in docker file produces image cache layer on docker host machine.
Cache layers could help application build time, for example
dotnet restore
result will be cached if files copied to the docker image are the same. That is why default Dockerfile generated by visual studio copies csproj files first, and other files only after dotnet restore.
WORKDIR /src
COPY ["src/webapi/webapi.csproj", "src/webapi/"]
RUN dotnet restore "src/webapi/webapi.csproj"
COPY . .
Andrew Lock wrote excellent article about this optimization.
https://andrewlock.net/optimising-asp-net-core-apps-in-docker-avoiding-manually-copying-csproj-files/
Docker-compose
Docker compose calls Docker under the hood. But docker-compose has it's own behavior and some commands could work differently.
Entrypoint
You can use shell script as Entrypoint. If you have windows developer environment you need to remember:
- Set lr for .sh files in .gitattributes
*.sh text eol=lf
- Set +x for .sh file in git metadata
git update-index --chmod=+x foo.sh
or you can use TortoiseGit:
Top comments (0)