Docker tips & tricks

here is depicted ship filled with containers on it
Docker tips & tricks

1. one-line command for build & run container at once

    docker-compose up -d --build

-d means in background (daemon) 

2. speedup docker builds

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

8. Doker 3.5+: network section changes
instead networks:
  default:
    external:
      name: ddev_default
should be networks:
  default:
    name: ddev_default
    external: true
reason: it's because of compose v2
alternative solution: just remove the tick mark in settings->eperimental features->use 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

Popular posts from this blog

Открываем порт для сервера Minecraft на роутере mikrotik (команда для терминала в WinBox)

Интересное о Формальдегиде