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 -m 256 # Port to use -p 11211 # User for the service -u memcache # Listening IP Adress -l 127.0.0.1
3. Create a script startup.sh in the newly created folder:
#!/bin/bash service memcached start npm 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.