Docker

How to run commands on docker container ?

You can use below command commands on docker container

docker exec <container> apt-get install ...

Can I have some commands on docker container which executes on every start ?

Yes. But the usual Docker approach is not to run apt update / apt install every time the container starts.

If you want commands to run on every container start, you can use an entrypoint script.

For example:

FROM nginx:latest

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh:

#!/bin/bash

apt-get update
apt-get install -y iputils-ping

exec nginx -g "daemon off;"

Then:

docker build -t my-nginx .
docker run my-nginx

Every time the container starts, it will run:

  1. apt-get update
  2. apt-get install -y iputils-ping
  3. Start nginx

But there’s a better way

If iputils-ping is something your container needs, install it when building the image, rather than every time the container starts:

FROM nginx:latest

RUN apt-get update \
    && apt-get install -y iputils-ping \
    && rm -rf /var/lib/apt/lists/*

Then:

docker build -t my-nginx .
docker run my-nginx

Now ping is already inside the image, so every new container starts immediately without downloading packages.

Important distinction

Docker images are immutable templates, while containers are runtime instances.

Dockerfile
    ↓ docker build
Docker Image
    ↓ docker run
Container
    ↓ restart
Same container filesystem

So if you do:

docker exec <container> apt-get install ...

the package exists in that particular container, but it doesn’t modify the original image.

If you tell me whether you’re using docker run, Docker Compose, or Kubernetes, I can show you the cleanest way to run initialization commands on every container start.