DevOps Docker - Part-2
DevOps - Docker Tutorial - Part 2
Understand Docker images, Dockerfiles, volumes, and Compose with diagrams and examples.
1Part 1 Recap
In Part 1, we learned that an image is a packaged template and a container is an instance of it. In this part, we explore how images are built and how multiple services work together.
2Images and Layers
An image is made of layers. During a build, Docker can reuse cached results for unchanged steps, which can make repeated builds faster.
Interactive example: See the build stages. This is a browser simulation; it does not run Docker.
3Understanding a Dockerfile
This illustrative Dockerfile packages a Java REST API. It assumes an executable Java 17-compatible JAR already exists in the build context at target/app.jar. It is a code illustration, not a complete Java project.
FROM eclipse-temurin:17-jre
WORKDIR /app
COPY target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
FROMselects a base runtime image andWORKDIRsets the working directory.COPYplaces the JAR from the build context into the image.EXPOSEdocuments the intended container port; it does not publish it.ENTRYPOINTstarts the Java application.
4Image Tags and Registry
A tag identifies an image name and version, such as my-api:1.0. A registry stores images for sharing. Tags can be moved; an image digest provides a more precise identifier.
docker image ls lists local images. Before pushing, check the target repository and required permissions.
5Container Data and Volumes
Data in a container's writable layer is lost when that container is removed. A Docker volume can keep data separate from the container's lifecycle.
6Docker Compose
Compose uses YAML to define multiple services as one application. The configuration below illustrates two separate services; Nginx does not automatically use Redis.
services:
web:
image: nginx:alpine
ports:
- "127.0.0.1:8080:80"
cache:
image: redis:7-alpine
With a Compose configuration, docker compose up starts services and docker compose down stops them and removes their containers and networks. Use volume-removal options carefully.
Properly configured containers can reach one another by service name on the default Compose network, such as cache:6379.
7Java REST API Flow
A typical Java API receives a request, reads data from a database, and returns JSON. Containerization does not automatically make the application secure or scalable.
The actual API port, database connection, authentication, and health checks depend on the application's configuration. This diagram illustrates the concept only.
8Better and Safer Builds
The build context contains the files available to the builder. Including only what is needed can speed up builds and reduce accidental exposure of unnecessary data.
Build context and .dockerignore
These are example .dockerignore patterns. Check them against your project: the Dockerfile above needs target/app.jar, so do not exclude the entire target directory.
.git
.env
*.log
Cache and multi-stage builds
When a step's inputs change, it and dependent later steps may need to run again. Placing less frequently changed steps before frequently changed application code can improve cache reuse.
A multi-stage Dockerfile can separate build and runtime stages. Copying only the required artifact into the final image can make it smaller without including build tools.
Read more: build context, multi-stage builds, and build secrets.
9Configuration and Networking
Application settings may differ between development, test, and production. Pass non-sensitive settings at runtime; use your environment's secret manager for passwords and API keys.
- Environment variables: Useful for settings such as ports and profiles, but they may be visible through inspection or logs. Use appropriate secret management for sensitive data.
- Service address: On a default Compose network, a Java API can reach a database by service name, such as
db:5432. Inside a container,localhostrefers to that container. - Health and readiness: A running process does not guarantee a ready service. Configure appropriate application health checks and handle a database that starts late or becomes temporarily unavailable.
- Least privilege: Use a non-root user and minimum required permissions where possible. First check which writable paths and ports the application needs.
The example db address works only when that Compose project has a database service named db. The Nginx/Redis example above does not define one.
10Inspecting and Troubleshooting
In these commands, a143mk-docker2-nginx is an example container name. Run them only against your own container; choose another name if it is already in use.
For an optional local test, start Docker, make sure internet is available and port 8080 is free. This starts a background container from the public Nginx image:
docker run -d --name a143mk-docker2-nginx -p 127.0.0.1:8080:80 nginx:alpine
While it is running, open http://localhost:8080; you should see the Nginx welcome page. In another terminal, inspect status, logs, and port mapping. The container must be running to view resource usage.
docker ps -a
docker logs --tail 50 a143mk-docker2-nginx
docker inspect --format '{{.State.ExitCode}}' a143mk-docker2-nginx
docker port a143mk-docker2-nginx
docker stats --no-stream a143mk-docker2-nginx
- Exited container: Read its logs and check its exit status. For example, invalid configuration or a failed main process can stop a container.
- Page does not load: Check that the container is running, the application listens on the right container port, and the host port is mapped. If 8080 is busy, choose another free host port.
- Database connection fails: Check the service name, network, credentials, and database readiness. Do not publish secrets in logs or screenshots.
If you started the container above and no longer need it, stop and remove only that container:
docker stop a143mk-docker2-nginx
docker rm a143mk-docker2-nginx
docker compose ps shows status and docker compose logs shows service logs.11Developer Checklist
Before sharing an image with your team or CI/CD pipeline, consider these points:
- Run tests during the build workflow and review the results; a successful image build alone does not mean application tests passed.
- Choose trusted base images, control versions, and regularly review dependencies and known vulnerabilities.
- Use appropriate configuration, secret management, least privilege, and only the required network ports.
- Define logging, health checks, resource limits, persistent data, and backup needs for your application.
- Identify images by tag and, where possible, digest so the same tested image can be deployed.
12Quick Quiz
Choose an answer for each question and check your results.
13What Comes Next?
You have explored image layers, Dockerfiles, volumes, and Compose. Next, learn Kubernetes fundamentals and how containers are deployed.
Learn more: Dockerfile documentation and Docker Compose documentation.
No comments :
Post a Comment
Please Write a Message for Programming or something else 🙏