Docker
Docker
1. Docker Engine: The core of Docker is the Docker Engine, which is responsible
for building, running, and managing containers. It consists of a server
(daemon) and a REST API that allows you to interact with the daemon.
Now, let's look at some common Docker commands and their usage:
1. Docker Build
• Command:
2. Docker Run
• Command:
• Command:
4. Docker Push
• Command:
5. Docker PS
• Command:
docker ps [options]
6. Docker Images
• Command:
7. Docker Stop
• Command:
• Command:
• Command:
docker rm container_id
• Command:
# Install dependencies
RUN apt-get update && \
apt-get install -y python3 && \
apt-get clean
# Expose a port
EXPOSE 8080
1. FROM: Specifies the base image. In this case, it's Ubuntu 20.04.
2. WORKDIR: Sets the working directory inside the container. Subsequent
instructions will be executed from this directory.
3. COPY: Copies files from the host machine to the container. Here, it copies the
content of the current directory (the "." represents the current directory) into
the "/app" directory inside the container.
4. RUN: Executes commands during the image build process. In this example, it
updates the package list, installs Python3, and cleans up the package cache.
5. EXPOSE: Informs Docker that the container will listen on the specified network
ports at runtime. It doesn't actually publish the ports, but it is a helpful
documentation for other developers.
6. CMD: Provides the default command to run when the container starts. It
specifies the command to run our application (in this case, a Python script).
Now, let's move on to a multi-stage Dockerfile example. Multi-stage builds allow you
to use multiple FROM statements in a single Dockerfile, resulting in a smaller and more
efficient final image.
In this example:
1. The first stage (build) uses the Node.js image to build a JavaScript/Node.js
application. It copies the necessary files, installs dependencies, and runs the
build process.
2. The second stage uses the smaller Nginx image and copies the output from
the build stage into the appropriate directory. This results in a final image
containing only the compiled application and the Nginx server.
This approach helps reduce the size of the final image by excluding unnecessary
build dependencies from the production stage. The --from=build flag in
the COPY command allows copying files from a specific build stage.
Docker Networking
Docker provides a networking model that allows containers to communicate with
each other and with the outside world. Docker networking involves creating
networks, connecting containers to networks, and managing the flow of traffic
between containers. Here are some Docker networking concepts along with
examples and commands:
docker network ls
docker network ls
5. Docker Inspect:
Inspecting a network provides detailed information about the network configuration.
• Inspect a Network:
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
docker-compose up -d
This command reads the docker-compose.yml file, creates the defined services,
and starts the containers in the background (-d flag).
• Stop Services:
docker-compose down
This command stops and removes the containers defined in the docker-
compose.yml file.
• View Logs:
docker-compose logs
• List Services:
docker-compose ps
3. Environment Variables:
You can set environment variables for your services in the docker-compose.yml file:
services:
web:
image: nginx
environment:
- MY_VARIABLE=value
4. Volumes:
Define volumes for persistent storage:
services:
web:
image: nginx
volumes:
- ./html:/usr/share/nginx/html
This maps the local ./html directory to the /usr/share/nginx/html directory in the
container.
5. Networks:
Specify custom networks:
networks:
my_network:
driver: bridge
services:
web:
image: nginx
networks:
- my_network
6. Dependencies and Restart Policies:
services:
web:
image: nginx
depends_on:
- db
restart: always
db:
image: postgres
The depends_on key ensures that the web service starts only after the db service is up.
The restart: always policy ensures that the container restarts automatically.
Docker Volumes
Docker volumes are a way to persist and share data between Docker containers. They
provide a mechanism for managing data that needs to live beyond the lifetime of a
container. Here's an overview of Docker volumes along with examples and
commands:
1. Creating Volumes:
• Creating a Named Volume:
3. Inspecting a Volume:
docker volume inspect my_volume
5. Bind Mounts:
Bind mounts allow you to mount a directory from the host into a container.
• version: '3'
• services:
• web:
• image: nginx
• volumes:
• - my_volume:/path/in/container
• volumes:
my_volume:
• Removing a Volume:
These commands and examples cover the basic usage of Docker volumes. Volumes
play a crucial role in Docker for persistent data storage and sharing between
containers, making it easier to manage data in containerized applications.
50 Docker Errors & Solutions
Docker errors can arise from various issues, and troubleshooting them can
sometimes be challenging. Here are 50 common Docker errors along with potential
solutions:
o Solution: Follow the correct installation instructions for your Linux distribution.
o Solution: Ensure the command exists in the container or check the container
image.
o Solution: Ensure the service inside the container is running or check firewall
settings.
o Solution: Ensure the command exists or check the path within the container.
15. Error: Cannot create container for service XXX: network XXX not found
18. Error: Got permission denied while trying to connect to the Docker
daemon socket at unix:///var/run/docker.sock
19. Error: image operating system "XXX" cannot be used on this platform
20. Error: Container command 'XXX' not found or does not exist
o Solution: Choose a different port or stop the service using that port.
25. Error: Cannot connect to the Docker daemon. Is 'docker -d' running on
this host?
26. Error: The requested image's platform (linux/amd64) does not match the
detected host platform (linux/arm64)
27. Error: no matching manifest for XXX in the manifest list entries
29. Error: ERROR: for service Cannot start service XXX: driver failed
programming external connectivity
o Solution: Release the port from another service or choose a different one.
30. Error: error while creating mount source path '/host/path': mkdir
/host/path: file exists
o Solution: Pull the image with docker pull before running the container.
32. Error: rpc error: code = 2 desc = "oci runtime error: exec format error"
35. Error: Dockerfile parse error line XXX: unknown instruction: YYY
o Solution: The specified network should exist or create it using docker network
create.
38. Error: Unable to load the image. The file is too big.
o Solution: Optimize the image size or use a more lightweight base image.
39. Error: Unable to find a node that satisfies the following conditions
42. Error: failed to register layer: Error processing tar file(exit status 1): write
/path/in/container: no space left on device
43. Error: The server requested authentication method unknown to the client
[tls]
o Solution: Remove or rename the existing container with the conflicting name.
o Solution:
48. Error: error parsing HTTP 403 response body: invalid character '<'
looking for beginning of value: "...