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 Memcached Docker container

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

Share

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

This article focuses on "how to build a Memcached Docker container". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to build Memcached Docker containers.

How do I run Memcached into a docker container?

Docker

Docker provides a running environment for containers (applications). Using Docker images to create containers, you can either execute commands manually or visually operate through the cSphere platform.

Introduction to Memcached

Memcached is a distributed, open source data storage engine. It is designed to store specific types of data in RAM (replacing low-speed traditional hard drives) for quick retrieval by applications. Reduce the time it takes to process applications and offset heavy and slow data sets or API by reducing the number of queries, such as traditional databases (MySQL, etc.).

By introducing a smart, well-designed and optimized caching mechanism, it can handle more requests and execute more programs. This is the most important application example of Memcached because it caches other applications or content in the same way.

Can be deeply dependent on and used in the production of websites or other applications, Memcached has become an instant performance improvement tool without using better hardware conditions (such as more servers or service resources).

Memcached works by storing keywords and their corresponding values (up to 1MB) in an association matrix (such as a hash table), extended and distributed across a large number of virtual servers.

Start creating a Memcached image

Based on what we learned in the Docker series, we went straight to creating Dockerfile to automatically build and install the image of the Mamcached function (which will be used to run sandboxed Memcached instances).

Quick Review: what is Dockerfile?

Dockerfile is a script containing executable declaration commands that will be executed in a given order to let Docker automatically create a new Docker image. This is of great help to the deployment.

These files (Dockerfile) use the FROM command and always start with a description of the underlying image. From then on, the build process starts running, and each step of submitting (saving the state of the mirror) to the host forms the final mirror.

Usage:

# Build an image using the Dockerfile at current location

# Tag the final image with [name] (e.g. * nginx*)

# Example: sudo docker build-t [name].

Sudo docker build-t memcached_img.

Create a Dockerfile of a Memcached image

Create a new Dockerfile by becoming familiar with the text editor:

First, let's define the goal of Dockerfile and declare the underlying image we need to use.

# # #

# Dockerfile to run Memcached Containers

# Based on Ubuntu Image

# # #

# Set the base image to use to Ubuntu

FROM ubuntu

# Set the file maintainer (your name-the file's author)

MAINTAINER cSphere

Then we can start installing Memcached

# Install Memcached

RUN apt-get install-y memcached

Set the container port that is open by default:

# Port to expose (default: 11211)

EXPOSE 11211

Set the default execution command and entry (for example, the Memcached process):

# Set the user to run Memcached daemon

USER daemon

# Set the entrypoint to memcached binary

ENTRYPOINT memcached

# Default Memcached run command arguments

CMD ["- u", "root", "- m", "128"]

# final Dockfile

# # #

# Dockerfile to run Memcached Containers

# Based on Ubuntu Image

# # #

# Set the base image to use to Ubuntu

FROM ubuntu

# Set the file maintainer (your name-the file's author)

MAINTAINER Maintaner Name

# Install Memcached

RUN apt-get install-y memcached

# Port to expose (default: 11211)

EXPOSE 11211

# Set the user to run Memcached daemon

USER daemon

# Set the entrypoint to memcached binary

ENTRYPOINT memcached

# Default Memcached run command arguments

CMD ["- m", "128"]

Dockerfile, get ready!

Create a Memcached container

Build a memcached image: "csphere-memcached"

Sudo docker build-t csphere-memcached.

* * Note**: Don't leave out the last "." Docker needs it to find Dockerfile.

Start the memcached container

Use the following command to create a new container, which can be modified to suit your needs.

# sudo docker run-name csphere-memcached-d-p 45001range 11211 csphere-memcached

The "csphere-memcached" container, which has been started, can be used using port 45001 connection.

Limit the memory of the Memcached container

If you want to limit the amount of memory a Docker container process can use, simply set `- m [memory amount]` and mark the limit on ok.

Run a container with memory limits of 256MB:

`# sudo docker run-name csphere-memcached-m 256m-d-p 45001pur11211 csphere- memcached`

Check that the container memory limit is set successfully and execute the following command:

`# Example: docker inspect [container ID] | grep Memory

Sudo docker inspect csphere-memcached | grep memory`

Test the Memcached container

We use a simple Python CLI program to test.

Make sure your host has the necessary library files for Python/Memcached:

`sudo apt-get update & & sudo apt-get-y upgrade

Sudo apt-get install-y python-pip

Pip install python- Memcached`

Create a simple Python script called cache.py

Copy and paste the following:

`# Import python-memcache and sys for arguments

Import memcache

Import sys

# Set address to access the Memcached instance

Addr = 'localhost'

# Get number of arguments

# Expected format: python cache.py [memcached port] [key] [value]

Len_argv = len (sys.argv)

# At least the port number and a key must be supplied

If len_argv < 3:

Sys.exit ("Not enough arguments.")

# Port is supplied and a key is supplied-let's connect!

Port = sys.argv [1]

Cache = memcache.Client (["{0}: {1}" .format (addr, port)])

# Get the key

Key = str (sys.argv [2])

# If a value is also supplied, set the key-value pair

If len_argv = = 4:

Value = str (sys.argv [3])

Cache.set (key, value)

Print "Value for {0} set!" .format (key)

# If a value is not supplied, return the value for the key

Else:

Value = cache.get (key)

Print "Value for {0} is {1}." .format (key, value) `

Test the Memcached instance of Docker:

# Example: python cache.py [port] [key] [value]

Python cache.py 45001 my_test_key test_value

# Return: Value for my_test_key set

# See if the key is set:

Python cache.py 45001 my_test_key

# Return: Value for my_test_key is test_value.

At this point, I believe you have a deeper understanding of "how to build a Memcached Docker container". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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