我想这样做,我可以在下面的代码中运行多个命令:

db:
  image: postgres
web:
  build: .
  command: python manage.py migrate
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db

如何执行多个命令?


当前回答

使用bash-c在docker compose文件中运行多个命令。

command: >
    bash -c "python manage.py makemigrations
    && python manage.py migrate
    && python manage.py runserver 0.0.0.0:8000"

资料来源:https://intellipaat.com/community/19590/docker-run-multiple-commands-using-docker-compose-at-once?show=19597#a19597

其他回答

最干净?

---
version: "2"
services:
  test:
    image: alpine
    entrypoint: ["/bin/sh","-c"]
    command:
    - |
       echo a
       echo b
       echo c

我在尝试将jenkins容器设置为作为jenkins用户构建docker容器时遇到了这个问题。

我需要触摸Dockerfile中的docker.sock文件,因为我稍后将其链接到docker-compose文件中。除非我先接触它,否则它还不存在。这对我有用。

Dockerfile文件:

USER root
RUN apt-get update && \
apt-get -y install apt-transport-https \
ca-certificates \
curl \
software-properties-common && \
curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; 
echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
$(lsb_release -cs) \
stable" && \
apt-get update && \
apt-get -y install docker-ce
RUN groupmod -g 492 docker && \
usermod -aG docker jenkins  && \
touch /var/run/docker.sock && \
chmod 777 /var/run/docker.sock

USER Jenkins

docker-compose.yml文件:

version: '3.3'
services:
jenkins_pipeline:
    build: .
    ports:
      - "8083:8083"
      - "50083:50080"
    volumes:
        - /root/pipeline/jenkins/mount_point_home:/var/jenkins_home
        - /var/run/docker.sock:/var/run/docker.sock

如果您需要运行多个守护进程,Docker文档中建议在非分离模式下使用Supervisord,这样所有子守护进程都将输出到stdout。

从另一个SO问题中,我发现可以将子进程输出重定向到stdout。这样你就能看到所有的输出!

要在Windows容器上运行:

创建.bat文件(使用cmd运行,或者如果容器中有.ps1,则可以使用powershell运行)在命令或入口点中,使用myFile.bat(或myFile.ps1)

贝娄我的码头组合.yml:

version: "3.4"

services:
  myservicename:
    image: mcr.microsoft.com/dotnet/sdk:6.0 
    container_name: mycontainername
    environment:
      - PORT=44390
    command: buildAndRun.bat
[...]

我的buildAndRun.bat:

dotnet --list-sdks
dotnet build
dotnet run

这是我解决这个问题的方法:

services:
  mongo1:
    container_name: mongo1
    image: mongo:4.4.4
    restart: always
#    OPTION 01:
#    command: >
#      bash -c "chmod +x /scripts/rs-init.sh
#      && sh /scripts/rs-init.sh"
#    OPTION 02:
    entrypoint: [ "bash", "-c", "chmod +x /scripts/rs-init.sh && sh /scripts/rs-init.sh"]
    ports:
      - "9042:9042"
    networks:
      - mongo-cluster
    volumes:
      - ~/mongors/data1:/data/db
      - ./rs-init.sh:/scripts/rs-init.sh
      - api_vol:/data/db
    environment:
      *env-vars
    depends_on:
      - mongo2
      - mongo3