Overview
1. Dockerfile
- A text file that contains a set of instructions used to build a Docker image.
- Each instruction in the Dockerfile represents a layer in the resulting image.
- Define what goes on in the environment inside your container and specify how to configure the container, install dependencies, and set up the application environment.
Dockerfile
2. Docker image
- A read-only template that contains a set of instructions for creating a Docker container.
- Essentially a snapshot of a Docker container in a particular time.
- Includes the filesystem and configuration needed to run a specific application.
- Built from a Dockerfile and can be stored in repositories such as Docker Hub.
3. Docker container
- An isolated environment for your code.
- Not depend on your OS or your files.
- Have everything (code, runtime, libraries, dependencies, and configurations) that your code needs in order to run.
- Share the host operating system’s kernel, making them portable and efficient.
- Can be started, stopped, moved, and deleted with ease.
Docker Container
From Dockerfile to Docker container.
4. Docker volume
- Docker containers are indeed designed to be ephemeral, meaning they are intended to be short-lived and disposable → Docker volume.
- A persistent data storage mechanism used by Docker containers to store and manage data independently of the container’s lifecycle.
- Allow data to survive container restarts and can be shared and reused among multiple containers.
Docker mount and volume.
How does Docker volume work?
5. Docker network
- Provides a way to let containers in docker isolate but can communicate from one to another.
- When Docker is installed on the system, it automatically creates 3 networks: bridge, host and none.
- Docker automatically creates a default bridge network (docker0) for communication among containers on the same host.
- You can also define custom Docker networks to isolate and secure communication between containers or connect containers to external networks.
Docker network example
6. Docker Compose
- A tool for defining and running multi-container Docker applications.
- Uses a YAML file (docker-compose.yml) to define the services (containers), networks, and volumes that make up the application stack.
- Simplifies the complex task of orchestrating and coordinating various services, making it easier to manage and replicate your application environment.
Docker Syntax Guide
Docker Commands
*Note: All options and sub-commands listed below are just a small portion, provided based on usage needs. You can print the help screen of any sub-command with syntax:
docker <subcommand> --help
docker run
- docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
- Description: Create and start a new container based on the specified IMAGE.
- Options:
- -d: Run container in detached mode (background).
- -it: Interactive mode with a pseudo-TTY (for running interactive shells).
- –name: Assign a name to the container.
- –rm: Automatically remove the container when it exits.
- -e: Set environment variables.
- -p: Publish container’s ports to the host.
- -v: Mount host volumes into the container.
- –entrypoint: Override the entry point of the Docker image.
docker build
- docker build [OPTIONS] PATH | URL | -
- Description: Build a Docker image from a Dockerfile.
- Options:
- -t: Name and optionally a tag in the ’name:tag’ format.
- –file: Name of the Dockerfile (default is ‘PATH/Dockerfile’).
docker images
- docker images [OPTIONS] [REPOSITORY[:TAG]]
- Description: List Docker images.
- Options:
- -a: Show all images (including intermediate images).
- –digests: Show digests.
docker ps
- docker ps [OPTIONS]
- Description: List containers.
- Options:
- -a: Show all containers (including stopped ones).
- -q: Only display container IDs.
- –filter: Filter output based on conditions.
docker exec
docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
- Description: Execute a command inside a running container.
- Options:
- -it: Interactive mode with a pseudo-TTY.
- -e: Set environment variables.
docker stop
- docker stop [OPTIONS] CONTAINER [CONTAINER…]
- Description: Stop one or more running containers.
- Options:
- -t: Timeout in seconds to wait for stop (default 10).
docker rm
- docker rm [OPTIONS] CONTAINER [CONTAINER…]
- Description: Remove one or more containers.
- Options:
- -f: Force removal of running container.
docker rmi
- docker rmi [OPTIONS] IMAGE [IMAGE…]
- Description: Remove one or more images.
- Options:
- -f: Force removal of the image.
docker pull
- docker pull [OPTIONS] NAME[:TAG|@DIGEST]
- Description: Pull an image or a repository from a registry.
- Options:
- –all-tags: Download all tagged images in the repository.
- docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Docker Compose
docker-compose up
- docker-compose up [OPTIONS] [SERVICE…]
- Description: Create and start containers defined in the Compose file.
- Options:
- –build: Build images before starting containers.
- -d: Detached mode.
docker-compose down
- docker-compose down [OPTIONS]
- Description: Stop and remove containers, networks, and volumes defined in the Compose file.
- Options:
- –volumes: Remove named volumes declared in the volumes section of the Compose file.
- docker-compose down [OPTIONS]
Docker Swarm
docker swarm init
- docker swarm init [OPTIONS]
- Description: Initialize a new Docker Swarm.
- Options:
- –advertise-addr: Advertised address (format: <ip|interface>[:port]).
docker service
- docker service COMMAND [OPTIONS]
- Description: Manage Docker services in a Swarm.
- Commands:
- create: Create a new service.
- ls: List services.
- scale: Scale one or multiple replicated services.
Docker Network
- docker network
- docker network COMMAND [OPTIONS]
- Description: Manage Docker networks.
- Commands:
- create: Create a new network.
- ls: List networks.
- connect: Connect a container to a network.
- disconnect: Disconnect a container from a network.
- docker network COMMAND [OPTIONS]
Docker Volumes
- docker volume
- docker volume COMMAND [OPTIONS]
- Description: Manage Docker volumes.
- Commands:
- create: Create a volume.
- ls: List volumes.
- inspect: Display detailed information about a volume.
- docker volume COMMAND [OPTIONS]
Docker Registry (Docker Hub)
docker login
- docker login [OPTIONS] [SERVER]
- Description: Log in to a Docker registry.
- Options:
- –username: Username.
- –password: Password.
docker push (Login required)
- docker push [OPTIONS] NAME[:TAG]
- Description: Push an image or a repository to a registry.
docker pull (No login required)
- docker pull [OPTIONS] NAME[:TAG]
- Description: Pull an image or a repository from a registry.
Docker System
- docker system
- docker system COMMAND [OPTIONS]
- Description: Manage Docker system resources.
- Commands:
- df: Show Docker disk usage.
- prune: Remove unused data.
Leave a comment