Intoduction to the Dockerfile

source image : deploybot.com
 Intoduction to the Dockerfile for beginner
 
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. source: Dockerfile
Create Dockerfile
vim Dockerfile
 The content Dockerfile
FROM docker/whalesay:latest
RUN apt -y update && apt install -y fortunes
CMD /usr/games/fortune -a | cowsay

The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.

RUN has 2 forms:

  • RUN <command> (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
  • RUN ["executable", "param1", "param2"] (exec form)

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

The CMD instruction has three forms:

  • CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  • CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

Build the Dockerfile

sudo docker build -t docker-whale .

see docker image

sudo docker image ls
docker images
Then run image docker-whale
sudo docker run docker-whale 
run image docker-whale

Reference : https://docs.docker.com/engine/reference/builder/

Share:

0 comments:

Post a Comment