Dockerfile example to build a Ubuntu image with basic packages


  • administrators

    Dockerfile example to build a Ubuntu Server with Python, Flask, vim, curl, wget and MongoDB already.

    Just copy and paste this script and name it dockerfile . All the source are now in git:

    https://github.com/loitd/dockertest

    Save the file in any directory (/dockersample for example). Now in this folder, you can build image with this command:

    docker build -t loitd/ubuntubase

    The Dockerfile example

    # Build & push and run commands
    # docker build -t loitd/ubuntubase
    # docker push loitd/ubuntubase
    # docker run -p 5000:5000 -v $PWD/app:/app --name docker1 -it --rm loitd/ubuntubase
    
    # from a repo. Recommend debian
    FROM loitd/ubuntubase:latest
    
    # set env
    ENV abc=hello
    MAINTAINER Loitd Name <"loitranduc@gmail.com">
    RUN apt-get update -y && apt-get install -y python-pip python-dev build-essential vim curl wget python3.5 lsb-release
    #RUN apt-get install -y vim curl wget python3.5 lsb-release
    
    ###install MongoDB
    #Step 1:  Import the MongoDB public key 
    RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
    
    #Step 2: Generate a file with the MongoDB repository url
    # to install mongodb 3.0
    #RUN echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list
    # To install mongodb 2.6
    RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
    
    #Step 3: Refresh the local database with the packages
    RUN apt-get update && apt-get install -y mongodb-org
    
    RUN mkdir -p /data/db
    
    # Even exposed, you still need to map ports for your local can reach the services.
    # Expose port 27017 - MongoDB from the container to the host
    EXPOSE 27017
    # expose port 5000 for flask
    EXPOSE 5000
    # copy or add the same but copy is prefered. copy local files into the container
    COPY . /app
    # volume: to expose any storage, config, file/folders created by docker container.
    # VOLUME ["/data"] 
    VOLUME /root/dockerapp:/app
    # set working dir
    WORKDIR /app
    
    # Run some pip setup for Python
    RUN pip install --upgrade pip
    RUN pip install -r requirements.txt
    RUN pip install virtualenv
    # set image main command. docker run python
    #ENTRYPOINT ["python"]
    # CMD ["exe", "param1", "param2"]
    #CMD ["app.py"]
    * list item
    

    And finally we’ve got our product like this.

    alt text

    Feel free to pull the image from docker hub as I’ve made it public.

    Running MongoDB on Docker

    After running the docker image with command:

    docker run -p 5000:5000 -p 27017:27017 -v $PWD/app:/app --name docker1 -it --rm loitd/ubuntubase
    

    You will be access to docker console. MongoDB now can run with command:

    /usr/bin/mongod
    

Hãy đăng nhập để trả lời
 

Có vẻ như bạn đã mất kết nối tới Cộng đồng Python Việt Nam, vui lòng đợi một lúc để chúng tôi thử kết nối lại.