git repos | use npm cache inside of docker | Selenium test scripts | Search

This code snippet launches a Selenium container in the background, mapping necessary ports for access, and then displays a list of running containers.

Run example

npm run import -- "run the Docker image"

run the Docker image

docker run --shm-size=2g --name selenium -d -p 8888:8888 -p 6080:6080 -p 5900:5900 -p 4444:4444 -p 4200:4200 -p 3000:3000 selenium
docker ps

What the code could have been:

#!/bin/bash

# Function to start the selenium container
start_selenium_container() {
  # Set the shared memory size to 2GB
  shm_size=2g
  
  # Set the container name
  container_name="selenium"
  
  # Set the ports to be exposed
  exposed_ports=(
    "8888:8888"
    "6080:6080"
    "5900:5900"
    "4444:4444"
    "4200:4200"
    "3000:3000"
  )
  
  # Build the docker run command
  docker_run_command=(
    "docker run"
    "--shm-size=${shm_size}"
    "-d"
    "-p ${exposed_ports[@]}"
    "${container_name}"
  )
  
  # Execute the docker run command
  "${docker_run_command[@]}"
}

# Function to get the list of running containers
get_running_containers() {
  # Use the 'docker ps' command with the '-a' option to get all containers
  docker ps -a
}

# Start the selenium container
echo "Starting selenium container..."
start_selenium_container

# Get the list of running containers
echo "Running containers:"
get_running_containers

This code snippet starts and manages a Selenium Docker container.

Here's a breakdown:

Let me know if you'd like more details on any specific part of the code!