29

i've been using a docker container to build the chromium browser (building for Android on Debian 10). I've already created a Dockerfile that contains most of the packages I need.

Now, after building and running the container, I followed the instructions, which asked me to execute an install script (./build/install-build-deps-android.sh). In this script multiple apt install commands are executed.

My question now is, is there a way to install these packages without rebuilding the container? Downloading and building it took rather long, plus rebuilding a container each time a new package is required seems kind of suboptimal. The error I get when executing the install script is:

./build/install-build-deps-android.sh: line 21: lsb_release: command not found

(I guess there will be multiple missing packages). And using apt will give:

root@677e294147dd:/android-build/chromium/src# apt install nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package nginx

(nginx just as an example install).

I'm thankfull for any hints, as I could only find guides that use the Dockerfile to install packages.

2
  • 1
    You may find the exec command helpful docs.docker.com/engine/reference/commandline/exec/…, it allows you to run a command in already running container. Commented Jul 22, 2020 at 5:32
  • 1
    Remember that any changes you make in a running container will be lost as soon as the container exits, and you need to delete and recreate containers for some pretty routine Docker-level settings changes. I'd consider changing the Dockerfile (and committing it to source control) better practice. Commented Jul 22, 2020 at 10:54

3 Answers 3

33

You can use docker commit:

  1. Start your container sudo docker run IMAGE_NAME
  2. Access your container using bash: sudo docker exec -it CONTAINER_ID bash
  3. Install whatever you need inside the container
  4. Exit container's bash
  5. Commit your changes: sudo docker commit CONTAINER_ID NEW_IMAGE_NAME

If you run now docker images, you will see NEW_IMAGE_NAME listed under your local images.

Next time, when starting the docker container, use the new docker image you just created:
sudo docker run **NEW_IMAGE_NAME** - this one will include your additional installations.

Answer based on the following tutorial: How to commit changes to docker image

Sign up to request clarification or add additional context in comments.

Comments

16

Thanks for @adnanmuttaleb and @David Maze (unfortunately, they only replied, so I cannot accept their answers).

What I did was to edit the Dockerfile for any later updates (which already happened), and use the exec command to install the needed dependencies from outside the container. Also remember to

apt update

otherwise you cannot find anything...

2 Comments

This did the trick. Thank you. This can be marked as the correct answer
Yes, this is what I needed: apt update. None of the other answers mentioned that. You should mark it as the answer.
8

A slight variation of the steps suggested by Arye that worked better for me:

  1. Create container from image and access in interactive mode: docker run -it IMAGE_NAME bin/bash
  2. Modify container as desired
  3. Leave container: exit
  4. List launched containers: docker ps -a and copy the ID of the container just modified
  5. Save to a new image: docker commit CONTAINER_ID NEW_IMAGE_NAME

If you haven't followed the Post-installation steps for Linux , you might have to prefix Docker commands with sudo.

2 Comments

Fewer steps than from arye and the hint to use "docker ps -a" to get the container id was what I was missing. Thanks.
Be sure to not include the -rm option in the command (something we normally do) or the modified image will be removed before you can commit it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.