How can I access a docker container direct from host

I am able to create docker containers. But I still hhva problems how to use it!
For example, if I install MariaDB on port 3306, I can access the database via port 3306.

But now I try to install php and composer in an container.
Is there any possibility to configure the container, that I can execute PHP and Composer in the same way, as I can do it, if I would install the scripts direct on the host?
For example, php <script_name> or composer .

Edit 1

DockerFile

FROM dunglas/frankenphp
COPY . /app/public


FROM php
COPY . /var/www/html/CRadio
WORKDIR /var/www/html/CRadio
#CMD [ "php", "./your-script.php" ]
ENTRYPOINT ["php"]

FROM composer
Copy . /var/www/html/CRadio
WORKDIR /var/www/html/CRadio
ENTRYPOINT ["composer"]

compose.yaml

services:
  frankenphp:
    container_name: frankenphp
    image: dunglas/frankenphp
    restart: always
    ports:
      - "80:80" # HTTP
      - "443:443" # HTTPS
#	   - "443:443/udp" # HTTP/3
    volumes:
      - ./:/app # Mounts your project files
      - caddy_data:/data
      - caddy_config:/config
    working_dir: /app/public
#    command: frankenphp php-server --root=/app --listen=:8080 # Or use default 
  db:
    container_name: mariadb
    image: mariadb
    restart: always
    user: root
    secrets:
      - db-password
    volumes: 
      - db-data:/var/lib/mysql
    environment:
      - MARIADB_ROOT_PASSWORD_FILE=/run/secrets/db-password
      - MARIADB_DATABASE=example
    expose:
      - "3306"
    healthcheck:
      test:
         [
           "CMD",
           "/usr/local/bin/healthcheck.sh",
           "--su-mysql",
           "--connect",
           "--innodb_initialized",
         ]
      interval: 10s
      timeout: 5s
      retries: 5
  php:
    restart: always
    container_name: php
    image: php
    
  composer:
    restart: always
    container_name: composer
    image: composer
    depends_on:
      - php


  phpmyadmin:
    container_name: phpmyadmin
    image: phpmyadmin
    ports:
      - 8080:80
    depends_on:
      - db
    environment:
      - PMA_HOST=db
volumes:
  db-data:
  caddy_data:
  caddy_config:
secrets:
   db-password:
     file: db/password.txt

I have used docker compose --build -d to build image and docker container.

docker compose ps

georg@hpradio1:/var/www/html/CRadio$ docker compose ps
NAME         IMAGE                COMMAND                  SERVICE      CREATED          STATUS                          PORTS
composer     composer             "/docker-entrypoint.…"   composer     51 seconds ago   Restarting (0) 21 seconds ago   
frankenphp   dunglas/frankenphp   "docker-php-entrypoi…"   frankenphp   2 hours ago      Up 50 seconds (healthy)         0.0.0.0:80->80/tcp, [::]:80->80/tcp, 0.0.0.0:443->443/tcp, [::]:443->443/tcp, 443/udp, 2019/tcp
mariadb      mariadb              "docker-entrypoint.s…"   db           2 hours ago      Up 50 seconds (healthy)         3306/tcp
php          php                  "docker-php-entrypoi…"   php          12 minutes ago   Restarting (0) 22 seconds ago   
phpmyadmin   phpmyadmin           "/docker-entrypoint.…"   phpmyadmin   2 hours ago      Up 50 seconds                   0.0.0.0:8080->80/tcp, [::]:8080->80/tcp

Thanks for you help.

The easiest way for us to help you, is if you show us what you did and how you did it (as in exact commands, if used: docker compose file content, Dockerfile content), what didn’t work as expected and what error messages you got.

Your questions make sense for you, as you have the whole context in your head. We need more details to actually have a chance to understand the context.

For instance “if I install” is quiet ambiguous with containers. The term “install” only makes sense when you install the docker engine itself, or in RUN instructions of a Dockerfile when you build your own image.

Happy new year!

You edited your first post, but that doesn’t trigger any new notification so I noticed it only because I came later. When you edit a post which is not the last one, writing a new post to let us know about the edited one is recommended.

@meyay pointed out that an error message can help us figuring out what your problem is. You haven’t shown any error message and I don’t see where you pointed out what is the unexpected behaviour, except docker compose ps shows two restarting containers.

Based on docker compose ps I guess your poroblem is that you expect php and composer containers just run as virtual machines would where you get a shell and can run php commands. But you are using containers, not virtual machines, so each container has its own command that has to run in the foreground, otherwise the command finishes and the container restarts to start the same command again, which fnishes again and everything starts again forever.

I’m not sure what your goal is with the php and composer container. composer is a command line tool to manage PHP dependencies. It doesn’t have to run as a compose service. so it doesn’t make sense to add it to the compose file and set php as dependency. composr is already based on PHP, so it doessn’t have to depend on another PHP container which has nothing to do with composer.

You also have frankenphp container, so I am not sure what you want to do with the php container. Currently, the php container you run just executes the php cli without any argument so it starts a php shell and stops immediately as there is no interactive terminal to keep it running.

You could add

    stdin_open: true

to the restarting services, but again, it wouldn’t make sense as you would just get independend containers that don’t even have access to any data or file to work with.

Small addition to @rimelek’s post:

Please read this post to get a better understanding of what should be in the same container or separated into different containers:

Update:
I think the confusion comes from how the Dockerfile works. Is it safe to assume that you expect the three stages to be merged into the final image? Unfortunately this is not how it works.

It looks like you try to do a multi-stage build, but end up without an image hierarchy or copying artifacts from previous stages.

See docs for multi-stage builds:

Let me try try to explain what I want to understand.
I have installed FrankenPHP and FrankenPHP, as I understood now, is only handle PHP for Webpages. If I want to use for example PHP for Composer I have to install a PHP-CLI version.
Is that right?
My idea was to install PHP and Composer in a separate container, but I don’t know, how to use it.
Yes, Composer is based on PHP. Does it comes with an own PHP version?
Without Docker I usually install install PHP on the Host and additional install Composer, which is running on PHP.
On the host I can use php and composer as a command and add the additional insructions after the command.
But now it seems to me, that this behavier is not possible when PHP or Composer is running in a container?
If I want to use Composer only, would it be enough to install only Composer, but would it than possible to use composer directly from the Host command line?
As you can see, I, have still a problem to understand Docker Containers in generall.
I understand the use of Docker Containers, if the run on a specific port, like MariaDB or FrankenPHP, but I don’t understand how to use PHP or Composer, if the are running in a Container.
I have looked for good documentation, which explain it for a beginner. My feeling is, that the docker documentation is written for people, which are using it already for a long time.

Your explanation of a VM and a Container is not really clear to me. Inside a VM I can use PHP commands inside this VM, but I can’t use this commands from an nother Host.
You are right, here is my real problem! How to use Containers, which don’t have a specific poert!,
Your last sentence, “a independend container that don’t have access to any data or file to work with”. That this mean that a container can only access data of files, which are inside of the container?
In this case, it would be not make any sense to put PHP and Composer inside a Container. So I have to to install it on the Host directly.
Do you agree?