In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to use dockerfile instructions to build a docker image", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use dockerfile instructions to build docker images.
First acquaintance of Dockerfile
Dockerfile is the build file used to build docker images! Command script!
Through this script, you can generate an image, the image is layer by layer, the script is a line of commands, each line of command is a layer
Dockerfile is development-oriented. If we want to release the project and mirror it in the future, we need to write a dockerfile file, which is very simple.
Dockerfile images have become the standard of enterprise delivery, which must be mastered!
Write your own docker image:
Write a Dockerfile file:
# create a dockerfile file with a random name. Suggest the contents of the Dockerfile# file: instruction (uppercase) parameter # instance test: # create a directory to store dockerfile files: [root@localhost ~] # mkdir docker_test # enter the directory to create a dockerfile file: [root@localhost ~] # mkdir docker_ test [root @ localhost] # cd docker_test/ [root @ localhost docker_test] # touch dockerfile # write a dockerfile file Write the dockerfile instruction in the file: [root@localhost docker_test] # vim dockerfileFROM centosVOLUME ["volume01", "volume02"] CMD echo "- end----" CMD / bin/bash
Turn the Dockerfile file into a mirror:
# use docker build. Command to mirror the dockerfile file [root@localhost docker_test] # docker build-f / root/docker_test/dockerfile-t hzw/centos .Sending build context to Docker daemon 2.048kB Step 1 to 4: FROM centos-- > 5d0da3dc9764Step 2 to 4: VOLUME ["volume01" "volume02"]-- > Running in d4b23ae86aceRemoving intermediate container d4b23ae86ace-- > 8f39792fbef7Step 3 end---- 4: CMD echo "- end----"-- > Running in 1bb4751f04efRemoving intermediate container 1bb4751f04ef-> 2e2bb5e2a502Step 4 Running in 1bb4751f04efRemoving intermediate container 1bb4751f04ef 4: CMD / bin/bash-- > Running in 82a0b5905405Removing intermediate container 82a0b5905405-- > 39a1fd86fd73Successfully built 39a1fd86fd73Successfully tagged hzw/centos:latest # thus it can be found that docker creates images layer by layer. Each command is a layer of mirroring.
View the docker image built by yourself
[root@localhost docker_test] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEhzw/centos latest 39a1fd86fd73 2 minutes ago 231MB
Use the docker image you built to generate the container:
[root@localhost docker_test] # docker run-it hzw/centos / bin/bash [root@f62a714bb306 /] # lsbin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02 # you can see that the image we built can also be used normally, and the directory we mounted in the dockerfile write instruction is also in this container. DockerFile
Dockerfile is used to build docker image files! Command parameter script!
Build steps:
1. Write a dockerfile file
2 、 docker build. Build is called a mirror image
3. Docker run runs the image
4. Docker push publishing image (DockerHub, Ali Cloud image repository)
Dockerfile directive:
FROM # specifies the basic image, everything starts here to build MAINTAINER # image who wrote, name + mailbox RUN # image construction command ADD # step: tomcat image, this tomcat package! Add content WORKDIR # image working directory VOLUME # mount directory EXPOST # expose port configuration CMD # specify the command to be run by this container at startup, only the last one will take effect, it can be replaced by ENTRYPOINT # to specify the command to be run by the container's board of directors, you can append the command ONBUILD # when an inherited dockerfile is built, the ONBUILD instruction will be run at this time. Is a trigger instruction COPY # similar to ADD, copy our files to the image ENV # to set the environment variable when building!
Docker build. Command
After building the dockerfile file, we use the docker build command to build the file into an image.
# how to use the command: docker build-f dockerfile file path-t take an image name. # Parameter description:-f dockerfile file path, you can write an absolute path or a relative path-t give the built image a name, name format: name: label. The path to the build context must be written to see how the image is generated by the docker history image ID
Practical training: Dockerfile to build Nginx image: write Dockerfile to make an image, and generate an image named my_build:Nginx1. First create the directory dockerfile-nginx1 and save the Dockerfile file. The specific requirements are as follows:
(1) based on mirror centos:7
(2) install Nginx
a. Installation related dependencies
b. Download and extract Nginx
(3) set the working directory to: nginx-1.9.7
(4) compile and install Nginx
(5) Port 80443 exposed to the public
(6) modify the Nginx configuration file to start it in a non-daemon way
(7) copy the service startup script and set permissions
(8) execute the script run.sh when starting the container
The run.sh content is as follows:
Start the operation:
Create the dockerfile-nginx1 folder, enter the folder, and write the Dockerfile file
Mkdir dockerfile-nginx1cd dockerfile-nginx1/vim Dockerfile
Write the contents of the Dockerfile file:
# this file is based on the FROM centos:7 created based on the centos7 image # download nginx dependent source RUN yum-y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel # download wgetRUN yum install-y wget # download nginxRUN wget http://nginx.org/download/nginx-1.9.7.tar.gz # extract nginxRUN tar-zxvf nginx-1.9.7.tar.gz # execute the compilation command and install the directory through-- prefix= Specify the nginx installation directory RUN. / configure-- prefix=/usr/localinx & & make & & make install # set the working directory WORKDIR nginx-1.9.7 # expose port 80 and 443EXPOSE 80EXPOSE 443 # copy virtual machine files into the container ADD run.sh / run.sh # grant run.sh executable permissions RUN chmod 775 / run.sh # execute the run.sh script CMD after starting the container ["/ run.sh"]
Write another run.sh in this folder
Content in vim run.sh run.sh: #! / bin/bash/usr/local/nginx/sbin/nginx
Docker build builds an image
Docker build-t my_build:nginx1.
Docker images to view the image:
Use this image to build the container:
You can see that the image starts successfully, things are downloaded and installed, the working directory is set, and the files are copied to the container.
At this point, I believe you have a deeper understanding of "how to build a docker image with dockerfile instructions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.