Home > Mobile >  How do I emulate this "docker run" command in a docker-compose.yml?
How do I emulate this "docker run" command in a docker-compose.yml?

Time:01-22

docker run command

I'm using the following command to launch a Docker container.

    docker run -it --rm -d -p 8080:80 \
        -v ~/dev/react-using-camera/.nginx-conf-for-container/conf.d:/etc/nginx/conf.d \
        -v ~/dev/nginx-test-area:/usr/share/nginx/html/ \
        --name web \
        nginx:stable-alpine

The command works satisfactorily:

  • the container launches;
  • nginx responds to requests and
  • the config and web pages used by nginx are those present in the host directories as defined by the '-v' arguments.

Using 'docker-compose up'

I would like to achieve the same effect by using docker-compose up and so I have written the following docker-compose.yml

version: "3.7"
services:
  ingress:
    image: nginx:stable-alpine
    container_name: docker-compose-test-0
    networks:
      - gateway
    ports:
      - '8080:80'
    restart: unless-stopped
    volumes:
      - /home/glaucon/dev/react-using-camera/.nginx-conf-for-container/conf.d:/etc/nginx/conf.d
      - /home/glaucon/dev/nginx-test-area:/usr/share/nginx/html/ \

networks:
  gateway: {}

When I use that file with docker-compose up :

  • the container launches
  • nginx responds to requests but
  • the web content served is not that present in /home/glaucon/dev/nginx-test-area on the host machine, instead the nginx default index.html is served.

Question

How may I emulate the use of the '-v' arguments in my docker run command in the context of docker-compose up ?

CodePudding user response:

The second volume-definition has a trailing backslash:

services:
  ingress:
    ...
    volumes:
      ...
      - /home/glaucon/dev/nginx-test-area:/usr/share/nginx/html/ \

If we remove the trailing backslash:

services:
  ingress:
    ...
    volumes:
      ...
      - /home/glaucon/dev/nginx-test-area:/usr/share/nginx/html/

it should work as expected.

  •  Tags:  
  • Related