Dockerfile Build apache, SSH Service

Source: Internet
Author: User
Tags docker run

Dockerfile file

Lab Requirements:

1. Creating a container daemon run

2. Create a mirror using the Dockerfile file

3. using mirrors to build httpd, sshd services

Experimental steps:

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M02/8F/0F/wKioL1jSfvGz1NtlAABk0ecGfWY189.png "title=" Image001.png "style=" float:none; "alt=" wkiol1jsfvgz1ntlaabk0ecgfwy189.png "/>

Daemon operation

more time, need to let Docker containers Run in the backgroundin the form of a daemon (daemonized). At this point, you can do so by adding the -d parameter. For example, The following command runs the container in the Background.

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M00/8F/11/wKiom1jSfvLQlXSQAABRZb4p76w360.png "title=" Image002.png "style=" float:none; "alt=" wkiom1jsfvlqlxsqaabrzb4p76w360.png "/>

Or

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M01/8F/11/wKiom1jSfvPQWUcjAABce0inXMo175.png "title=" Image003.png "style=" float:none; "alt=" wkiom1jsfvpqwucjaabce0inxmo175.png "/>

when the container starts, it returns a unique ID, or you can view the container information by using the Dockerps command.

1. Docker run-d runs a new container and we let him run as a background with the-d command

2. CENTOS:CENTOS6 is an image of the command we want to run internally

3. /bin/sh-c is the command we want to run inside the Container.

4. while true; Do Echohello weibo; Sleep 1; Done This is a simple script that we just print once per second Hello Word until we end it

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M01/8F/0F/wKioL1jSfvSzFVSBAACJ7zoL76s848.png "title=" Image004.png "style=" float:none; "alt=" wkiol1jsfvszfvsbaacj7zol76s848.png "/>

to view the host name of a container with Docker inspect

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M00/8F/0F/wKioL1jSfvXhc8_rAAAs5ZKT0DA102.png "title=" Image005.png "style=" float:none; "alt=" wkiol1jsfvxhc8_raaas5zkt0da102.png "/>

Enter the container

When the-d parameter is used , the container boots into the background when it is Started. There are a number of ways to get into a container at some point, including using the Docker attach command or the Nsenter command.

entering containers using docker attach

Docker attach is a command that comes with Docker. The following example how to use this Command.

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M02/8F/11/wKiom1jSfvXjYINoAABnvJnHqHU077.png "title=" Image006.png "style=" float:none; "alt=" wkiom1jsfvxjyinoaabnvjnhqhu077.png "/>

Docker attach allows us to enter the background process .

Remove all containers

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M02/8F/0F/wKioL1jSfvejuUnVAAA12qzXCyo688.png "title=" Image007.png "style=" float:none; "alt=" wkiol1jsfvejuunvaaa12qzxcyo688.png "/>

Dockfile is a script that is interpreted by the Docker program,Dockerfile consists of a single instruction, each of which corresponds to a command under Linux. the Docker program translates these Dockerfile instructions into real Linux commands. Dockerfile has its own writing format and support commands,and the Docker program solves the dependencies between these commands, similar to Makefile. the Docker program will read the Dockerfileand generate a custom image according to the instructions . compared toImagethis black box,DockerfileThis obvious script is more easily accepted by the user, and it clearly indicatesImageHow It's Produced. with Dockerfile, when we need to customize our own additional requirements, simply Add or modify instructions on the Dockerfile to regenerate the image , eliminating the hassle of knocking COMMANDS.

Dockerfile consists of a row of line command statements and supports comment lines that begin with #.

The Dockerfile instruction is case-insensitive, it is recommended to use uppercase, each line supports only one instruction, each instruction can carry more than one parameter.

Dockerfile 's instructions can be divided into two types, building instructions and setting instructions according to the Function. the build directive is used to build an imagewhose specified action is not executed on the container running the image; the settings directive is used to set the property of the image , and the specified action will run executed in the container of Image.

in general, The Dockerfile is divided into four parts: basic image information, Maintainer information, mirroring operation instructions, and execution instructions when the container Starts.

Docker Application case: create an sshd image template with Dockerfile and provide HTTP access to the app

1) Create a sshd_dockerfile working directory

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M00/8F/11/wKiom1jSfvjCNtcOAAB0J44sfVM360.png "title=" Image008.png "style=" float:none; "alt=" wkiom1jsfvjcntcoaab0j44sfvm360.png "/>

edit run.sh file

[email protected] dockerfile_ssh]# Cat run.sh

#!/bin/sh

/usr/sbin/httpd-d Dforeground

/usr/sbin/sshd-d


650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M00/8F/0F/wKioL1jSfviwCWBQAABKSz3eXik674.png "title=" Image009.png "style=" float:none; "alt=" wkiol1jsfviwcwbqaabksz3exik674.png "/>

Generate an SSH key pair on the host and create the Authorized_keys file

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M01/8F/11/wKiom1jSfvzDZQRUAAEkb2eURuU682.png "title=" Image010.png "style=" float:none; "alt=" wkiom1jsfvzdzqruaaekb2euruu682.png "/>

2) write Dockerfile

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M01/8F/0F/wKioL1jSfvyy-bncAAAIRJfh9q4282.png "title=" Image011.png "style=" float:none; "alt=" wkiol1jsfvyy-bncaaairjfh9q4282.png "/>


From DOCKER.IO/CENTOS:CENTOS6

Maintainer from [email protected]

RUN Yum install-q-y httpd Openssh-serversudo

RUN Useradd Admin

RUN echo "admin:admin" | chpasswd

RUN echo "admin all= (all) all" >>/etc/sudoers

RUN ssh-keygen-t Dsa-f/etc/ssh/ssh_host_dsa_key

RUN ssh-keygen-t Rsa-f/etc/ssh/ssh_host_rsa_key

RUN mkdir-p/var/run/sshd

RUN Mkdir-p/home/admin/.ssh

RUN Sed-ri ' s/#ServerNamewww. example.com:80/servername www.benet.com/g '/etc/httpd/conf/httpd.conf

ADD Authorized_keys/home/admin/.ssh/authorized_keys

ADD run.sh/run.sh

RUN chmod 775/run.sh

EXPOSE 22 80

CMD ["/run.sh"]

The meanings of the above options are explained:

From CENTOS:CENTOS6 Select an existing OS image as the basis

author of the maintainer image

RUN yum install-y openssh-server sudo install openssh-server and sudo packages

Add test User admin, password admin, and add this user to sudoers

RUN Useradd Admin

RUN echo "admin:admin" | chpasswd

RUN echo "admin all= (all) all" >>/etc/sudoers

the following two sentences are more special, must have on the CENTOS6 , otherwise created by the container sshd cannot log on

RUN ssh-keygen-t Dsa-f/etc/ssh/ssh_host_dsa_key

RUN ssh-keygen-t Rsa-f/etc/ssh/ssh_host_rsa_key

note:Centos7 must have, otherwise the created container sshd cannot log on

RUN ssh-keygen-t Dsa-f/etc/ssh/ssh_host_dsa_key

RUN ssh-keygen-t Rsa-f/etc/ssh/ssh_host_rsa_key

RUN ssh-keygen-t Ed25519-f/etc/ssh/ssh_host_ed25519_key

RUN ssh-keygen-t Ecdsa-f/etc/ssh/ssh_host_ecdsa_key

upload the public key information to the remote connection User's host directory under . ssh

ADD Authorized_keys/home/admin/.ssh/authorized_keys

start the sshd service and expose the port

RUN mkdir/var/run/sshd

EXPOSE 22 80

CMD ["/run.sh"] can also be written in this way cmd["/usr/sbin/sshd", "-d"]

under the Sshd_dockerfile directory, use the Docker build command to create the image, and note that there is a ". ", which means using Dockerfile in the current directory , or using an absolute path,/root/sshd_dockerfile

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M02/8F/11/wKiom1jSfv3SPPEUAAIrtucfMB0502.png "title=" Image012.png "style=" float:none; "alt=" wkiom1jsfv3sppeuaairtucfmb0502.png "/>

View the created image such as:

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M01/8F/11/wKiom1jSfv7iMnbEAACO23dSi1Y154.png "title=" Image013.png "style=" float:none; "alt=" wkiom1jsfv7imnbeaaco23dsi1y154.png "/>

Use the image you just built to run a container that maps the Container's port to the Host's 10122

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M01/8F/0F/wKioL1jSfv6BNK9QAAC0BXzyJJo658.png "title=" Image014.png "style=" float:none; "alt=" wkiol1jsfv6bnk9qaac0bxzyjjo658.png "/>


using the native SSH connection container

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M02/8F/0F/wKioL1jSfv_Dn2V9AAHc1Y-9RQE921.png "title=" Image015.png "style=" float:none; "alt=" wkiol1jsfv_dn2v9aahc1y-9rqe921.png "/>

Using the native Access Web services

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M02/8F/11/wKiom1jSfwCjn4SsAAHfk4uabJY336.png "title=" Image016.png "style=" white-space:normal;color:rgb (0,0,238); float:none; alt= "wkiom1jsfwcjn4ssaahfk4uabjy336.png" />

The above experiment is done!


===========================================================================================


Through the combination of theory and practice can better improve, Welcome to reprint, learning!


This article is from "xiao ming" blog, Please be sure to keep this source http://taoliang.blog.51cto.com/12183183/1909401

Dockerfile Build apache, SSH Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.