Välja ett startkommando för din Docker-image

Introduktion till Docker

Tim Sangster

Software Engineer @ DataCamp

Vad är ett startkommando?

hello-world-imagen skriver ut text och avslutas sedan.

docker run hello-world
Hello from Docker!

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon created a new container from the hello-world image which runs the
    executable that produces the output you are currently reading.
 3. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
Introduktion till Docker

Vad är ett startkommando?

En image med python kan starta Python vid uppstart.

docker run python3-sandbox
Python 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
...
....
>>> exit()
repl@host:/#
Introduktion till Docker

Köra ett shell-kommando vid uppstart

CMD <shell-command>

CMD-instruktionen:

  • Körs när imagen startas.
  • Ökar inte imagens storlek.
  • Lägger inte till tid vid bygget.
  • Om flera finns angivna gäller endast den sista.
Introduktion till Docker

Vanliga användningsfall

Starta ett program som kör ett arbetsflöde eller tar emot externa anslutningar.

CMD python3 my_pipeline.py
CMD postgres

Starta ett skript som i sin tur startar flera program

CMD start.sh
CMD python3 start_pipeline.py
Introduktion till Docker

När avslutas den?

$$

  • hello-world-imagen -> Efter utskrift av text
  • En databasimage -> När databasen avslutas

$$

En mer generell image behöver ett mer generellt startkommando.

  • En Ubuntu-image -> När shell-sessionen stängs
Introduktion till Docker

Åsidosätta standardstartkommandot

Starta en image

docker run <image>

Starta en image med ett anpassat startkommando

docker run <image> <shell-command>

Starta en image interaktivt med ett anpassat startkommando

docker run -it <image> <shell-command>
docker run -it ubuntu bash
Introduktion till Docker

Sammanfattning

Användning Dockerfile-instruktion
Lägg till ett shell-kommando som körs när en container startas från imagen. CMD <shell-command>

Användning Shell-kommando
Åsidosätt CMD i imagen docker run <image> <shell-command>
Åsidosätt CMD i imagen och kör interaktivt docker run -it <image> <shell-command>
Introduktion till Docker

Nu kör vi en övning!

Introduktion till Docker

Preparing Video For Download...