Skip to content

Instantly share code, notes, and snippets.

@glamp
Created July 23, 2015 20:19
Show Gist options
  • Save glamp/74188691c91d52770807 to your computer and use it in GitHub Desktop.
Save glamp/74188691c91d52770807 to your computer and use it in GitHub Desktop.

Modifying an Existing Docker Image

To install a custom package or modify an existing docker image we need to

  1. run a docker a container from the image we wish to modify
  2. modify the docker container
  3. commit the changes to the container as a docker image
  4. test changes made to image

1.) Running a docker container from an image

The command to do this is,

docker run -it yhat/scienceops-python:0.0.2 /bin/bash
  • The -i tells docker to attach stdin to the container

  • The -t tells docker to give us a pseudo-terminal

  • /bin/bash will run a terminal process in your container

2.) Modify the docker container

Once we are in our container we can install package(s), and set environment variables

$ sudo apt-get install vim
$ export AWS_SECRET_KEY=mysecretkey123
$ export AWS_ACCESS_KEY=fooKey

When you are done modifying your container you must exit by running the exit command. Once we exit the container, we need to find the container ID by running

docker ps -a

3.) Commit the changes to the container as a new image

Copy the container ID for the container you just modified, and then run the docker commit command to commit changes to your container as an image.

docker commit [options] [container ID] [repository:tag]

An example docker commit command is the following.

docker commit e8f0671518a2 yhat/scienceops-python:0.0.2

Note here! You must commit the changes with the same tags as the scienceops image on your system. To see your new image run.

docker images

4.) Test changes made to image

To test your changes when adding an environment variable run the test command

$ docker run -it yhat/scienceops-python:0.0.2 echo $AWS_SECRET_KEY
@yanggeorge
Copy link

thanks

@yixizhang
Copy link

that run /bin/bash trick is genius.

btw if you wanna fast iteration when test changes, and the change is in code, try mount to local filesystem with the -v option in docker run.

@korenlev
Copy link

what if your container can not run and you need to edit the image to fix it ? a BIG unresolved issue in docker ;-)

@peterd3270
Copy link

/bin/bash will only work if bash is included in the image. If it doesn/t work, /bin/sh is worth trying (but remember that the resulting shell session probably isn't bash, and may not have the same facilities.)

@RafaelRuales
Copy link

thanks

@sully-27
Copy link

moby/moby#26479 - Please update with this info (cost me 4 hours)

@azmanhamid
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment