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 all kinds of docker images

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you "how to make a variety of docker images", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to make a variety of docker images" this article.

I have done a mirror image for a week and received a lot of goods. Now I have sorted out the records and took them as work notes. Share the Dockerfile of several commonly used images.

Create a basic docker image

Step 1: set up the Docker mirror source

Yum install-y yum-priorities & & rpm- ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm & & rpm--import / etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

Step 2: install docker-io febootstrap, which is used to make a centos image, and a centos image will be generated at that time.

Yum-y install docker-io; if you do not have docker installed, you need to install docker service docker start first; start docker yum-y install febootstrap; to make docker image tool

Step 3: make the CentOS image file centos6-image directory

Febootstrap-I bash-I wget-I yum-I iputils-I iproute-I man-I vim-I openssh-server-I openssh-clients-I tar-I gzip centos6 centos6-image http://mirrors.aliyun.com/centos/6/os/x86_64/

After the execution of the previous step, a directory of centos6-image files is generated, and the parameter-I in the above command is followed by some services installed in the basic image. If you don't want so many services (because the image will become very large after installing all the services), you can install only some basic, essential services. Centos6 is the version, and centos6-image is the name of the generated directory.

Step 4: at this time, there are neither any files nor hidden point files in the root directory, such as .bash _ logout .bash _ profile .bashrc. If the image created at this time uses ssh to log in, it will go directly to the root directory, while the general image will enter the root directory, so you can set the .bash _ logout .bash _ profile .bashrc in the root directory of the centos6-image directory.

Cd centos6-image & & cp etc/skel/.bash* root/

Step 5: generate the most basic base image

Cd centos6-image & & tar-c. | docker import-centos6-base

Step 6: view the image, or go directly to centos6-base to view it.

Docker images; this is to view all the generated images docker run-I-t centos:centos6 / bin/bash; into the terminal (no ssh service),-I assign the terminal,-t means executed in the foreground,-d means to run in the background

Create docker image of ssh based on basic image

To make a ssh login image, the most important thing is Dockerfile (of course, this is the method of Dockerfile), and create a new Dockerfile file in a directory (be sure to name it Dockerfile).

Let's analyze the Dockerfile file:

# Dockerfile FROM centos6-base # means to use a certain image as the base image, which is equivalent to inheritance in object-oriented languages Indicates that the generated image contains some services of the basic image MAINTAINER yzh # this is an image author information RUN ssh-keygen-Q-N ""-t dsa-f / etc/ssh/ssh_host_dsa_key RUN ssh-keygen-Q-N ""-t rsa-f / etc/ssh/ssh_host_rsa_key RUN sed-ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' / etc/pam .d / sshd RUN mkdir-p / root/.ssh & & chown root.root / root & & chmod 700 / root/.ssh # the above lines configure the ssh login directory and login authentication The installation of ssh is completed in the basic image centos6-base (- I openssh-server-I openssh-clients) EXPOSE 22 # indicates which port number to open, and port 22 is for ssh service. If you do not need the port number, you can comment out this line RUN echo 'root:redhat' | chpasswd # this is to change the root password, in fact, this method is not very good, because it is to set the root password Not to change RUN yum install-y yum-priorities & & rpm- ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm & & rpm--import / etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 RUN yum install tar gzip gcc vim wget-y # it is to install some tools and source ENV LANG en_US.UTF-8 ENV LC_ALL en_US.UTF-8 # set up the environment CMD / Usr/sbin/sshd-D # sets the enabled service There can be only one valid CMD per image # End

Generate an image according to the Dockerfile file: docker build-t generate the image name Dockerfile location; suppose: the image name is centos6-ssh,Dockerfile in / home/yzh/ssh

Cd centos6-images;docker build-t centos6-ssh / home/yzh/ssh # can generate a centos6-ssh image

Look at all the mirrors with docker images, and you will see two images: centos6-base and centos6-ssh. So centos6-ssh is the docker image of the ssh login.

Test step commands for mirroring

After making an image, it is necessary to test whether the image is correct and whether some of the services are available. The above two articles have more detailed test instructions, as well as data storage, migration backup, you can refer to their articles. I don't currently use data storage and migration backup, so I'll test it with simple steps:

Step 1: check the image information and you can see the id with the image name and image, as well as the time when it was generated.

Docker images

Step 2: use the test image to generate a container, which can be generated according to the image name or image id. You'll end up with a series of numeric and alphabetic messages, which should be the container ID ID.

Docker run-d xxx (mirror name or id)

Step 3: use the container ID generated in the previous step to view the container information generated by the test image. This step results in detailed information about the container, including the iP address.

Docker inspect xxxx (Container id)

Step 4: log in to ssh using the ip address obtained in the previous step.

Ssh root@xxxx (Container ip)

Step 5: test the service, where the ssh service has been tested when you log in. If it is another image, such as mysql: then this step is to enter the image to test the MySQL service, and different services are tested in different ways.

Deletion of images and containers

Delete all containers after testing each image to avoid taking up too much space.

Delete a single container (specify a container):

First take a look at which containers are currently running: docker ps-a

Find the container that needs to be deleted and make it stop: docker stop xxx (CONTAINER ID)

Delete the container: docker rm xxx (CONTAINER ID)

Delete all containers:

If you want to delete all containers, you don't have to go to so much trouble, just stop running all containers: docker stop $(docker ps-a-Q); then delete all containers: docker rm $(docker ps-a-Q)

Delete the mirror:

If you fail to make an image, or if you want to add a new function and need to delete the original image, use: docker images to view all the images, and then use: docker rmi xxx (image name or image id)

Make an Apache image

