Table of contents
Terms
Term | Description |
---|---|
Docker | The containerization platform. |
Container | A lightweight, standalone, executable package for software. In other words, it's isolated environment for your code. The container has no knowledge of your operating system, or your files. |
Image | A snapshot or template containing application code and dependencies. |
Dockerfile | Script defining instructions for building a Docker image. |
Build | Creating an image snapshot from the Dockerfile |
Tag | Version of image / release |
Registry | Centralized repository for storing and sharing Docker images. |
Docker Compose | Tool for defining and running multi-container Docker applications. |
Container Orchestration | Automated management of containerized applications. |
Docker Swarm | Docker's native clustering and orchestration solution. |
Service | A group of containers managed as a single unit. |
Volume | Docker feature for persisting data between container restarts. |
Networking | Docker's various options for connecting containers. |
Overlay Network | Docker network spanning multiple nodes in a Swarm. |
Health Check | Docker feature to define a command for checking container health. |
Docker Machine | Tool for provisioning Docker hosts on virtual machines. |
Docker Hub | Cloud-based registry for sharing and accessing Docker images. |
docker-compose.yml | Configuration file for Docker Compose, written in YAML. |
Docker Daemon | Background process managing Docker containers on a host. |
Dockerignore | File specifying patterns to exclude from the Docker build context. |
General commands
List of commands
docker
Command help
docker ps --help
Version:
docker --version
Containers
Display containers
List of running containers
docker ps
Display all containers, even those that are not running at the moment.
docker ps -a
Display all container IDs.
docker ps -aq
Running the container
Create and run the container from image.
docker run --name home-container -d -p 3000:8080 home-image:1.0.0
--name home-container - Container name-d - Run as a daemon process-p 3000:8080 - Port,8080 inside,3000 outsidehome-image - Image name1.0.0 - Image tag
Stopping the container
Stop the container, where
docker stop 7cc16e2
Stop the container, where the
docker stop my-container
Starting the container
Start the container, where
docker start 7cc16e2
Start the container, where the
docker start my-container
Removing the container
Remove container by name
docker rm my-container
Remove all the containers.
docker rm $(docker ps -aq)
Force remove all the containers.
docker rm -f $(docker ps -aq)
Connect to running container
docker exec -it home-container /bin/bash
Display container logs
docker logs local-redis-container
Images
List images
Display list of images.
docker images
or
docker image ls
Display all image IDs.
docker images -aq
Installing images
docker pull nginx
Building images
docker build -t home-image:1.0.0 -f ./Dockerfile-local .
-t home-image:1.0.0 -home-image is the name, and1.0.0 is a tag-f ./Dockerfile-local - Docker file to use. If not set, docker uses the default file nameDockerfile
Publishing images
docker push
Removing images
Remove Image
docker rmi nginx
or
docker image rm nginx
Force remove
docker image rm -f nginx
Remove multiple images
docker image rm $(docker images -aq)
Dockerfile
Here are the most used instructions:
# Image to build from
FROM <image-name>
# Change working directory.
WORKDIR /path/to/workdir
# Install vim with apt-get
RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "vim"]
# Adds files from your Docker client's directory.
# COPY <source-path> <destination-path>
COPY to-be-copied .
# Set environment variable
ENV MY_NAME="John Doe"
# Use build-time variables.
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV
# Execute command
RUN echo Hello
# Specify what command to run within the container
CMD python /app/app.py
# Describe which ports your application is listening on.
EXPOSE 80/tcp
# Add metadata to an image.
LABEL version="1.0"
LABEL description="This is my website \
and one more line."
The whole list looks like this:
Volumes
Create volume
docker volume create my-volume
Volume mounting
docker run \
--name my-website \
-v ./:/usr/share/nginx/html:ro \
-d \
-p 8080:80 \
nginx
List volumes
docker volume ls
Inspect a volume
docker volume inspect my-volume
output:
[
{
"CreatedAt": "2023-12-27T12:05:54Z",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/my-volume/_data",
"Name": "my-volume",
"Options": null,
"Scope": "local"
}
]
Remove a volume
docker volume rm my-volume
Docker compose
Start
Starts the services defined in the docker-compose.yml file. This command initializes containers based on the specified configuration.
docker-compose start
Stop
Stops the running services and associated containers defined in the docker-compose.yml file. This command gracefully shuts down the containers.
docker-compose stop
Pause
Pauses the running containers defined in the docker-compose.yml file. The containers are temporarily halted, and their state is saved.
docker-compose pause
Unpause
Resumes the execution of previously paused containers defined in the docker-compose.yml file. This command allows the containers to continue their operations.
docker-compose unpause
Ps
Displays the status of containers associated with the services defined in the docker-compose.yml file. It provides information such as container names, IDs, and their current status.
docker-compose ps
Up
Creates and starts the services and containers defined in the docker-compose.yml file. If the services and containers do not exist, this command will build and initialize them.
docker-compose up
Down
Stops and removes the services and containers defined in the docker-compose.yml file. This command also removes associated networks and volumes. It effectively brings the entire environment down.
docker-compose down