简体   繁体   中英

docker-compose.yml file errors on running docker-compose up

Here is my docker-compose.yml file:

version:'2':  
services: 
    redis:
        image: redis  
        environment: 
            - HOST='localhost'
            - PORT=6379
        ports: 
            -"0.0.0.0:${PORT}:6379"

I get this error on running docker-compose up:

ERROR: The Compose file './docker-compose.yml' is invalid because:
Invalid service name 'services' - only [a-zA-Z0-9\._\-] characters are allowed
Unsupported config option for services: 'redis'

There are multiple problems with your file. The one causing the syntax error is that you have an extra colon on the first line:

version:'2': 

that way you define a scalar string key version:'2' with value of null . Since you are therefore not defining the version of the docker compose file, the rest of the file (which is version 2 oriented) fails. This is best resolved by adding a space after version:

In addition your ports definition is incorrect, the value for that should be a sequence / list , and you again specify a scalar string -"0.0.0.0:${PORT}:6379" because there is no space after the initial dash.

Change your docker_compose.yaml file to:

version: '2'  
          # ^ no colon here
      # ^ space here
services: 
    redis:
        image: redis  
        environment: 
            - HOST='localhost'
            - PORT=6379
        ports: 
            - "0.0.0.0:${PORT}:6379"
           # ^ extra space here

just remove last character ":" into string version:'2':

after it docker-compose.yml must be like

version:'2'
services: 
redis:
    image: redis  
    environment: 
        - HOST='localhost'
        - PORT=6379
    ports: 
        -"0.0.0.0:${PORT}:6379"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM