Docker Community Forums

Share and learn in the Docker community.

Add depencies / command after deploying a container with docker-compose


(Martinvdm) #1

Hi,

Perhaps a newbie question, but how do i add some dependencies or command to a container after has been bringed up with compose.

i have a container which automatically pulls the image from the web, but after it is started i would like to install a dependencie with “apt-get install”. If i don’t specify this somewhere those depencies are gone when i bring the container down/up.

 containername:
    container_name: name
    image: dockerhub/imagename
    restart: always
    network_mode: host
    volumes:

Can i use the command switch for this? or does this an overwrite of the docker deployment itself? or does this add the command after the container is deployed?

thanks


(David Maze) #2

You want to build your own custom image that adds the dependency. This is pretty straightforward; the Dockerfile would look like

FROM dockerhub/imagename
RUN apt-get update \
 && apt-get install whatever

It should keep the default CMD, EXPOSEd ports, ENVironment variables, etc. from the base image. Your Docker Compose YAML file can then be the very simple

container_name:
  build: .
  restart: always  # not the default setting

(Martinvdm) #3

Great! thanks man. where do i save this docker file? in the same dir as the docker compose file? And witch name?

thanks


(David Maze) #4

I refer you to the excellent official Docker tutorial, Getting Started, Part 2: Containers, which describes the process of building and running custom images.