Introduction
Docker is a powerful tool for containerization, allowing developers to encapsulate their applications and dependencies into portable containers. In this blog, we’ll cover the basics of Docker, including creating Docker images, launching containers, and using Docker Compose.
Chapter 1: Docker Image Basics
1.1 Pulling and Running an Image
When an image is not available locally, Docker automatically downloads it before launching the container. Let’s pull and run an Nginx web server image:
docker run -d -p 8080:80 — name my-nginx nginx
This command pulls the Nginx image and runs a container named ‘my-nginx,’ mapping port 8080 on the host to port 80 in the container.
1.2 Accessing Containerized Websites
You can use curl
to access a website running in a container. For example:
1.3 Attaching to Containers
To attach to a container with a bash shell, you can use the docker attach
command. If the container doesn't have a bash shell, use docker exec
:
docker exec -it my-nginx bash
Chapter 2: Tomcat Web Server
2.1 Dockerfile for Tomcat Server
Here’s a simple Dockerfile for a Tomcat web server:
FROM tomcat:latest
COPY ./webpages /usr/local/tomcat/webapps/ROOT
2.2 Building and Running Tomcat Container
Build the image using:
docker build -t my-tomcat .
Launch the container and expose port 8080:
docker run -d -p 8080:8080 — name my-tomcat-container my-tomcat
Access the web page using:
Chapter 3: Docker Compose
3.1 Docker Compose File
Create a docker-compose.yml
file for launching the Tomcat container:
version: ‘3’
services:
tomcat:
build: .
ports:
— “8080:8080”
3.2 Building and Launching with Docker Compose
Build and launch the container using Docker Compose:
docker-compose up -d
Chapter 4: Docker Compose with Image Building
4.1 Dockerfile Integration with Docker Compose
Use the build
keyword in docker-compose.yml
:
version: ‘3’
services:
tomcat:
build: .
ports:
— “8080:8080”
Build the image with:
docker-compose build
Conclusion
Docker simplifies the deployment of applications with containerization. This blog covered the basics of Docker images, running containers, and using Docker Compose. In upcoming chapters, we’ll explore more advanced topics and best practices. Stay tuned!