我想运行一个容器:
人们能想到的最简单的事情就是执行以下 Dockerfile:
FROM openjdk:8
RUN apt-get update && apt-get -y install nano
RUN apt-get update && apt-get -y install cron
RUN useradd -u 8877 dockeras
RUN mkdir /home/dockeras
RUN chown -R dockeras /home/dockeras && chmod -R u+rwx /home/dockeras
USER dockeras
CMD ["cron", "-f"]
显然,CMD指令会返回错误,因为cron服务需要由root运行。怎么解决这个问题?
你可以这样实现:Dockerfile 和startup.sh
Dockerfile
FROM openjdk:8
# Install necessary packages
RUN apt-get update && \
apt-get -y install nano cron
# Create a non-root user
RUN useradd -u 8877 dockeras && \
mkdir /home/dockeras && \
chown -R dockeras /home/dockeras && \
chmod -R u+rwx /home/dockeras
# Copy the startup script
COPY startup.sh /home/dockeras/startup.sh
# Set the script as executable
RUN chmod +x /home/dockeras/startup.sh
# Switch to the non-root user
USER dockeras
# Set the entry point to the startup script
ENTRYPOINT ["/home/dockeras/startup.sh"]
启动.sh
#!/bin/bash
# Start the cron service
service cron start
# Run your application service here
# Example:
# java -jar /path/to/your/application.jar
# Keep the container running
tail -f /dev/null