Executing Linux shell commands in docker

Time:2022-5-2

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


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. Create script file (script to be executed in container)


container.sh
#!/bin/bash
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 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'

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.