简体   繁体   中英

docker build an image with httpd and memcached installed, but when run a container with this image, memcached not start

I built an image with httpd and memcached installed, I expect that when I launch a container with this image, memcached and httpd were both started.

Here is my dockerfile

FROM amazonlinux:2
MAINTAINER tian

# Install necessary commands
RUN yum update -y
RUN yum install tar -y
RUN yum install unzip -y
RUN yum install wget -y
RUN yum install -y procps
RUN yum install telnet -y

#install apache httpd
RUN yum install httpd -y

# Install memcached and libmemcached in Amazon Linux 2
RUN yum install -y libevent libevent-devel
RUN yum install -y gcc make
RUN wget http://www.memcached.org/files/memcached-1.4.24.tar.gz
RUN tar xvzf memcached-1.4.24.tar.gz
RUN /memcached-1.4.24/configure --enable-64bit
RUN make && make install

CMD ["-D", "FOREGROUND"]
#start httpd, work
ENTRYPOINT ["/usr/sbin/httpd"]
#start memcached, not work
ENTRYPOINT ["memcached","-u","nobody","-m","512","-p","11211","127.0.0.1", "start"]
EXPOSE 80

build image

docker build -t demo ./ --ulimit nofile=20480:40960 --ulimit nproc=1024:2048

launch a container with this image, go into shell

PS D:\Docker> docker run -d -p 80:80 demo
8619a58a948893916d4a70fcdb89ca2f6c1384a17e29b50a45a4f358fae38afb
PS D:\Docker> docker exec -it 8619a58a948893916 /bin/bash
bash-4.2# ps -ef|grep httpd
root         1     0  0 13:59 ?        00:00:00 /usr/sbin/httpd -D FOREGROUND
apache       6     1  0 13:59 ?        00:00:00 /usr/sbin/httpd -D FOREGROUND
apache       7     1  0 13:59 ?        00:00:00 /usr/sbin/httpd -D FOREGROUND
apache       9     1  0 13:59 ?        00:00:00 /usr/sbin/httpd -D FOREGROUND
apache      14     1  0 13:59 ?        00:00:00 /usr/sbin/httpd -D FOREGROUND
apache      15     1  0 13:59 ?        00:00:00 /usr/sbin/httpd -D FOREGROUND
root        60    52  0 14:00 pts/0    00:00:00 grep httpd
bash-4.2# ps -ef|grep memcached
root        62    52  0 14:00 pts/0    00:00:00 grep memcached
bash-4.2# memcached -d -u nobody -m 512 -p 11211 127.0.0.1 start
bash-4.2# ps -ef|grep memcached
nobody      72     1  0 14:06 ?        00:00:00 memcached -d -u nobody -m 512 -p 11211 127.0.0.1 start
root        79    52  0 14:06 pts/0    00:00:00 grep memcached
bash-4.2#

you can see that httpd was automatically started, but memcached was not started until I manually ran memcached -d -u nobody -m 512 -p 11211 127.0.0.1 start

I also try redhat7, not work, too

FROM registry.access.redhat.com/ubi7/ubi:latest
MAINTAINER tian
# Install necessary commands
RUN yum update -y
RUN yum install unzip -y
RUN yum install httpd -y
RUN yum install wget -y
RUN yum install telnet -y


# Install memcached and libmemcached in RHEL 7
RUN yum install memcached libmemcached -y

# can not start memcached
#RUN systemctl enable memcached.service
#RUN systemctl start memcached.service

CMD ["-D", "FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]
EXPOSE 80

How can I make memcached automatically started, too?

Thanks

Docker is meant to run 1 service only. So usually you'll want a container for your each of the 2 services (httpd + memcached).

If you really need it, you can use a service manager script that controls several services in a single container (see https://docs.docker.com/config/containers/multi-service_container/ ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM