Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: February 27, 2025
When working with Docker in automated environments like Jenkins, it’s common to encounter scenarios where we need to create a Docker container only if it doesn’t already exist. Running a command to create a container without checking its existence can lead to errors, and cause the Jenkins job to fail. In order to avoid this, we can conditionally execute commands in Bash and check whether a Docker container with a specific name already exists.
In this tutorial, we’ll look into different ways to check if a Docker container with a specific name exists before attempting to create it.
The simplest way to check if a container exists is by using the docker ps command. Below is a bash script to demonstrate the same:
if ! docker ps -a --format '{{.Names}}' | grep -wq "container_name"; then
docker run -d --name container_name container_image
else
echo "Container exists"
fi
Here, we first list all the Docker container names that are present on the system using the docker ps command. Further, we filter the Docker container with the desired name using the grep command. Finally, if the container does not exist, the docker run command will create a new container.
Another approach is to use docker inspect, which provides detailed information about a container. If the container does not exist, the command will return a non-zero exit code. We can leverage this in a Bash conditional statement:
if ! docker inspect container_name > /dev/null 2>&1; then
docker run -d --name container_name container_image
else
echo "Container exists"
fi
Here we fetch the details about the container using the docker inspect command. We also suppress both standard output and the error output. At last, if the container does not exist, the docker run command is executed.
We can also use the –filter flag with docker container ls to check for the existence of a container by name:
if [ -z "$(docker container ls -a --filter name=^/container_name$ --format '{{.Names}}')" ]; then
docker run -d --name container_name container_image
else
echo "Container exists"
fi
The if condition lists containers with an exact container name match. The –format ‘{{.Names}}’ make sure to output only the container name. If the output is empty (i.e., the container does not exist), we create a new container.
In the case of Docker Compose, we can check the existence of the Docker container using the below bash script:
if ! docker-compose ps | grep -wq "container_name"; then
docker-compose up -d
else
echo "Container exists"
fi
The command docker-compose ps will list the status of containers managed by Docker Compose. Next, “grep -wq container_name” will check if the container exists. If the container does not exist, the docker-compose up -d command is executed.
Although more complex, querying Docker API or using tools that leverage it can provide sophisticated checks:
container_name="my_container"
curl --unix-socket /var/run/docker.sock http://localhost/containers/$container_name/json | grep -q '"Id":'
if [ $? -eq 0 ]; then
echo "Container exists"
else
docker run -d --name container_name container_image
fi
The above script uses the curl command to send a request to the Docker daemon’s local API endpoint. The response, which contains JSON data about the container, is then piped to grep to search for the presence of “ID:“. If the ID field is not present, we create a new container using the same name.
This approach leverages Docker’s API to perform a precise existence check without relying solely on command-line utilities like docker ps.
All the approaches that we discussed so far can be made cleaner and reusable by encapsulating the logic in a Bash function. For eg:
container_exists() {
docker ps -a --format '{{.Names}}' | grep -wq "$1"
}
if ! container_exists "container_name"; then
docker run -d --name container_name container_image
fi
Here, we created a reusable function, container_exists, that checks if a container exists. The function is called with the container name as an argument.
In this tutorial, we learned multiple ways to check if a Docker container exists before attempting to create it in a Bash script.
For simplicity, we use docker ps -a with grep or docker inspect to ensure efficient scripting in Docker environments. By incorporating these techniques into the Jenkins jobs or other automation scripts, we can avoid unnecessary errors and ensure smooth execution.