Standard Workflow
# Search for available images
docker search <image_name>
# Download an image to the local machine
docker pull <image_name>
# Verify the downloaded image
docker images
# Launch a container from the image
docker run [options] <image_name>
# Halt a running container
docker stop <container_id>
# Delete a stopped container
docker rm <container_id>
Setting Up Tomcat
Locate the official Tomcat image on Docker Hub.
docker search tomcat
Pull the latest Tomcat image.
docker pull tomcat
Confirm the image is stored locally using docker images.
Start an interactive Tomcat container, mapping the host's port 8080 to the container's port 8080.
docker run -it -p 8080:8080 tomcat
The -it flags allocate an interactive terminal session. Use -P instead of -p to let Docker assign a random high port on the host.
Setting Up MySQL
Search the registry for the MySQL image.
docker search mysql
Download a specific version, for instance 5.6, from the configured mirror.
docker pull mysql:5.6
Launch a MySQL container with persistent storage and a predefined root password. The command below maps host port 12345 to container port 3306, mounts configuration, log, and data directories, and sets up the environment.
docker run -p 12345:3306 --name mysql_srv \
-v /opt/docker/mysql/conf:/etc/mysql/conf.d \
-v /opt/docker/mysql/logs:/logs \
-v /opt/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=securepwd123 \
-d mysql:5.6
Parameter breakdown:
-p 12345:3306: Configures the host-to-container port mapping.--name mysql_srv: Assigns a custom name to the container.-v /opt/docker/mysql/conf:/etc/mysql/conf.d: Binds the host directory for custom MySQL configurations.-v /opt/docker/mysql/logs:/logs: Persists log files on the host.-v /opt/docker/mysql/data:/var/lib/mysql: Ensures database files survive container restarts.-e MYSQL_ROOT_PASSWORD=securepwd123: Supplies the root user's password.-d mysql:5.6: Runs the container in detached mode using the version 5.6 image.
To access the runing MySQL container via a shell, execute:
docker exec -it mysql_srv /bin/bash
After the container is operational, external clients (e.g., a Windows machine) can connect to the MySQL service using the host's IP address and port 12345.
Setting Up Redis
Pull a specific Redis tag, such as 3.2, from the mirror.
docker pull redis:3.2
Start a Redis container that mounts a custom configuration file and data directory, enabling append-only persistence.
docker run -p 6379:6379 \
-v /opt/docker/redis/data:/data \
-v /opt/docker/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-d redis:3.2 \
redis-server /usr/local/etc/redis/redis.conf \
--appendonly yes
In this command:
- Container port 6379 is exposed on host port 6379.
- The host path
/opt/docker/redis/datastores the dump.rdb and append-only files. - The custom
redis.confis mounted from the host's/opt/docker/redis/conf/directory. - The final arguments instruct the Redis server to use the provided configuration file and enable the AOF persistence mode.
Preparing the Redis Configuration File
Before launching the container, create a minimal redis.conf at the host location. Essential parameters include:
port 6379
bind 0.0.0.0
protected-mode no
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly no
appendfsync everysec
Adjust these settings according to your performance and durability needs. For instance, modify the save directives to control RDB snapshot frequency, and set appendonly yes in the container launch arguments if AOF is preferred.
Testing the Redis Deployment
Open a redis-cli session inside the container to verify functionality:
docker exec -it <redis_container_id> redis-cli
Once connected, run basic commends like PING and SET testkey "hello" to confirm the server is responding. Persistence can be validated by enspecting the /opt/docker/redis/data directory on the host after a write operation and container restart.