This post shows how to craft a basic docker-compose.yml file to start up a Keycloak Docker container with a first user account in a local development environment.
A docker-compose.yml
The docker-compose.yml that we use for this post has the following content. It uses a Docker image from JBoss and has a configuration for an initial user within KeyCloak. Moreover, it uses a bridge network. If we want to add other Docker images, we need to include them in the same network by using networks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | version: "2.3" services: keycloak-container: image: jboss/keycloak:5.0.0 command: # -Djboss.socket.binding.port-offset=200 restart: always environment: KEYCLOAK_USER: turreta KEYCLOAK_PASSWORD: 4909cbfa-2aba-4a30-836d-12b6d468a311 ports: - 8080:8080 # - 8280:8280 networks: - turreta_network networks: turreta_network: driver: bridge |
There are two commented lines. See the last section of this post on changing ports.
Start-Up Container For KeyCloak
Open a Windows Command Line prompt. Then, change to the directory where the docker-compose.yml file for KeyCloak container. Then, run the following commands in order.
The following command will pull the KeyCloak image Docker Hub to the local machine. If we added other images in the docker-compose.yml file, the command would put them too.
1 | docker-compose pull |
Then, create and run a Keycloak container. The following command only targets the creation of a KeyCloak container.
1 | docker-compose up keycloak-container |
If we want to create and run all containers for all the images we defined in the docker-compose.yml, we can use the following command.
1 | docker-compose up |
Then, we can access the Keycloak Web console via http://localhost:8080.
Change Ports
In case we wish to change to entry port to something other than 8080, simply uncomment the commented lines. Then, comment out - 8080:8080. The -Djboss.socket.binding.port-offset=200 will change the port to Keycloak port to 8280 (from 8080 + 200).
For example, we want to use port 9999. We change the value for ports to 9999:9999 and with an offset of 1919. The modified docker-compose.yml would like the following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | version: "2.3" services: keycloak-container: image: jboss/keycloak:5.0.0 command: -Djboss.socket.binding.port-offset=1919 restart: always environment: KEYCLOAK_USER: turreta KEYCLOAK_PASSWORD: 4909cbfa-2aba-4a30-836d-12b6d468a311 ports: - 9999:9999 networks: - turreta_network networks: turreta_network: driver: bridge |
Another example, we want to use port 8181. Change the value for ports to 8181:8181 and set the offset to 101.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | version: "2.3" services: keycloak-container: image: jboss/keycloak:5.0.0 command: -Djboss.socket.binding.port-offset=101 restart: always environment: KEYCLOAK_USER: turreta KEYCLOAK_PASSWORD: 4909cbfa-2aba-4a30-836d-12b6d468a311 ports: - 8181:8181 networks: - turreta_network networks: turreta_network: driver: bridge |
The file docker-compose.yml is just for a local development environment.
This post is part of the Docker For Developers tutorial.
We have recently tested the configuration with
jboss/keycloak:16.1.1
by just changing only the value for the image property.Other details: