Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying a docker image that was created from an existing one

Tags:

docker

By following a how-to, I've created a new image based on an existing one.
I don´t have the original images' Dockerfile and there are things happening when the container starts that I can't change - that´s how it seems anyway.

Is there a way to modify the commands in the Dockerfile of the base image?

For example the container runs a bash script when it starts, I want to change this.

like image 737
michabbb Avatar asked Nov 24 '14 12:11

michabbb


People also ask

Can a docker image be modified?

Docker images can now be edited simply and reliably. This is an example of a Dockerfile for a Zabbix monitoring container. To change the image used by an existing container, we must first delete it, then edit the Docker file to make the necessary changes, and then recreate the container using the new file.

Can you combine 2 docker images?

Docker doesn't do merges of the images, but there isn't anything stopping you combining the dockerfiles if available, and rolling into them into a fat image which you'd need to build.


3 Answers

To answer your specific q: "the container runs a bash script when it starts, i want to change this". Let's assume you want to run /script.sh (part of the image) instead of the default, you can instantiate a container using:

docker run --entrypoint /script.sh repo/image

If script.sh isn't part of the image and/or you prefer not having to specify it explicitly each time with --entrypoint as above, you can prepare an image that contains and runs your own script.sh:

  1. Create an empty directory and copy or create script.sh in it
  2. Create Dockerfile with following content:

    FROM repo/image
    ADD script.sh /
    ENTRYPOINT /script.sh
    
  3. docker build -t="myimage" .

  4. docker run myimage

Notes:

  • When running the container (step 4), it's no longer necessary to specify --entrypoint since we have it defaulted in the Dockerfile.
  • It's really that simple; no need to sign up to docker hub or any such thing (although it's of course recommended in time ;-)
like image 116
sxc731 Avatar answered Oct 16 '22 11:10

sxc731


Just create a new Dockerfile in an empty Directory. Start the Dockerfile with

FROM repo/image 

where 'repo/image' is the id of the image your are currently using.

and add your customizations below.

This way you build a new image that is based on another image.

In particular, to change the command that runs at startup, put in a CMD and/or ENTRYPOINT line.

like image 25
Günter Zöchbauer Avatar answered Oct 16 '22 11:10

Günter Zöchbauer


If launching the container with the command from the tutorial fails, your base image most likely introduced an ENTRYPOINT. Any commands you specify for docker run will be appended as options to the ENTRYPOINT command. Thus, given an ENTRYPOINT of e.g. /usr/bin/somescript,

docker run -d --name newguest -p 8080:80 mymod/httpd:v1 /usr/sbin/httpd -D FOREGROUND

will cause the container to execute

/usr/bin/somescript /usr/sbin/httpd -D FOREGROUND

instead of

/usr/sbin/httpd -D FOREGROUND

To get rid of the ENTRYPOINT, use the --entrypoint option:

docker run --entrypoint "" -d --name newguest -p 8080:80 mymod/httpd:v1 /usr/sbin/httpd -D FOREGROUND
like image 27
Harald Albers Avatar answered Oct 16 '22 11:10

Harald Albers