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 use Docker to build services

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

Share

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

This article is about how to use Docker to build services. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

6.1Building applications using Jekyll framework and Apache

Two images need to be built:

An image installs Jekyll and other necessary software packages for building a Jekyll website

A mirror image uses Apache to make the Jekyll site work.

The workflow is as follows:

Create Jekyll basic image and Apache image

Create a container from the Jekyll image that holds the website source code mounted through the volume

Create a container from the Apache image that uses the volume containing the compiled website to serve it

Clean up and repeat the above steps when the site needs to be updated.

This example can be seen as the easiest way to create a multi-hosting site.

1. Jekyll basic image

Establish a build environment:

Mkdir jekyllcd jekylltouch Dockerfile

Write a Dockerfile:

FROM ubuntu:14.04MAINTAINER James Turnbull RUN apt-get-yqq updateRUN apt-get-yqq install ruby ruby-dev make nodejsRUN gem install-- no-rdoc-no-ri jekyll-v 2.5.3VOLUME / dataVOLUME / var/www/htmlWORKDIR / dataENTRYPOINT ["jekyll", "build", "--destination=/var/www/html"]

Note that there is no-v 2.5.3 after the code gem jekyll in the book, which will report an error.

Two volumes are created using the VOLUME directive in Dockerfile:

/ data/, is used to store the source code of the website

/ var/www/html/, is used to store the compiled Jekyll site code.

Finally, specify the working directory as / data and specify the auto-build command through the ENTRYPOINT directive, which builds all the Jekyll Web site code in the working directory / data/ into the / var/www/html/ directory.

2. Build a basic image of Jekyll

Docker build-t ivan/jekyll.

3. Apache image

Establish a build environment:

Mkdir apachecd apachetouch Dockerfile

Write a Dockerfile:

FROM ubuntu:14.04MAINTAINER James Turnbull RUN apt-get-yqq updateRUN apt-get-yqq install apache2VOLUME ["/ var/www/html"] WORKDIR / var/www/htmlENV APACHE_RUN_USER www-dataENV APACHE_RUN_GROUP www-dataENV APACHE_LOG_DIR / var/log/apache2ENV APACHE_PID_FILE / var/run/apache2.pidENV APACHE_RUN_DIR / var/run/apache2ENV APACHE_LOCK_DIR / var/lock/apache2RUN mkdir-p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_ DIREXPOSE 80ENTRYPOINT ["/ usr/sbin/apache2"] CMD ["- D" "FOREGROUND"]

A volume is created using the VOLUME directive-/ var/www/html/, this directory is used to hold the compiled jekyll website

Finally, a combination of ENTRYPOINT and CMD instructions is specified to run apache by default when the container starts.

4. Build an Apache image

Docker build-t ivan/apache.

5. Launch the Jekyll website

First download some of the source code for Jekyll:

Cd ~ git clone https://github.com/jamtur01/james_blog.git

Then start the container:

Docker run-v ~ / james_blog:/data/-- name james_blog ivan/jekyll

Mount the james_blog directory you just downloaded from github to / data as a volume (the directory contains the source code of the jekyll website).

Here we review the volume, which is a directory specifically specified in one or more containers, and the volume bypasses the federated file system, providing several useful features for persistent and shared data:

Volumes can be shared and shared between containers

It is not necessary to run the corresponding container when sharing volumes

Changes to the volume will be reflected directly on the volume.

Changes to the volume are not included when updating the mirror

Volumes remain until there is no container to use them.

The volume is located in the / var/lib/docker/volumes directory of the Docker host. You can view the specific location of a volume through the docker inspect command:

Docker inspect-f "{{.volumes}}"

If you want to use the compiled Web site in the / var/www/html/ volume in another container, you can create a new container to connect to the volume (that is, the Apache container):

Docker run-d-P-- volumes-from james_blog ivan/apache

The volumes-from flag adds all volumes in the specified container to the new container.

If the last container that uses the volume is deleted, the volume is deleted. So be careful when deleting containers with volumes.

In this way, the whole service is started.

6. Back up the Jekyll volume

As mentioned above, deleting the container may have accidentally deleted the volume. So you can use the command to back up the volume:

Docker run-rm-volumes-from james_blog-v $(pwd): / backup ubuntu tar cvf / backup/james_blog_backup.tar / var/www/html

The rm command indicates that the container is used only once and will be deleted after running it.

Mount the current directory to / backup so that after the data is packaged into / backup in the container, the package is actually in the current directory.

The tar cvf command creates a tar file called james_blog_backup.tar (which contains everything in the / var/www/html directory).

7. Extended Jekyll example

Run multiple Apache containers, all of which use the volumes of james_blog containers. Add a load balancer in front of these Apache containers to have a Web cluster

Further build an image that copies the user-provided source data to the volume and mounts the volume into the jekyll container. This is a general scheme that can be migrated, and the host contains any source code locally.

Build a Web front segment for our service based on the previous extension, which automatically builds and deploys the Web site from the specified source.

6.2Building a Java Application Service (Tomcat)

1. WAR file acquirer

Create a build environment

Mkdir fetchercd fetchertouch Dockerfile

Write Dockerfile

FROM ubuntu:14.04MAINTAINER Ivan Jiang ENV REFRESHED_AT 2016-07-13 RUN apt-get-yqq updateRUN apt-get-yqq install wget VOLUME ["/ var/lib/tomcat7/webapps/"] WORKDIR / var/lib/tomcat7/webapps/ ENTRYPOINT ["wget"] CMD ["--help"]

When the container executes, use wget to get the file from the specified URL and save it in the / var/lib/tomcat7/webapps directory. Return wget help if you do not specify a combination of URL,ENTRYPOINT and CMD instructions when running the container.

2. Get the WAR file

Docke run-t-I-- name sample ivan/fetcher https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war

3. Build Tomcat7 application server

Create a build environment:

Mkdir tomcat7cd tomcat7touch Dockerfile

Write a Dockerfile:

FROM ubuntu:14.04MAINTAINER Ivan Jiang ENV REFRESHED_AT 2016-07-13 RUN apt-get-yqq updateRUN apt-get-yqq install tomcat7 default-jdk ENV CATALINA_HOME / usr/share/tomcat7ENV CATALINA_BASE / var/lib/tomcat7ENV CATALINA_PID / var/run/tomcat7.pidENV CATALINA_SH / usr/share/tomcat7/bin/catalina.shENV CATALINA_TMPDIR / tmp/tomcat7-tomcat7-tmp RUN mkdir-p $CATALINA_TMPDIR VOLUME ["/ var/lib/tomcat7/webapps/"] EXPOSE 8080 ENTRYPOINT ["/ usr/share/tomcat7/bin/catalina.sh" "run"]

Build an image:

Docker build-t ivan/tomcat7.

4. Run the tomcat7 container

Docker run-- name sample_app-- volumes-from sample-d-P ivan/tomcat7

View the port number mapped to the host:

Docker port sample_app 8080

Assuming that the port number is 49154, you can view the running web app in the browser: IP address: 49154/sample.

In the example, the author uses the command:

Docker inspect-f "{{.volumes}}" sample

Check the host mount location of the sample volume, but this command reports an error.

TimWang on OSChina answered my question by saying that Docker 1.8 removed the Volumes information from the output of docker inspect. You need to use Mounts instead:

Docker inspect-f "{{.Mounts}}" sample

6.3 Multi-container application stack

1. Node.js image

Create a build environment:

Mkdir nodejscd nodejstouch Dockerfile

Write a Dockerfile:

FROM ubuntu:14.04MAINTAINER James Turnbull ENV REFRESHED_AT 2014-06-01RUN apt-get-yqq updateRUN apt-get-yqq install nodejs npmRUN ln-s / usr/bin/nodejs / usr/bin/nodeRUN mkdir-p / var/log/nodeappADD nodeapp/ opt/nodeapp/WORKDIR / opt/nodeappRUN npm installVOLUME ["/ var/log/nodeapp"] EXPOSE 3000ENTRYPOINT ["nodejs", "server.js"]

Establish a soft connection with ln-s, connect the binary modejs to node, and solve some of the original backward compatibility problems on Ubuntu (do not understand. ).

Build an image:

Docker build-t ivan/nodejs.

2. Redis basic image

Mkdir redis_basecd redis_basetouch DockerfileFROM ubuntu:14.04MAINTAINER James Turnbull ENV REFRESHED_AT 2014-06-01RUN apt-get-yqq updateRUN apt-get install-yqq software-properties-common python-software-propertiesRUN add-apt-repository ppa:chris-lea/redis-serverRUN apt-get-yqq updateRUN apt-get-yqq install redis-server redis-toolsVOLUME ["/ var/lib/redis", / var/log/redis "] EXPOSE 6379CMD [] docker build-t ivan/redis.

3. Redis main mirror image

Mkdir redis_primarycd redis_primarytouch DockerfileFROM ivan/redisMAINTAINER James Turnbull ENV REFRESHED_AT 2014-06-01ENTRYPOINT ["redis-server", "--logfile / var/log/redis/redis-server.log"] docker build-t ivan/redis_primary

4. Redis from the mirror

Mkdir redis_replicacd redis_replicatouch DockerfileFROM ivan/redisMAINTAINER James Turnbull ENV REFRESHED_AT 2014-06-01ENTRYPOINT ["redis-server", "- logfile / var/log/redis/redis-replica.log", "--slaveof redis_primary 6379"] docker build-t ivan/redis_replica.

5. Create a Redis cluster

Start the primary image of Redis

Docker run-d-h redis-primary-- name redis_primary ivan/redis_primary

Here, the-h parameter is used to specify the hostname of the container. Redis_primary is given in the book, but the execution will report an error. It should be that the new version of Docker does not support underlined hostnames. Here it is changed to redis-primary.

Start the Redis slave service

Docker run-d-h redis-replical-name redis_replical-link redis_primary:redis_primary ivan/redis_replica

The same treatment is done for the-h parameter here.

6. Create a Node container

Docker run-d-- name nodeapp-p 3000 3000-- link redis_primary:redis_primary ivan/nodejs

Test with the host IP:3000 in the browser.

7. Capture the application log

Use Logstash to capture logs and save them to the log server.

Mkdir logstashcd logstashtouch DockerfileFROM ubuntu:14.04MAINTAINER James Turnbull ENV REFRESHED_AT 2014-06-01RUN apt-get-yqq updateRUN apt-get-yqq install wgetRUN wget-O-http://packages.elasticsearch.org/GPG-KEY-elasticsearch | apt-key add-RUN echo 'deb http://packages.elasticsearch.org/logstash/1.4/debian stable main' > / etc/apt/sources.list.d/logstash.listRUN apt-get-yqq updateRUN apt-get-yqq install logstashADD logstash.conf / etc/WORKDIR / opt/ LogstashENTRYPOINT ["bin/logstash"] CMD ["--config=/etc/logstash.conf"]

Logstash monitors two files / var/log/nodeapp/nodeapp.log and / var/log/redis/redis-server.log to capture new content and output the captured content to standard output. In reality, the contents of Logstash are usually output to the Elasticsearch cluster.

Download the author's logstash.conf to the build root directory before building.

Build an image

Docker build-t ivan/logstash.

Start the container

Docker run-d-name logstash-volumes-from redis_primary-volumes-from nodeapp ivan/logstash

View the output log of the logstash container

Docker logs-f logstash

6.4 manage Docker containers without using SSH

Traditionally, SSH has been used to log in to the running environment or virtual machine management services. In Docker, most containers run only one process, so you can't use this method. However, most administrative operations are done using volumes or links. If you need to signal the container, you can use the docker kill command:

Docker kill-s

This operation sends the specified signal to the container instead of killing the container.

You can log in to the container using nsenter widgets, which are generally available in version 1.2 or earlier, and the introduction of docker exec in version 1.3 replaces most of its functionality.

The tool nsenter can enter the kernel namespace that Docker uses to make up the container. It can enter an existing namespace or execute a process in a new set of namespaces.

You can install nsenter through the Docker container:

Docker run-v / usr/local/bin:/target jpetazzo/nsenter

This will install nsenter into the / usr/local/bin directory.

To use nsenter, you first need to know the process ID of the container you want to enter, and you can use the docker inspect command to get the process PID:

PID=$ (docker inspect-- format {{.State.Pid}})

Then run the following command to enter the container:

Nsenter-- target $PID-- mount-- uts-- ipc-- net-- pid

You can also add commands executed within the container after the nsenter command line:

Nsenter-target $PID-mount-uts-ipc-net-pid ls Thank you for your reading! This is the end of this article on "how to use Docker to build a service". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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