Docker is a tool designed to make it easier to create, deploy, and run applications using containers. Containers allow you to package up an application with all parts it needs, such as libraries and other dependencies, and ship it all out as one package. Here’s a quick and easy-to-understand cheat sheet to get you started.
What is Docker?
- Docker is a platform for developing, shipping, and running applications in containers.
- Containers are lightweight, portable, and self-sufficient environments that run applications and their dependencies.
Key Concepts in Docker
- Docker Image: A read-only template used to create containers. For example, an image could contain a Python application, including everything it needs to run.
- Docker Container: A running instance of a Docker image.
- Dockerfile: A text file that contains all the instructions to build a Docker image.
- Docker Hub: A registry for sharing Docker images. You can pull or push images from and to Docker Hub.
Common Docker Commands
Basic Docker Commands
- Check Docker Version
docker --version
- List Docker Images
docker images
- List Running Containers
docker ps
- List All Containers (including stopped ones)
docker ps -a
- Run a Container
docker run <image-name>
- Stop a Running Container
docker stop <container-id or container-name>
- Remove a Container
docker rm <container-id or container-name>
- Remove an Image
docker rmi <image-id or image-name>
Working with Docker Images
Pull an Image from Docker Hub
To pull a pre-built image from Docker Hub:
docker pull <image-name>
Build an Image from a Dockerfile
1. Create a Dockerfile with instructions. Example Dockerfile
:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y python3
COPY app.py /app.py
CMD ["python3", "/app.py"]
2. Build the image:
docker build -t <image-name> .
Running Containers
Run a Container in Interactive Mode
docker run -it <image-name> bash
This command runs a container and opens a bash terminal inside it.
Run a Container in Detached Mode
docker run -d <image-name>
This runs the container in the background (detached mode).
Expose Ports
To expose a container’s port to your local machine, use the -p
flag:
docker run -p <host-port>:<container-port> <image-name>
Docker Networking
Create a Custom Network
docker network create <network-name>
Connect a Container to a Network
docker network connect <network-name> <container-name>
Docker Volumes
Docker Volumes allow you to persist data across container restarts. Here’s how to create and use volumes:
Create a Volume
docker volume create <volume-name>
Mount a Volume
docker run -v <volume-name>:/path/in/container <image-name>
List Volumes
docker volume ls
Docker Compose (For Multi-Container Applications)
Docker Compose allows you to define and manage multi-container Docker applications.
Check Docker Compose Version
docker-compose --version
Example docker-compose.yml
file
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres
environment:
POSTGRES_PASSWORD: example
Start Services Using Compose
docker-compose up
Stop Services Using Compose
docker-compose down
Dockerfile Example
A basic Dockerfile example for a Python app:
1. Create a directory for your app.
2. Add an app.py
(your Python app) and the following Dockerfile:
# Use the official Python base image
FROM python:3.9-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the local app.py to the container
COPY app.py /app
# Install dependencies
RUN pip install flask
# Command to run the app
CMD ["python", "app.py"]
3. Build and run the image:
docker build -t python-app .
docker run -p 5000:5000 python-app
Cleaning Up
Remove Stopped Containers
docker container prune
Remove Unused Images
docker image prune
Remove Unused Volumes
docker volume prune
Conclusion
With this cheat sheet, you should be able to get started with Docker and begin working with containers, images, and even multi-container applications. Practice is key — try running your own containers, creating Dockerfiles, and experimenting with Docker Compose to deepen your understanding. For more in-depth information, you can refer to Docker’s official CLI documentation at Docker CLI Reference.
Comments
Post a Comment