简体   繁体   中英

docker is not found when running a docker command in entrypoint.sh

I'm getting

app_1 |./entrypoint.sh: line 2: docker: command not found

when running this line of code in entrypoint.sh

docker exec -it  fullstacktypescript_database_1 psql -U postgres -c "CREATE DATABASE elitypescript"

How would i properly execute this command?

entrypoint.sh

# entrypoint.sh
docker exec -it  fullstacktypescript_database_1 psql -U postgres -c "CREATE DATABASE elitypescript"
npm run seed # my attempt to run seed first before server kicks in. but doesnt work
npm run server

docker-compose.yml

# docker-compose.yml
version: "3"
services:
  app:
    build: ./server
    depends_on:
      - database
    ports:
      - 5000:5000
    environment:
      PSQL_HOST: database
      PSQL_PORT: 5430
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-elitypescript}
    entrypoint: ["/bin/bash", "./entrypoint.sh"]
  client:
    build: ./client
    image: react_client
    links:
      - app
    working_dir: /home/node/app/client
    volumes:
      - ./:/home/node/app
    ports:
      - 3001:3001
    command: npm run start
    env_file:
      - ./client/.env

  database:
    image: postgres:9.6.8-alpine
    volumes:
      - database:/var/lib/postgresql/data
    ports:
      - 3030:5439

volumes:
  database:

Try this Dockerfile:

FROM node:10.6.0
COPY . /home/app
WORKDIR /home/app
COPY package.json ./
RUN npm install
ENV DOCKERVERSION=18.03.1-ce
RUN curl -fsSLO https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKERVERSION}.tgz \
  && tar xzvf docker-${DOCKERVERSION}.tgz --strip 1 -C /usr/local/bin docker/docker \
  && rm docker-${DOCKERVERSION}.tgz
EXPOSE 5000

You trying to run docker container inside of the docker container. In most cases it is very bad approach and you should to avoid it. But in case if you really need it and if you really understand what are you doing, you have to apply Docker-in-Docker(dind).

As far as I understand you, you need to run script CREATE DATABASE elitypescript , the better option will be to apply sidecar pattern - to run another one container with PostgreSQL client that will run your script.

Link the containers together and connect using the hostname.

# docker-compose
services:
  app:
    links:
    - database
    ...

then just:

# entrypoint.sh
# the database container is available under the hostname database
psql -h database -p 3030 -U postgres -c "CREATE DATABASE elitypescript"

Links are a legacy option, but easier to use then networks.

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