Day 2: Building and Orchestrating with Docker - Lecture Notes
Yesterday, we laid the groundwork for Docker. Today, we'll learn how to build our own images and orchestrate multiple containers.
Session 5: Building Docker Images (1 hour)
Dockerfiles:
A Dockerfile is a text file containing instructions for building a Docker image. It's like a recipe for creating your container.
Key Dockerfile Instructions:
FROM
: Specifies the base image (where to start). Example:FROM ubuntu:latest
RUN
: Executes commands inside the image during the build process. Example:RUN apt-get update && apt-get install -y nginx
COPY
: Copies files from your host machine into the image. Example:COPY my-app /app
ADD
: Similar to COPY, but can also handle URLs and tar files.CMD
: The default command to run when a container is started from the image. Example:CMD ["nginx", "-g", "daemon off;"]
WORKDIR
: Sets the working directory inside the image.EXPOSE
: Declares which ports the container will listen on. Example:EXPOSE 80
Best Practices:
- Use a minimal base image.
- Combine
RUN
commands to reduce image layers. - Use
.dockerignore
to exclude unnecessary files from the image.
Hands-on:
- Create a Dockerfile: Create a new directory,
my-web-app
, and inside it, create a file namedDockerfile
(no extension). - Simple Web App: Create an
index.html
file in the same directory with some basic HTML content (e.g., "Hello from my Dockerized web app!"). - Dockerfile Contents: Add the following to your
Dockerfile
:
FROM nginx:latest
COPY index.html /usr/share/nginx/html
EXPOSE 80
- Build the Image: In the terminal, navigate to the
my-web-app
directory and run:
docker build -t my-web-app-image .
-t my-web-app-image
: Tags the image with the namemy-web-app-image
..
: Specifies the build context (the current directory).
- Run the Container:
docker run -p 8080:80 my-web-app-image
-p 8080:80
: Maps port 8080 on your host machine to port 80 in the container (where Nginx is listening).
- Test: Open your web browser and go to
http://localhost:8080
. You should see your "Hello from my Dockerized web app!" message.
Session 6: Docker Compose (1 hour)
Introduction to Docker Compose:
Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file (docker-compose.yml
) to
docker-compose.yml
:
This file defines the services that make up your application.
Example: A web application with a Redis database.
version: "3.9" # Specify the Compose file version
services:
web:
build: . # Build the image from the current directory (where the Dockerfile is)
ports:
- "80:80"
depends_on:
- redis
redis:
image: "redis:latest"
Hands-on:
- Create
docker-compose.yml
: Create adocker-compose.yml
file in the same directory as yourDockerfile
. Add the example above. - Run with Compose:
docker-compose up -d # -d runs the containers in detached mode (background)
- Access the Web App: Go to
http://localhost
in your browser. - Stop with Compose:
docker-compose down
Session 7: Docker Networking (1 hour)
Container Networking:
Containers can communicate with each other and with the outside world through Docker networks.
Network Types:
- Bridge: The default network. Containers connected to a bridge network can communicate with each other and the outside world through port mapping.
- Host: The container shares the host's network namespace. This gives the container direct access to the host's network interfaces. Use with caution!
- None: The container has no network access.
User-defined Bridge Networks:
You can create custom bridge networks to isolate your containers.
Hands-on (Continuing from the Docker Compose example):
- Explicitly Define a Network: Add a
networks
section to yourdocker-compose.yml
:
version: "3.9"
services:
web:
build: .
ports:
- "80:80"
depends_on:
- redis
networks:
- my-network
redis:
image: "redis:latest"
networks:
- my-network
networks:
my-network: # Define the custom bridge network
- Run with Compose:
docker-compose up -d
- Inspect the Network:
docker network ls
(lists Docker networks).docker network inspect my-network
(shows details about the network).
Session 8: Docker Volumes (1 hour)
Data Persistence:
By default, data inside a container is lost when the container is stopped or removed. Volumes provide a way to persist data outside of the container.
Volume Types:
- Bind Mounts: Mount a directory from the host machine into the container.
- Named Volumes: Volumes managed by Docker. Easier to back up and restore.
Hands-on (Continuing from the Docker Compose example):
- Add a Named Volume: Modify your
docker-compose.yml
to use a named volume for Redis data:
version: "3.9"
services:
web:
build: .
ports:
- "80:80"
depends_on:
- redis
networks:
- my-network
redis:
image: "redis:latest"
networks:
- my-network
volumes:
- redis-data:/data # Mount a named volume at /data inside the container
volumes:
redis-data: # Define the named volume
- Run with Compose:
docker-compose up -d
- Check the Volume:
docker volume ls
(lists Docker volumes).
Now, even if you stop and remove the Redis container, the data will be preserved in the redis-data
volume.
This concludes Day 2. You've learned how to build images, orchestrate containers with Compose, manage networking, and persist data with volumes. These are essential skills for working with Docker. Practice these concepts and you'll be well on your way to becoming a Docker pro!
No comments:
Post a Comment