Install Memcached Inside Docker Container in Linux

Install Memcached Inside Docker Container in Linux

Memcached is an in-memory key-value data store. It is often used for caching data received from a database or API endpoint. It can speed up web applications. Memcached is an open-source project which released under Revised BSD license.

This tutorial explains how to install Memcached inside a Docker container in the Linux. Commands have been tested on Ubuntu.

Prepare environment

Make sure you have installed Docker in your system. If you are using Ubuntu, installation instructions can be found in the post.

Install Memcached

  • Host network

Run the following command to create a container for Memcached that uses host network:

docker run -d --name=memcached --restart=always --network=host \
    memcached
  • User-defined bridge network

User-defined bridge network can be used for listening on different port. By default, Memcached service is listening on port 11211. It can be changed with -p option.

docker network create app-net
docker run -d --name=memcached --restart=always --network=app-net \
    -p 8080:11211 \
    memcached

Testing Memcached

The telnet command can be used to test Memcached connectivity. Run the following command to connect to Memcached:

telnet 127.0.0.1 11211

Once connected, type stats command and press Enter. To exit telnet, type quit. Output example:

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
STAT pid 1
STAT uptime 2324
STAT time 1670140750
...
STAT lru_bumps_dropped 0
END
quit
Connection closed by foreign host.

Uninstall Memcached

To completely remove Memcached, remove its container:

docker rm --force memcached

Remove Memcached image:

docker rmi memcached

If a user-defined bridge network was created, you can delete it as follows:

docker network rm app-net

Leave a Comment

Cancel reply

Your email address will not be published.