Executing Linux shell commands in docker


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.

Supplement: [docker application] execute the specified script in docker (run springboot application under docker)

[docker application] execute the specified script in docker

Here is an example of an application executing spring boot:

1. Create an image file (template) for executing sh script

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
#ENTRYPOINT ["/bin/sh", "-c", "$APP_FILE"]

2. Create script file (script to be executed in container)

echo "test xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
java -jar -Xms500m -Xmx500m -Dspring.profiles.active=test-docker-1 /root/app.jar

3. Make startup script (here is only startup command)

docker run -itd \
 --name test_container \
 --hostname test_container \
 --net test_net --ip \
 --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'

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If there are mistakes or not fully considered, please don’t hesitate to comment.