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 build a Docker container

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

Share

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

How to build Docker containers, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

one。 Build a Nginx container

Nginx is a lightweight WEB server and an excellent reverse proxy server. Nginx service occupies less memory and has strong concurrency ability, so it is deeply welcomed by users at home and abroad. Let's use the Dockerfile file to create an Docker image with a Nginx service.

1. Download the basic image

Download a basic image centos image that creates a Nginx image:

[root@localhost media] # systemctl start docker [root@localhost media] # docker load

< centos 2.建立工作目录 创建工作目录 [root@localhost /]# mkdir nginx[root@localhost /]# cd nginx 3.创建并编写Dockerfile文件 [root@localhost nginx]# vim DockerfileFROM centos \\设置基础镜像MAINTAINER the contos projec \\维护该镜像用户信息RUN yum install -y wget proc-devel net-tools gcc zlib zlib-develmake openssl-devel \\安装相关依赖包RUN wget http://nginx.org/download/nginx-1.9.7.tar.gz RUN tar zxf nginx-1.9.7.tar.gz \\下载并解压源码包WORKDIR nginx-1.9.7 RUN ./configure --prefix=/usr/local/nginx && make && make install \\编译安装NginxEXPOSE 80EXPOSE 443 \\开启80和443端口RUN echo "daemon off;">

> / usr/local/nginx/conf/nginx.conf\\ copy the service startup script and set permissions WORKDIR / root/nginxADD run.sh / run.shRUN chmod 755 / run.shCMD ["/ run.sh"]\\ execute the script when you start the container

4. Write and execute script content

[root@localhost nginx] # vim run.sh#!/bin/bash/usr/local/nginx/sbin/nginx

5. Generate a mirror image

[root@localhost nginx] # docker build-t nginx:xws. There is a point in the back, don't forget it.

6. Start the container for testing

[root@localhost nginx] # docker run-d-P nginx:xws\\-P is mapped random port 4794d0f564c93f09827d33f70be21f3893733648a56e4d3985f82611efb2aa0b

You can see that the port is mapped to 32769

[root@localhost nginx] # docker ps-a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4794d0f564c9 nginx:xws "/ run.sh" 13 minutes ago Up 13 minutes 0.0.0.0 a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4794d0f564c9 nginx:xws 32769-> 80/tcp, 0.0.0.0 a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4794d0f564c9 nginx:xws 32768-> 443/tcp optimistic_goldberg

Verification

two。 Build a Tomcat container

Tomcat is a free and open source lightweight Web server, which is widely used in small and medium-sized enterprises and small and medium-sized enterprises. It is the first choice for developing and debugging JSP programs. Let's use the Dockerfile file to create an Docker image with a Tomcat service.

1. Create a working directory

[root@localhost /] # mkdir tomcat [root@localhost /] # cd tomcat/ [root@localhost tomcat] # cp / media/jdk-8u91-linux-x64.tar.gz. [root@localhost tomcat] # lsjdk-8u91-linux-x64.tar.gz

two。 Create a Dockerfile file

[root@localhost tomcat] # vim DockerfileFROM centos\\ basic image MAINTAINER the centos project\\ maintain the image user information ADD jdk1.8.0_91 / usr/local/jdk-8u91\\ set the jdk environment variable ENV JAVA_HOME / usr/local/jdk-8u91ENV JAVA_BIN / usr/local/jdk-8u91/binENV JAVA_HOME / usr/local/jdk-8u91/jreENV PATH $PATH:/usr/local/jdk-8u91/bin:/usr/local/jdk-8u91/ Jre/binENV CLASSPATH / usr/local/jdk-8u91/jre/bin:/usr/local/jdk-8u91/lib:/usr/local/jdk-8u91/jre/lib/charsets.jarRUN yum install-y wget\\ install the wget tool RUN wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.49/bin/apache-tomcat-8.5.49.tar.gzRUN tar zxf apache-tomcat-8.5.49.tar.gzRUN mv apache-tomcat -8.5.49 / usr/local/tomcatEXPOSE 8080 ADD run.sh / run.sh\\ add and execute the script RUN chmod 775 / run.shCMD ["/ run.sh"] when starting the container

3. Write and execute script content

[root@localhost tomcat] # vim run.sh#!/bin/bash/usr/local/tomcat/bin/startup.shtailf / run

4. Use Dockerfile to generate an image

[root@localhost tomcat] # docker build-t tomcat:aaa.

There can be no less dots behind.

5. Run the container and verify

[root@localhost tomcat] # docker run-d-name asd-p 80purl 8080 tomcat:aaa

three。 Build a mysql container

MySQL is the most popular relational database at present, and SQL language is the most commonly used standardized language for accessing databases. MySQL has the advantages of small size and high speed. The advantage of low cost has become the preferred database for small and medium-sized enterprises. Let's use the Dockerfile file to create an Docker image with a MySQL service.

1. Download the basic image and create a working directory

Download image command with: docker pull docker.io

[root@localhost /] # cd / media/ [root@localhost media] # lscentos centos6 jdk-8u91-linux-x64.tar.gz [root@localhost media] # docker load < centos65d574ede99e4: Loading layer 202 MB/202 MBb077477046f8: Loading layer 35.54 MB/35.54 MBb244861945e8: Loading layer 2.048 kB/2.048 kBLoaded image: docker.io/guyton/centos6:latest [root@localhost media] # mkdir / mysql [root@localhost media] # cd / mysql/ [root@localhost mysql] #

two。 Create a Dockerfile file

[root@localhost mysql] # vi DockerfileFROM guyton/centos6\\ set the basic image MAINTAINER the centos6 to maintain the user information of the image RUN yum install-y mysql mysql-serverRUN / etc/init.d/mysqld start & &\ start the MySQL service to authorize mysql- e "grant all privileges on *. * to 'root'@'%' identified by' 123456;" & &\ mysql- e "grant all privileges on *. * to 'root'@'localhost' identified by' 123456' "EXPOSE 3306\\ Open port 3306 CMD [" mysqld_safe "]\\ run the initialization script mysql_safe

3. Use Dockerfile to generate an image

[root@localhost mysql] # docker build-t mysql:bbb.

There can be no less dots behind.

4. Run the container and verify

[root@localhost mysql] # docker run-d-name mysql-P mysql:bbb

5. Verification

[root@localhost mysql] # mysql-h 192.168.6.132-u root-P 32271-p123456

To connect to the container database using the MysQL command here, you need to use the

Yum install mysql installs the mariaDB client.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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