In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the method of making Docker image? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Two methods of making Docker Image
Method 1:docker commit # saves the current state of container to image, and then generates the corresponding image method 2:docker build # automatically making image using Dockerfile files
Method 1:
Docker commit
Create a container image with the apache tool installed
[root@Docker ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 470671670cac 4 months ago 237MB [root@Docker ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@Docker ~] # docker Run-it centos:latest / bin/bash [root@1b96e68a3cce /] # [root@1b96e68a3cce /] # yum-y install httpd # install the apache package [root@1b96e68a3cce /] # exit in container
View images list
[root@Docker ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 470671670cac 4 months ago 237MB
Create an image image based on the current state of the container: create a centos image with the apache tool installed
Syntax: docker commit "ID of container" or "image_name"
View Container ID
[root@Docker ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1b96e68a3cce centos:latest "/ bin/bash" 3 minutes ago Exited (0) 2 minutes ago awesome_hypatia607752360adf centos:latest "/ bin/bash-c 'while …" 18 minutes ago Exited 12 minutes ago brave_fermi0a297ff99af8 centos:latest "/ bin/bash" 22 minutes ago Exited (1) 20 minutes ago ecstatic_yonathefb4af688330 centos:latest "/ bin/bash" 24 minutes ago Exited (0) 23 minutes ago Epic_ McClintock [root @ Docker ~] # docker commit 1b96e68a3cce centos:apachesha256:b8822ec8a7bbb325793e7908e355180be82a49481cff197661fb5d0da5872e88 [root@Docker ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos apache b8822ec8a7bb 9 seconds ago 280MBcentos latest 470671670cac 4 months ago 237MB
Use the newly created centos:apache image to generate a container instance
[root@Docker ~] # docker run-it centos:apache / bin/bash [root@e4c295d27581 /] # rpm-qa httpdhttpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
The httpd software name indicates that the container based on apache image has been created successfully.
Method 2:
Create a centos-based httpd web server image through: docker build
When you use docker build to create an image, you need to use a Dockerfile file to automatically create an image image.
Dockerfile is a bit like Makefile generated after source code compilation. / configure
1. Create a working directory,
[root@liangxu ~] # mkdir / docker-build [root@liangxu ~] # cd / docker-build [root@liangxu] # touch Dockerfile [root@liangxu] # lsDockerfile Note: Makefile file is required for automatic compilation of make, and Dockerfile is required for automatic creation of docker images.
2. Edit Dockerfile
Dockerfile is used to create a custom image that includes user-defined software dependencies, etc. [root@liangxu] # vim DockerfileFROM centos:latestMAINTAINERRUN yum-y install httpdADD start.sh / usr/local/bin/start.shADD index.html / var/www/html/index.htmlCMD echo hello world
Annotation
FROM centos:latest # FROM based on which image the MAINTAINER# MAINTAINER image creator RUN yum-y install httpd # RUN installer uses ADD start.sh / usr/local/bin/start.shADD index.html / var/www/html/index.html# ADD to copy files to the path corresponding to the file system of the newly created image. All files and folders copied to the new image have permissions of 0755 container and gid for commands or startup services executed when 0CMD echo hello world # container starts, but only one CMD command can be used in a Dockerfile, and the last CMD can be executed if there are more than one.
3. Create a start.sh script to start the httpd service and apache default home page index.html file
[root@liangxu] # echo "#! / bin/bash" > > start.sh [root@Docker docker-build] # echo "/ usr/sbin/httpd-DFOREGROUND" > > start.sh Note: / usr/sbin/httpd-DFOREGROUND is equivalent to executing systemctl start httpd [root@liangxu] # chmod axix start.sh to create index.html [root@liangxu] # echo "docker image build test from jaking" > index.html
4. Use the command build to create a new image
Syntax: docker build-t parent image name: path where the mirrored tag Dockerfile file is located
-t: indicates tag, image name
Example: use the command docker build to create a new image and name it centos:httpd
[root@liangxu] # lsDockerfile index.html start.sh [root@Docker docker-build] # docker build-t centos:httpd. / # Note:. / indicates the current directory In addition, your current directory should include DockerfileSending build context to Docker daemon 4.096kBStep 1 AppStream 5: FROM centos:latest-- > 470671670cacStep 2 MB 00:31CentOS-8 5: MAINTAINER-- > Running in 0180810d2ab3Removing intermediate container 0180810d2ab3-- > 5b9af0184bcfStep 3 MB 00:31CentOS-8 5: RUN yum-y install httpd-- > Running in 8f5c114649edCentOS-8-AppStream 228 kB/s | 7.0 MB 00:31CentOS-8-Base 547 kB/s | 2.2 MB 00:04CentOS-8-Extras 431 Bplink s | 5.9 kB 00:14Dependencies resolved.==== Package Arch Version Repo Size====Installing: httpd x86 16.module_el8.1.0+256+ae790463 AppStream 64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 1.7 MInstalling dependencies: apr X86'64 1.6.3-9.el8 AppStream 125k apr-util x86'64 1.6.1-6.el8 AppStream 105k centos-logos-httpd noarch 80.5-2.el8 AppStream 24 k... Omit some of the output information. Complete removing intermediate container 8f5c114649ed-> 040b5f229962Step 4 11a106005031Step 5: ADD start.sh / usr/local/bin/start.sh-> 11a106005031Step 5: ADD index.html / var/www/html/index.html-- > 85b4a3657cedSuccessfully built 85b4a3657cedSuccessfully tagged centos:httpd
View images list
[root@liangxu] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos httpd 85b4a3657ced 45 seconds ago 280MBcentos apache b8822ec8a7bb 20 minutes ago 280MBcentos latest 470671670cac 4 months ago 237MB# Note: docker image = application / program + library
Run the newly generated image
[root@liangxu] # docker run-it centos:httpd / bin/bash [root@liangxu /] # lsbin etc lib lost+found mnt proc run srv tmp vardev home lib64 media opt root sbin sys usr [root@liangxu /] # rpm-qa httpdhttpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64 [root@liangxu /] # exitexit [root@liangxu] # after reading the above, have you mastered the method of making Docker image? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.