Commandes Linux : docker image build

DOCKER IMAGE BUILD

Commande : docker image build
Usage:	docker image build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --force-rm                Always remove intermediate containers
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build (default "default")
      --no-cache                Do not use cache when building the image
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
      --rm                      Remove intermediate containers after a successful build (default true)
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
  -t, --tag list                Name and optionally a tag in the 'name:tag' format
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])

 

Construction d’une image à partir du Dockerfile :

Généralement le Dockerfile se trouve à la racine du contexte (tout comme le .dockerignore).

$ docker image build .

Équivaut à :
$ docker build .
ex :
Avec dockerfiles/Dockerfile

FROM busybox
RUN echo "hello world"
COPY . /home/workfiles/
$ docker image build .
Sending build context to Docker daemon   2.56kB
Step 1/3 : FROM busybox
latest: Pulling from library/busybox
07a152489297: Already exists 
Digest: sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
Status: Downloaded newer image for busybox:latest
 ---> 8c811b4aec35
Step 2/3 : RUN echo "hello world"
 ---> Running in c09167e9ca23
hello world
Removing intermediate container c09167e9ca23
 ---> 36841040c842
Step 3/3 : COPY . /home/workfiles/
 ---> 044398957652
Successfully built 044398957652
$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
                            4f8e9b900fc0        9 seconds ago       1.15MB
busybox             latest              8c811b4aec35        6 weeks ago         1.15MB
$ docker run -it --name busytest 4f8e9b900fc0
/ # ls /home/workfiles/
essai.txt

Mais il est possible de spécifier le chemin vers le Dockerfile et le contexte, tout en définissant un tag pour l’image :

$ docker image build -f /path/to/Dockerfile -t image:tag /path/to/context-dir/

Équivaut à :
$ docker build -f /path/to/Dockerfile -t image:tag /path/to/context-dir/

  • -t : spécifie le tag de l’image
  • -f : spécifie le fichier à utiliser pour la construction (Dockerfile par défaut)
  • --no-cache : invalide le cache

Le contenu du fichier .dockerignore est ignoré du contexte.
ex :
Avec dockerfiles/Dockerfile

FROM busybox
RUN echo "hello world"
COPY . /home/workfiles/
$ docker image build -f dockerfiles/Dockerfile -t darwinos/repotest:busybox-V1.2 docker/
Sending build context to Docker daemon  2.095kB
Step 1/3 : FROM busybox
latest: Pulling from library/busybox
07a152489297: Pull complete 
Digest: sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
Status: Downloaded newer image for busybox:latest
 ---> 8c811b4aec35
Step 2/3 : RUN echo "hello world"
 ---> Running in 6b6bbf94efeb
hello world
Removing intermediate container 6b6bbf94efeb
 ---> 7d7ff4a1708b
Step 3/3 : COPY . /home/workfiles/
 ---> 79d9a58ba6da
Successfully built 79d9a58ba6da
Successfully tagged darwinos/repotest:busybox-V1.2
$ docker image ls darwinos/repotest:busybox-V1.2
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
darwinos/repotest   busybox-V1.2        79d9a58ba6da        29 seconds ago      1.15MB

On peut ensuite lancer un container à partir de cette image :

$ docker run -it --name busytest darwinos/repotest:busybox-V1.2
/ # ls /home/workfiles/
essai.txt

 

Fermer le menu