简体   繁体   中英

How to build an docker image using dockerfile which includes databases creation

I am trying to build docker image using docker file . The docker file will contain database creation.

FROM ubuntu:18.04

RUN apt-get update && apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get update && apt-get install -y nodejs
RUN node -v
RUN npm -v

RUN apt-get install -y redis-server
RUN redis-server -v

RUN apt-get install -my wget gnupg

RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv D68FA50FEA312927

RUN echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list

RUN apt-get update
RUN apt-get install -y mongodb-org
RUN mongodb -version


I am unable to start redis server after installation in docker container

You should to expose ports for redis:

EXPOSE 6379

And please remember, that each RUN creates a new layer in your image, and you can group all shell commands in one RUN directive. It should be something like this:

FROM ubuntu:18.04

RUN apt-get update && apt-get install -y curl wget gnupg && \
    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv D68FA50FEA312927 && \
    echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list && \
    curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    apt-get update && \
    apt-get install -y nodejs redis-server mongodb-org redis-server && \
    node -v && \
    npm -v && \
    mongodb -version

EXPOSE 6379

And one more thing. Docker way tell us run only one process in one container, so you should to separete your Redis, Mongo and other apps to different containers and run it with some orchestrator(such as docker-swarm or node or kubernetes) or just docker-compose.

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