Docker Cheatsheet for Beginners
Tags: docker, devops
Published: 2023-09-26 03:25:32


Docker Cheatsheet for Beginners (in Detail)

Docker Concepts

Docker image: A read-only template that contains everything needed to run a specific application, including the application code, system libraries, and runtime environment. Docker images are built using Dockerfiles, which are text files that contain instructions for building the image.
Docker container: A running instance of a Docker image. Containers are isolated from each other and from the host machine, making them lightweight and portable. Containers can be started, stopped, and restarted quickly and easily.
Docker registry: A central repository for storing and distributing Docker images. Docker Hub is the official Docker registry, but there are also many private and third-party registries available.
Docker Commands

docker build: Builds a Docker image from a Dockerfile.
docker run: Creates and runs a container from a Docker image.
docker ps: Lists all running containers.
docker stop: Stops a running container.
docker rm: Removes a container.
docker images: Lists all local Docker images.
docker rmi: Removes a Docker image.
docker pull: Pulls a Docker image from a registry.
docker push: Pushes a Docker image to a registry.
Useful Docker Flags

-d: Detaches the container from the terminal, allowing it to run in the background.
-p: Publishes a container port to the host machine. This allows you to access the application running in the container from the host machine.
-v: Mounts a host directory to a container directory. This allows you to share data between the host machine and the container.
-e: Sets an environment variable in the container. This can be useful for configuring the application running in the container.
Example Docker Command

To run a simple web application in a Docker container, you would use the following command:

docker run -d -p 8080:80 nginx
This command will create and run a container from the nginx image, publishing port 80 of the container to port 80 of the host machine. This means that you will be able to access the web application at http://localhost:8080.

Dockerfile

A Dockerfile is a text file that contains instructions for building a Docker image. Dockerfiles are typically divided into stages, each of which performs a specific task, such as installing dependencies, copying files, or running commands.

Here is an example of a simple Dockerfile for a web application:

FROM nginx

COPY . /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
This Dockerfile will build an image from the nginx base image and copy the current directory to the /usr/share/nginx/html directory in the container. It will then expose port 80 of the container and start the nginx server with the -g flag to disable daemon mode.

Docker Compose

Docker Compose is a tool that allows you to define and manage multiple Docker containers as a single service. This is useful for running complex applications that require multiple containers to work together.

To use Docker Compose, you create a YAML file called docker-compose.yml that defines your services. For example, the following docker-compose.yml file defines a simple web application with a backend and frontend service:

YAML
version: "3.7"

services:
  backend:
    image: nginx
    volumes:
      - ./backend:/usr/share/nginx/html

  frontend:
    image: nginx
    volumes:
      - ./frontend:/usr/share/nginx/html
    depends_on:
      - backend

ports:
  - "80:80"
Use code with caution. Learn more
To build and run the application, you would simply run the following command:

docker-compose up -d
This will build the images for the backend and frontend services and then start the containers. You will be able to access the web application at http://localhost:80.

Troubleshooting Docker

If you are having problems with Docker, there are a few things you can check:

Make sure that Docker is installed and running correctly. You can check this by running the docker info command.
Make sure that you have the required permissions to run Docker. You may need to run Docker with sudo.
Make sure that you have enough disk space to build and run Docker images and containers.
Check the Docker documentation or online forums for help with specific problems.