Last mod: 2025.08.31
Docker and Docker Compose
Verification
Check docker and docker-compose version:
docker -v
docker-compose -v
Run docker image test:
docker run hello-world
Status checks
List running containers:
docker ps
List all containers:
docker ps -a
Access to the container via bash.
Let's run Ubuntu container in the background and give the container a name for convenience:
docker run -dit --name container_no_1 ubuntu bash
Where:
- -d - run in the background (detached)
- -i - interactive mode
- -t - assign a pseudo-terminal
- --name container_no_1 - assigns a name to the container
- ubuntu bash - runs in the bash container (It is required in order to log in via bash)
And list running containers:
docker ps
To access via ssh, execute the following command:
docker exec -it container_no_1 bash
Useful docker compose commands
Stop all running containers:
docker stop $(docker ps -q)
Remove all containers:
docker rm $(docker ps -aq)
Utwórzmy defincję dwóch kontnerów, jeden z Tomcatem, drugi z bazą MariaDB. Remove all images
docker rmi $(docker images -q)
Force remove everything in one step
docker system prune -a --volumes
Docker Compose
If we want to define several connected containers, Docker Compose is a very good solution. Let us create definitions for two containers, one with Tomcat and the other with MariaDB, file docker-compose.yml:
version: '3.9'
services:
tomcat:
image: tomcat:10.1-jdk17
container_name: tomcat_app
restart: always
ports:
- "8080:8080"
environment:
- TZ=Europe/Warsaw
depends_on:
- mariadb
volumes:
- ./webapps:/usr/local/tomcat/webapps
mariadb:
image: mariadb:11.3
container_name: mariadb_db
restart: always
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_DATABASE=mydb
- MYSQL_USER=myuser
- MYSQL_PASSWORD=mypassword
- TZ=Europe/Warsaw
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
In the directory wit the docker-compose.yml, run:
docker-compose up
Let's check if Tomcat is working. Enter the address of our host in the browser. If we are running locally, it is http://127.0.0.1:8080 (in my case, however, it is a remote machine http://192.168.3.112:8080):
Tomcat responds to the request. There is nothing on it, so we get a 404 code.
Let's connect to the MariaDB database:
mysql -h 127.0.0.1 -P 3306 -u myuser -p
The password is in the MYSQL_PASSWORD parameter.
Both containers are working. We can list them: