To use memcached in our Docker container, we have to modify the existing Dockerfile a little bit. First it is required to install memcached in the container itself, and then it is required to change the CMD command to start the service and our express application.
1. Create a folder /conf in the project folder
2. Create a file memcached.conf in this folder with the following content:
# Memory: 256 MB
# Port to use
# User for the service
# Listening IP Adress
3. Create a script startup.sh in the newly created folder:
service memcached start
4. Modify the Dockerfile to install memcached
# install memcached
RUN apt-get update
RUN apt-get install -y memcached
5. Copy the startup.sh script and the memcached.conf
COPY ./conf/memcached.conf /etc
COPY ./conf/startup.sh .
RUN chmod +x /usr/src/app/startup.sh
6. Change the CMD to use the startup.sh script
CMD [ "./startup.sh" ]
The reason for the startup script is that in Docker containers the services are disabled by default, so we need to start the service by our own. Also, only one CMD command is allowed, so we have to use a script.