run command in docker container code example

Example 1: docker interactive shell

docker exec -it [containerid/name] [shell]

#Example
docker exec -it fa80b69 bash
#if its an apline container use "sh" instead of "bash"

Example 2: how to bash into docker container

docker exec -it nginx /bin/bash

Example 3: docker exec bash

//For executing commands in the container
docker exec -it <CONTAINER_NAME> bash

Example 4: docker run command on container

sudo docker exec -it <container name> <command>

Example 5: run docker container with database as rds metabase

docker run -d -p 3000:3000 \
  -e "MB_DB_TYPE=postgres" \
  -e "MB_DB_DBNAME=metabase" \
  -e "MB_DB_PORT=5432" \
  -e "MB_DB_USER=<username>" \
  -e "MB_DB_PASS=<password>" \
  -e "MB_DB_HOST=my-database-host" \
  --name metabase metabase/metabase

Example 6: docker compose keep container running

version: '2'
services:
  my-test:
    image: ubuntu
    command: tail -F anything