Docker撰写标志

时间:2019-01-17 12:26:54

标签: docker docker-compose

我没有办法运行与我的代码等效的docker-compose

docker run -d --name=server --restart=always --net network --ip 172.18.0.5 -p 5003:80 -v $APP_PHOTO_DIR:/app/mysql-data -v $APP_CONFIG_DIR:/app/config webserver

我已经做到了:

version: '3'
services:
  server:
    image: app-dependencies
    ports:
     - "5003:80"
    volumes:
      - ./app:/app
    command:  python /app/app.py
    restart: always
    networks:
      app_net:
        ipv4_address: 172.18.0.5

2 个答案:

答案 0 :(得分:0)

您确定容器需要IP地址吗?不推荐这样做,为什么要显式设置它?

docker-compose.yml

version: '3'
services:
  server:   # correct, this would be container's name
    image: webserver  # this should be image name from your command line
    ports:
     - "5003:80"  # correct, but only if you need to communicate to service from ouside
    volumes:  # volumes just repeat you command line, you can use Env vars
      - $APP_PHOTO_DIR:/app/mysql-data
      - $APP_CONFIG_DIR:/app/config
    command:  ["python", "/app/app.py"]  # JSON notation strongly recommended
    restart: always

然后docker-compose up -d就是这样。您可以使用localhost:5003从主机访问服务,而无需内部IP。

答案 1 :(得分:0)

对于网络,我总是在docker-compose文件中包括网络规范。如果网络已经存在,则docker将不会创建新的网络。

version: '3'
services:
   server:
      image: app-dependencies
   ports:
      - "5003:80"
   volumes:
      - ./app:/app
   command:  python /app/app.py
   restart: always
   networks:
     app_net:
       ipv4_address: 172.18.0.5

networks:
  app_net:
     name: NETWORK_NAME
     driver: bridge
     ipam:
       config:
          - subnet: NETWORK_SUBNET

volumes:
   VOLUME_NAME:
      driver:local

您将需要单独添加卷以匹配docker run命令。