Execute the operation of linux shell command in docker

  • 2021-10-25 08:20:17
  • OfStack

To execute the shell command in docker, you need to add sh-c before the command, for example:


docker run ubuntu sh -c 'cat /data/a.txt > b.txt'

Otherwise, the instruction cannot be parsed normally.

Added: "Docker application" executes the specified script in docker (springboot application runs under docker)

"Docker Application" Executes the specified script in docker

Here is an example of an application that executes spring boot:

1. Create a mirror file (template) for executing sh scripts


Dockfile
FROM vertigomedia/ubuntu-jdk8
RUN touch /root/app_start.sh
RUN echo "#!/bin/bash" > /root/app_start.sh
RUN echo "echo 111" >> /root/app_start.sh
RUN chmod a+x /root/app_start.sh
ENV TZ 'Asia/Shanghai'
ENV APP_FILE /root/app_start.sh
EXPOSE 8889
CMD $APP_FILE
#ENTRYPOINT ["/bin/sh", "-c", "$APP_FILE"]

2. Make a script file (the script to be executed in the container)


container.sh
#!/bin/bash
echo "test xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
java -jar -Xms500m -Xmx500m -Dspring.profiles.active=test-docker-1 /root/app.jar

3. Make a startup script (here just the startup command)


docker run -itd \
 --name test_container \
 --hostname test_container \
 --net test_net --ip 170.170.1.199 \
 --volume /root/container.sh:/root/app_start.sh \
 --volume /opt/test-1.0.0-SNAPSHOT.jar:/root/app.jar \
 --privileged=true \
 test:123 /bin/bash -c 'sh /root/app_start.sh'

Related articles: