This code snippet launches a Selenium container in the background, mapping necessary ports for access, and then displays a list of running containers.
npm run import -- "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
#!/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:
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 run
: This command starts a new Docker container.--shm-size=2g
: Allocates 2GB of shared memory for the container. This is important for Selenium, which can use a lot of memory.--name selenium
: Names the container "selenium" for easy identification.-d
: Runs the container in detached mode (in the background).-p 8888:8888 -p 6080:6080 ...
: Maps ports from the container to the host machine. This allows you to access Selenium's web driver and other services from your local machine.selenium
: Specifies the Docker image to use. This assumes you have a Selenium image pulled and available.docker ps
: Lists all running Docker containers, including the "selenium" container you just started.
Let me know if you'd like more details on any specific part of the code!