Docker Community Forums

Share and learn in the Docker community.

How to add a new application to a docker image

Hello,
I have an docker image. And I would like to add some package to it.
I managed to do it with the following command:
docker exec -it artemis-server apk install graphviz

I have to run this command every time I stat a contianier.
how to do in permanently way to have this package in the image?

Hi :slight_smile:

You need to make your own image, based on the one you’re running with docker run…

An example of this:
Create a file named: dockerfile with content:

FROM the-image-you-want-to-build-from:tag
RUN apk install graphviz

Then build it:

docker build -t yourimage:latest .

Then you’re able to use that image as normal:

docker run -tid ... yourimage:latest

Thank you Martin for your answer.
I have a following situation. A docker image is created by “docker compose up” command which reads the “docker-compose.yml” config file. here is it’s content:

  services:
    artemis-server:
      command: sh -c "./gradlew buildJarForDocker && java -jar build/libs/Artemis-*.jar"
      image: openjdk:16-jdk-alpine
      environment:
         - SPRING_DATASOURCE_URL=jdbc:mysql://artemis-mysql:3306 
      ...

I would like to add “apk install graphviz” command.

How to add this in yml file?

The correct way is still my approach.

what you can do, is create the dockerfile as:

FROM openjdk:16-jdk-alpine
RUN apk install graphviz

Change your compose to use the dockerfile via the build: arg:

  services:
    artemis-server:
      command: sh -c "./gradlew buildJarForDocker && java -jar build/libs/Artemis-*.jar"
      image: yourimage:latest
      build: .
      environment:
         - SPRING_DATASOURCE_URL=jdbc:mysql://artemis-mysql:3306 

(the . in build just tells that the dockerfile is in the same directory as the docker-compose file)

now, when you run: docker-compose up -d
it will see that the image you defined, does not exist, but it can see the build: tag, and will then build the image with you apk install… and then run the container with that image.

hope that makes sense

It works fine now :slight_smile:
Thank you very much

1 Like