Skip to main content

Docker containers list

To view a list of Docker containers that are currently running, you can use the docker ps command. Open your terminal or command prompt and run the following command:

docker ps

This command will display a list of running containers, showing information like the container ID, image name, status, ports, and more.

If you want to see all containers, including those that are not currently running, you can use the following command:

docker ps -a

The -a option shows all containers, including stopped ones.  These commands will help you view the status and details of Docker containers on your system.

To view the PHP version within a Docker container, you can use the following command:

docker exec -it <container_name_or_id> php -v

Replace <container_name_or_id> with the actual name or ID of your running Docker container.

Here's a breakdown of the command:

  • docker exec: This command is used to execute a command within a running container
  • -it: These flags make the command interactive and allocate a pseudo-TTY for your input
  • <container_name_or_id>: Replace this with the name or ID of the Docker container you want to check
  • php -v: This part of the command runs php -v within the specified container, which will display the PHP version

Running this command will output the PHP version installed in the specified Docker container.

 

Docker build PHP

Here's how you can build the Docker image:

  • Save the Dockerfile in your project directory
  • Open a terminal or command prompt
  • Navigate to the directory containing your Dockerfile
  • Run the following command to build the Docker image:
docker build -t my-php-app .

-t my-php-app tags the image with the name "my-php-app."

The dot (.) at the end of the command specifies the build context (the current directory).

After running this command, Docker will build the image based on the instructions in your Dockerfile and tag it as "my-php-app" (you can use a different name if you prefer). You can then run containers from this image to host your PHP application.

Remember that you may need to customise the Dockerfile to match the specific requirements of your PHP application, such as installing necessary PHP extensions and configuring your web server settings.  Lets review this now, by applying the following

docker build -t php-8.1.12-apache-2.4.54-bullseye -f Dockerfile.foundation .

Or not requiring a separate file to manage local (foundation)

 docker build -t php-8.1.16-apache-2.4.54-bullseye.local -f Dockerfile.base.local .

Updating to PHP 8.2.x

 docker build -t php-8.2.12-apache-2.4.56-bullseye.local -f Dockerfile.base.local .

 

 

Permission error

If you get a permission access error, check your AWS login via shell

# Get AWS ECR login password  (required AWS valid profile/credentials or temporary key/secret and token)
ECR_PWD=$(aws ecr get-login-password)
echo $ECR_PWD | docker login --password-stdin --username AWS {aws_account_id}.dkr.ecr.{region}.amazonaws.com
## or in a one-liner
aws ecr get-login-password \
    --region {region} \
| docker login \
    --username AWS \
    --password-stdin {aws_account_id}.dkr.ecr.{region}.amazonaws.com

If you receive an error stating - unable to locate credentials

Unable to locate credentials. You can configure credentials by running "aws configure".
Error: Cannot perform an interactive login from a non TTY device

To resolve, log in to your AWS page, select the relevant environment and along the row 'readonly-and-session-manager', select Command line or programmatic access.

CLICK on Option 1 to copy the keys.  The keys will display something similar to

export AWS_ACCESS_KEY_ID=""
export AWS_SECRET_ACCESS_KEY=""
export AWS_SESSION_TOKEN=""

PASTE them into your bash

Run one of the two commands from above - 'aws ecr get-login-password \...' and press RETURN.  For example,

aws ecr get-login-password \
    --region {region} \
| docker login \
    --username AWS \
    --password-stdin {aws_account_id}.dkr.ecr.{region}.amazonaws.com

aws_account_id is numeric - see below

# Example usage
account_id = "123456789012"

Response from this action

Login succeeded

 

Build Docker using tag and file

Build Docker using the tag php8.1.12-local and file Dockerfile.base.local.  If using the Docker.base.local, skip this line

docker build -t php8.1.12-local -f Dockerfile.base.local .

Or using the php8.2.12-local tag

docker build -t php8.2.12-local -f Dockerfile.base.local .

Post running this command compose the instance

docker compose up -d

To shut down a Docker instance

docker compose down

 

Clean up Docker image and containers

Remove Docker images handing around

docker image prune -y

Remove Docker all stopped containers

docker container prune -y

 

Related articles

Andrew Fletcher10 Aug 2023
sudo docker ps and exec -it
Working in an AWS ec2 environment, my goal is to access the server via terminal.I initially work to get the instance ID. &nbsp;With this ID, I'm able to connect to the server. &nbsp;When first accessing the environment on the server the steps I take are:sudo docker psps - refers to the 'process...