docker-compose up for only certain containers

前端 未结 7 567
盖世英雄少女心
盖世英雄少女心 2020-12-22 20:31

I have a docker-compose.yml which contain several containers. Three of them are for my app (client, server and database) and the rest are for various dev tools

相关标签:
7条回答
  • 2020-12-22 20:45

    You usually don't want to do this. With Docker Compose you define services that compose your app. npm and manage.py are just management commands. You don't need a container for them. If you need to, say create your database tables with manage.py, all you have to do is:

    docker-compose run client python manage.py create_db
    

    Think of it as the one-off dynos Heroku uses.

    If you really need to treat these management commands as separate containers (and also use Docker Compose for these), you could create a separate .yml file and start Docker Compose with the following command:

    docker-compose up -f my_custom_docker_compose.yml
    
    0 讨论(0)
  • 2020-12-22 20:46

    Since docker-compose v1.5 it is possible to pass multiple docker-compose.yml files with the -f flag. This allows you to split your dev tools into a separate docker-compose.yml which you then only include on-demand:

    # start and attach to all your essential services
    docker-compose up
    
    # execute a defined command in docker-compose.dev.yml
    docker-compose -f docker-compose.dev.yml run npm update
    
    # if your command depends_on a service you need to include both configs
    docker-compose -f docker-compose.yml -f docker-compose.dev.yml run npm update
    

    For an in-depth discussion on this see docker/compose#1896.

    Update

    The compose-spec introduced a new profiles attribute for services which will make such use cases much easier in the future. Once this lands in docker-compose you will be able to do this:

    services:
      client:
        # ...
      db:
        # ...
      npm:
        profiles: ["cli-only"]
        # ...
    
    docker-compose up # start main services, no npm
    docker-compose run --rm npm
    

    There also is already an open Pull Request to implement this in docker-compose.

    0 讨论(0)
  • 2020-12-22 20:47

    One good solution is to run only desired services like this:

    docker-compose up --build $(<services.txt)
    

    and services.txt file look like this:

    services1 services2, etc
    

    of course if dependancy (depends_on), need to run related services together.

    --build is optional, just for example.

    0 讨论(0)
  • 2020-12-22 20:49

    To start a particular service defined in your docker-compose file. for example if your have a docker-compose.yml

    sudo docker-compose start db  
    

    given a compose file like as:

    version: '3.3'
    
    services:
       db:
         image: mysql:5.7
         ports:
           - "3306:3306"
         volumes:
           - ./db_data:/var/lib/mysql
         restart: always
         environment:
           MYSQL_ROOT_PASSWORD: yourPassword
           MYSQL_DATABASE: wordpress
           MYSQL_USER: wordpress
           MYSQL_PASSWORD: yourPassword
    
       wordpress:
         depends_on:
           - db
         image: wordpress:latest
         ports:
           - "80:80"
         volumes:
           - ./l3html:/var/www/html
         restart: always
         environment:
           WORDPRESS_DB_HOST: db:3306
           WORDPRESS_DB_USER: wordpress
           WORDPRESS_DB_PASSWORD: yourPassword
    volumes:
        db_data:
        l3html:
    

    Some times you want to start mySQL only (sometimes you just want to populate a database) before you start your entire suite.

    0 讨论(0)
  • 2020-12-22 20:54

    You can start containers by using:

    $ docker-compose up -d client
    

    This will run containers in the background and output will be avaiable from

    $ docker-compose logs
    

    and it will consist of all your started containers

    0 讨论(0)
  • 2020-12-22 21:04

    I actually had a very similar challenge on my current project. That broght me to the idea of writing a small script which I called docker-compose-profile (or short: dcp). I published this today on GitLab as docker-compose-profile. So in short: I now can start several predefined docker-compose profiles using a command like dcp -p some-services "up -d". Feel free to try it out and give some feedback or suggestions for further improvements.

    0 讨论(0)
提交回复
热议问题