Docker tips & tricks
Docker tips & tricks
1. one-line command for build & run container at once
docker-compose up -d --build
-d means in background (daemon)
3. Rebuild containers using docker compose when dockerfile changes
docker-compose up --force-recreate
4. If I have a bad deploy this may help
docker-compose build --no-cache
5. Remove all images
docker-compose down --rmi ("local"|"all")6. Automatically update services with file watch in Docker Compose (experimental, Docker Desktop 4.24.0 said that this feature is now open for all)
docker-compos up --watch
7. Removing containers with the data (be careful, you will lose all your data for the containers that are in docker-compose)
docker-compose down -v
instead networks:
default: external: name: ddev_defaultshould be networks:
default: name: ddev_default external: truereason: it's because of compose v2
Docker Run Flags:
Docker Compose Dry Run is now feature complete.
You can now safely preview any Compose command with the --dry-run flag experimental (it doesn't work for me now, looking for an issue, let you know when it will work)
-w, --workdir string --> Working directory inside the container
docker-compose up -d --wait --> option tells docker-compose to wait for a certain condition before starting the containers. This can be useful if you need to ensure that some dependencies are ready before running your service. For example, you can wait for a fixed amount of seconds, for a TCP port to be open on a target image, or for a file or directory to be present on the local filesystem.
Docker compose (inside):
When you describe docker-compose file you frequently need to use variables there. You probably should use variables Interpolation, it has non-so-obvious syntax but is well described here in detail:
Compose files use a Bash-like syntax ${VARIABLE}
Both $VARIABLE and ${VARIABLE} syntax are supported.
Default values can be defined inline using typical shell syntax: ${VARIABLE:-default} evaluates to default if VARIABLE is unset or empty in the environment.
It is usable only for values, not for keys
example:
environment:
- MY_ARG=${MY_ARG:-777}
so
default
value here will be
777
if
MY_ARG is
unset or empty in the environment
Comments