In this HashiQube DevOps lab, you'll get hands-on experience with Docker. You'll learn how to:
- Build a custom Docker image from a Dockerfile
- Run Docker containers
- Configure a Docker daemon with authentication
- Set up a Docker registry
This lab follows the official Docker installation guide for Ubuntu-based systems.
bash docker/docker.sh
vagrant up --provision-with basetools,docsify,docker
docker compose exec hashiqube /bin/bash
bash hashiqube/basetools.sh
bash docker/docker.sh
bash docsify/docsify.sh
When you run the provisioner, it will:
- Install Docker on your HashiQube environment
- Build an Apache 2.4 container from a Dockerfile
- Run the container and expose it to your host machine
- Make the container accessible at http://localhost:8889
Below is the Dockerfile used to build the Apache web server container:
FROM ubuntu:18.04
# Install dependencies
RUN apt-get update && \
apt-get -y install apache2
# Install apache and write hello world message
RUN echo 'Hello World!' > /var/www/html/index.html
# Configure apache
RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh && \
echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh && \
echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh && \
echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh && \
chmod 755 /root/run_apache.sh
EXPOSE 80
CMD /root/run_apache.sh
This Dockerfile:
- Uses Ubuntu 18.04 as the base image
- Installs Apache web server
- Creates a simple "Hello World" web page
- Sets up a script to run Apache in the foreground
- Exposes port 80 for web traffic
- Specifies the command to run when the container starts
HashiQube includes Prometheus and Grafana for monitoring Docker containers.
For detailed information, see the Monitoring Docker guide.
- Images: Read-only templates used to create containers
- Containers: Runnable instances of images
# List running containers
docker ps
# List all containers (including stopped ones)
docker ps -a
# List available images
docker images
# Pull an image from Docker Hub
docker pull ubuntu:latest
# Run a container
docker run -d -p 8080:80 --name my-container nginx
# Stop a container
docker stop my-container
# Remove a container
docker rm my-container
# Remove an image
docker rmi nginx
Docker creates isolated networks for containers by default. You can:
- Create custom networks
- Connect containers to multiple networks
- Configure network settings
# Create a custom network
docker network create my-network
# Run a container connected to the custom network
docker run -d --network=my-network --name my-db postgres
# Connect an existing container to a network
docker network connect my-network my-container
Volumes provide persistent storage for containers:
# Create a named volume
docker volume create my-data
# Run a container with a volume
docker run -d -v my-data:/data --name my-container ubuntu
# Mount a host directory as a volume
docker run -d -v /host/path:/container/path --name my-container ubuntu
The script below automates the setup of Docker in your HashiQube environment:
- Docker Official Website
- Docker Documentation
- Docker Compose Documentation
- Docker Hub - Repository for Docker images
- Docker Best Practices
- Docker Cheat Sheet