Share the creation of the Apache image, which is slightly different from the basic image. Because when this is mirrored, you have to open two services at the same time: the ssh service (required) and the Apache service, and there can only be one valid sentence of CMD in Dockerfile. So use another method: supervisord; is the same as other steps, the only difference is the Dockerfile file, this Dockerfile is based on the ssh image (usually based on the ssh image, because no matter which image requires ssh login)

Dockerfile file:

# Dockerfile FROM centos6-ssh MAINTAINER http://blog.csdn.net/yuzhihui_no1 EXPOSE 80 # Open the service port RUN yum-y install httpd supervisor;chkconfig httpd on for Apache service; mkdir-p / var/log/supervisor ADD supervisord.conf / etc/supervisord.conf # copy local files to the image system CMD ["/ usr/bin/supervisord"] # execute supervisord.conf script file # End

RUN xxx; followed by commands to be executed, which are commands to install a tool / software / service in Linux. The supervisord.conf file is introduced here:

[supervisord] nodaemon=true [program:sshd] command=/usr/sbin/sshd-D [program:httpd] command=/sbin/service httpd start

This allows you to start both services at the same time. The other steps are the same as the basic image creation, but remember that supervisord.conf should be placed in the same level directory as Dockerfile.

Make a Java image

Originally, the Java image can be created the same as the basic image, but I always report an error when installing the jdk package or getting the JDK package online, so I download a jdk on the official website and put it on the server, then use the ADD command to add it to the image system, decompress it in the system, and finally delete the jdk package to configure the environment.

Dockerfile file:

# Dockerfile FROM centos6-ssh MAINTAINER yzh RUN mkdir-p / usr/java;yum install-y tar ADD jdk-8u25-linux-x64.tar.gz / usr/java/ # add local jdk to the mirror system RUN echo 'export JAVA_HOME=/usr/java/jdk1.8.0_25' > > / etc/profile; echo' export JRE_HOME=/usr/java/jdk1.8.0_25/jre' > > / etc/profile Echo 'export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin' > > / etc/profile RUN echo' export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib' > > / etc/profile; echo 'export JAVA_HOME JRE_HOME PATH CLASSPATH' > > / etc/profile above is the configuration jdk environment variable RUN source / etc/profile Yum clean all # make environment variables effective ADD HelloWorld.java / home/java/HelloWorld.java # add a simple Java test program CMD / usr/sbin/sshd-D # End

The files to be added to the mirror system through the ADD command must be in the same level directory as Dockerfile. For example, there are HelloWorld.java jdk-8u25-linux-x64.tar.gz files in the same directory as Dockerfile in the Java image. There is also some Dockerfile ADD command will also extract some basic compression packages, such as the above to add jdk, you do not need to decompress yourself, you only need to install the tar decompression command.

-add -

Recently, I have made several more complex docker images. Here's a summary: take postgresql images. If you follow the previous practice, of course, do some installation operations on the experimental machine, wait until the installation is successful, the test is no problem, then write these steps into dockerfile. Of course, because it is a database, it needs to boot itself, so we need to use supervisord.conf to write both services into this configuration. Then put these written files on the server and start making images.

But you will find that this method will be very slow (this method also has the advantage that according to dockerfile can clearly know what is done in this image), if you are skilled enough, then you should try an upgraded version of the new method, using containers to make images

Quickly make an image with a container

To create an image with a container is to create a container for a mirror. For example, when making a postgresql image, because it is also a database, you use the container of mysql to make it. Get the image of mysql, then enter the image, and start to install postgresql. After installation, test it. If you can, package the container into an image. Here are the simple steps:

1. When you start making postgresql images, use mysql to get a container: docker run-d xxx_mysql_xxx

2. Omit some steps (for commands such as creating a container and logging in to a container, please refer to the "Mirror Test step Command" in this blog)

3. When entering the container, first uninstall mysql to see how many mysql packages are available: rpm-qa | grep 'mysql';, and then uninstall them one by one according to the displayed packages: yum remove xxx

4. For the steps of postgresql installation, please refer to https://www.jb51.net/LINUXjishu/10861.html

5. Modify boot and start, because this is a mysql image, and the startup starts with mysql service. You can modify it: in vim / etc/supervisord.conf, you can modify the relevant startup commands.

6. Package the container into an image: docker commit xxx (id of the container) xxxx (the name of the image to be made)

Through the above steps, you can quickly make a mirror image, which has many advantages:

The first is that it can be done step by step. When you make a more complex image, it is impossible to succeed in one step, so when you think there may be an error in the next step (irreversible error), you can first package the image. If you make a mistake next, delete the container and use the image you just packaged as a container to continue the previous steps.

The second is that it doesn't matter how much you mess around in the container, just delete the container, if you pay attention to the physical server, otherwise the whole server will be paralyzed by duang.

The third benefit is similar to the first, because some databases cannot be tested, and you will generate a lot of data (logs and some default data) as soon as you test (when I do mongodb mirroring, the packaged image reaches more than 4 gigabytes, while the untested one is hundreds of MB, the difference is too big), because the docker image cannot be too big, otherwise it is difficult to upload. So it is generally done, first make a package into a mirror image, and then test it, if successful. That's fine.

Pull the image into openstack for testing

1. Find keystone_admin first, and run the command: source keystonerc_admin

2. Put the created image into openstack. Command: docker save centos (centos is the name of the image) | glance image-create-- is-public=True-- container-format=docker-- disk-format=raw-- name centos (centos is the name of the image) verify whether the operation is successful: echo $? If it is 0, it should be successful.

3. Log in to openstack and go to the image column to see if there is any image you uploaded.

4. Create a container in openstack to start testing

Package and unpack the docker image

1. Packaging: docker save IMAGENAME (image name) | bzip2-9-c > img.tar.bz2 (packaged name)

2. Unpack: bzip2-d-c

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

Servers

Wechat

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

12
Report