Skip to content

Instantly share code, notes, and snippets.

@DanNYSPD
Forked from glamp/modify-docker-image.md
Created October 30, 2020 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanNYSPD/ea43659388e12cb85f0418a67b658f13 to your computer and use it in GitHub Desktop.
Save DanNYSPD/ea43659388e12cb85f0418a67b658f13 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment