Tech Tutorial: 352.3 Docker (weight: 9) #
Docker has revolutionized how we think about and deploy applications by making containerization accessible and practical. In this tutorial, we’ll explore Docker’s capabilities, focusing on the exam objective 352.3 of a Linux certification, which covers Docker at an intermediate level.
Exam Objective: #
- Understand Docker’s core concepts and architecture
- Manage Docker containers and images
- Set up and configure Docker environments
- Use Dockerfiles to automate the creation of Docker images
Key Knowledge Areas: #
- Docker installation and configuration
- Managing Docker containers
- Managing Docker images
- Docker networking configurations
- Docker volumes and storage
- Dockerfile basics
Utilities: #
docker
docker-compose
Introduction #
Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.
Step-by-Step Guide #
1. Installing Docker #
Before we can work with Docker, we need to install it. Here’s how to install Docker on Ubuntu:
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce
To verify the installation:
sudo docker run hello-world
2. Managing Docker Containers #
Let’s explore how to manage Docker containers.
Running a container:
sudo docker run -d --name mynginx nginx
This command downloads and runs an Nginx container in detached mode.
Listing containers:
sudo docker ps # active containers
sudo docker ps -a # all containers
Stopping a container:
sudo docker stop mynginx
Removing a container:
sudo docker rm mynginx
3. Managing Docker Images #
Managing Docker images is crucial for maintaining your Docker environment.
Pulling an image:
sudo docker pull ubuntu
Listing images:
sudo docker images
Removing an image:
sudo docker rmi ubuntu
4. Docker Networking #
Docker allows you to set up your own networks.
Creating a network:
sudo docker network create mynetwork
Listing networks:
sudo docker network ls
Attaching a container to a network:
sudo docker run -d --name mynginx --network mynetwork nginx
Inspecting a network:
sudo docker network inspect mynetwork
5. Docker Volumes and Storage #
For persistent storage, Docker uses volumes.
Creating a volume:
sudo docker volume create myvolume
Listing volumes:
sudo docker volume ls
Using a volume:
sudo docker run -d --name mynginx -v myvolume:/usr/share/nginx/html nginx
6. Using Dockerfiles #
Dockerfiles automate the process of Docker image creation.
Sample Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Run app.py when the container launches
CMD ["python", "app.py"]
Build and run:
sudo docker build -t mypythonapp .
sudo docker run -d -p 4000:80 mypythonapp
Conclusion #
This tutorial should provide a solid foundation for understanding Docker and its functionalities. The skills learned here are essential for anyone looking to deploy and manage containerized applications efficiently. Docker’s versatility in building, shipping, and running distributed applications is invaluable for any developer or system administrator.