简体   繁体   中英

Installing software in a Docker container interactively

I want to create a Docker container for my scientific computing project. For this project I need to experiment with some dependencies, so it would be efficient if I access the shell of the container and install various packages and then choose some of them. Also, I want different people to reproduce my work and understand how I setup the environment. So, if possible, I want to make a file which is like a recipe for reproducing the same container. Should I just handwrite a text file listing what dependencies I chose at the end, or is there a tool in Docker which records automatically what packages are installed in the container after its creation?

I am not aware of any Docker tool to save the list of dependencies you install automatically. To make it easier to reproduce your exact container, you should follow the advice of @lastr2d2: record the packages you install and convert to a RUN apt-get install -y package1 package2... line in your Dockerfile. You could also use apt list --installed to list what you have installed, although you'll get a LOT of other packages you didn't manually install as well.

An alternative way would be to use docker commit to save the state of a running container and pushing the image to Docker Hub. The commit commands will create a Docker image of the current state of your container. These docs provide details about this command.

Important Note: If you choose to do this, please read the warnings about using docker commit . In this answer , the author points out you should not use docker commit at all because the container isn't completely reproducible.

If you've read this warning and still want to use docker commit, here's an example:

# run the container to install packages
$ docker run -it --name container-to-commit ubuntu:20.04 /bin/bash

## inside Docker terminal session
# install packages here
$ apt-get update
$ apt-get install gcc ...

To save the container, you need to keep it running and run docker commit CONTAINER_ID in another terminal. For example:

$ docker ps
CONTAINER ID   IMAGE            COMMAND     CREATED          STATUS          PORTS     NAMES
a26092c038ab   ubuntu:20.04   "/bin/bash"   56 seconds ago   Up 55 seconds             container-to-commit

# commit the image with the label "username/container-name"
# username should be your Docker Hub username if you choose to distribute on Docker Hub
$ docker commit a26092c038ab username/container-name:v1

$ docker images
REPOSITORY                   TAG       IMAGE ID       CREATED         SIZE
username/container-name       v1     4752ae644acf   5 seconds ago     523MB

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