The following two approaches are for you to consider. Approach 1 is for images that will only be updated occasionally whereas approach 2 is for images that will be frequently updated.
This approach is suitable for images that will only be updated occasionally.
Procedure:
For example, start a CentOS container in the interactive mode.
docker run -it centos
git clone https://github.com/lh3/bwa.git
cd bwa;make
Install Git in advance and check whether an SSH key is set on the local host.
This approach is suitable for images that will be frequently updated. In Approach 1, you create a snapshot of the whole container. This could be demanding if you need to frequently update your images. In this case, Approach 2 is put forward to automate the image build process.
The idea behind Approach 2 is to write the process of Approach 1 into a Dockerfile and then run the docker build -t test/image:tag. command to automatically build an image from the Dockerfile. In the preceding command, . indicates the path to the Dockerfile.
Example Dockerfile:
If an external network is required, ensure that network connectivity is available.
#Version 1.0.1 FROM centos:latest # Setting the root user as the executor of subsequent commands USER root # Performing operations RUN yum update -y RUN yum install -y java # Using && to concatenate commands RUN touch test.txt && echo "abc" >>abc.txt # Setting an externally exposed port EXPOSE 80 8080 1038 # Adding a network file ADD https://www.baidu.com/img/bd_logo1.png /opt/ # Setting an environment variable ENV WEBAPP_PORT=9090 # Setting a work directory WORKDIR /opt/ # Setting a start command ENTRYPOINT ["ls"] # Setting start parameters CMD ["-a", "-l"] # Setting a volume VOLUME ["/data", "/var/www"] # Setting the trigger operation for a sub-image ONBUILD ADD . /app/src ONBUILD RUN echo "on build excuted" >> onbuild.txt
It is used to specify the parent image (base image) from which you are building a new image. Except annotations, a Dockerfile must start with a FROM instruction. Subsequent instructions run in this parent image environment until the next FROM instruction appears. You can create multiple images in the same Dockerfile by adding multiple FROM instructions.
It is used to specify the information about the author who creates an image, including the username and email address. This parameter is optional.
It is used to modify an image. Generally, RUN commands are executed to install libraries, and install and configure programs. After a RUN command is executed, an image layer will be created on the current image. The next command will be executed on the new image. The RUN statement can be in one of the following formats:
It is used to specify one or more network ports that will be exposed on a container. If there are multiple ports, separate them by spaces.
When running a container, you can set -P (uppercase) to map the ports specified in EXPOSE to random ports on a host. Other containers or hosts can communicate with the container through the ports on the host.
You can also use -p (lowercase) to expose the ports that are not listed in EXPOSE.
It is used to add a file to a new image. The file can be a host file, a network file, or a folder.
It is used to create a mount point for a specified path (file or folder) in the image. Multiple containers can share data through the same mount point. Even if one of the containers is stopped, the mount point can still be accessed.
It is used to specify a new work directory for the next command. The directory can be an absolute or a relative directory. WORKDIR can be specified multiple times as required. When a container is started, the directory specified by the last WORKDIR command is used as the current work directory of the container.
It is used to set an environment variable for running the container. When running the container, you can set -e to modify the environment variable or add other environment variables.
Example:
docker run -e WEBAPP_PORT=8000 -e WEBAPP_HOST=www.example.com ...
It is used to specify the default command for starting a container.
It is used to specify the default command for starting a container. Difference: For ENTRYPOINT, parameters added to the image during container running will be spliced. For CMD, these parameters will be overwritten.
docker run gutianlangyu/test --entrypoint echo "hello world"
It is used to specify the user or UID for running the container, and running the RUN, CMD, or ENTRYPOINT command.
Trigger command. During image build, the image builder of the container engine saves all commands specified by the ONBUILD command to the image metadata. These commands will not be executed in the process of building the current image. These commands will be executed only when a new image uses the FROM instruction to specify the parent image as the current image.
Using the FROM instruction to build a child image based on the parent image created by the Dockerfile:
ONBUILD ADD. /app/src: The ADD. /app/src command is automatically executed.