Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install multiple packages using apt-get via a Dockerfile

Tags:

docker

So I am trying to make a basic Dockerfile, but when I run this it says

The command bin/sh -c sudo apt-get install git python-yaml python-jinja2 returned a non-zero code: 1

My question is what am I doing wrong here, and is it even allowed to do commands like 'cd' and 'source' from the Dockerfile?

FROM Ubuntu
MAINTAINER example

#install and source ansible
RUN sudo apt-get update
RUN sudo apt-get install git python-yaml python-jinja2 python-pycurl
RUN sudo git clone https://github.com/ansible/ansible.git
RUN sudo cd ansible
RUN sudo source ./hacking/env-setup
like image 316
stephan Avatar asked Oct 26 '16 22:10

stephan


People also ask

Which Dockerfile instruction can be used to install packages?

To install packages in a docker container, the packages should be defined in the Dockerfile. If you want to install packages in the Container, use the RUN statement followed by exact download command . You can update the Dockerfile with latest list of packages at anytime and build again to create new image out of it.

Can I run 2 commands in Dockerfile?

Apart from the Dockerfile CMD directive and the docker run command, we can execute multiple commands with the docker-compose tool.


1 Answers

Couple of pointers / comments here:

  • It's ubuntu not Ubuntu
  • From base ubuntu (and unfortunately, a lot of images) you don't need to use sudo, the default user is root (and in ubuntu, sudo is not included anyway)
  • You want your apt-get update and apt-get install to be RUN as part of the same command, to prevent issues with Docker's layer cache
  • You need to use the -y flag to apt-get install as the Docker build process runs non-interactively
  • Few other points I could make about clearing up your apt-cache and other non-required artifacts after running the commands, but this should be enough to get going on

New Dockerfile (taking into account the above) would look something like:

FROM ubuntu
MAINTAINER example

#install and source ansible
RUN apt-get update && apt-get install -y \
    git \
    python-yaml \
    python-jinja2 \
    python-pycurl

RUN git clone https://github.com/ansible/ansible.git
WORKDIR ansible/hacking
RUN chmod +x env-setup; sync \
    && ./env-setup

You might also find it useful to read the Dockerfile best practises.

Edit: Larsks answer also makes some useful points about the state of the container not persisting between layers so you should go upvote him too!

like image 177
johnharris85 Avatar answered Sep 30 '22 02:09

johnharris85