Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Docker Image under CentOS7

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to make Docker image under CentOS7". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

There are two methods for making Docker Image: 1:docker commit # saves the current state of container to image, and then generates the corresponding image method 2:docker build # automates the production of 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 Dockerfile files to automate the creation of image image Dockerfile, which is a bit like the Makefile generated after source code compilation. / configure

1. Create a working directory

[root@Docker ~] # mkdir / docker-build [root@Docker ~] # cd / docker-build [root@Docker docker-build] # touch Dockerfile [root@Docker docker-build] # 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@Docker docker-build] # 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 RUN yum # 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@Docker docker-build] # 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@Docker docker-build] # chmod axix start.sh to create index.html [root@Docker docker-build] # 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@Docker docker-build] # 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 5: MAINTAINER-- > Running in 0180810d2ab3Removing intermediate container 0180810d2ab3-- > 5b9af0184bcfStep 3 y install httpd 5: RUN yum-y install httpd-- > Running in 8f5c114649edCentOS-8-AppStream 228 kB/s | 7.0 MB 00:31 CentOS-8-Base 547 kB/s | 2.2 MB 00:04 CentOS-8-Extras 431 Bash s | 5.9 kB 00:14 Dependencies resolved.==== Package Arch Version Repo Size====Installing: httpd x86 examples 64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 1.7 MInstalling dependencies : apr x86 centos-logos-httpd noarch 64 1.6.3-9.el8 AppStream 125k apr-util x8634 1.6.1-6.el8 AppStream 105k centos-logos-httpd noarch 80.5-2.el8 AppStream 24k. Omit part of the output information. Completing 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@Docker docker-build] # 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

This is the end of [root@Docker docker-build] # docker run-it centos:httpd / bin/bash [root@1188a43a4585 /] # lsbin etc lib lost+found mnt proc run srv tmp vardev home lib64 media opt root sbin sys usr [root@1188a43a4585 /] # rpm-qa httpdhttpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64 [root@1188a43a4585 /] # exitexit [root@Docker docker-build] # "how to make Docker Image under CentOS7". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report