Docker

Docker CLI

Basic Commands

  • docker --version

    • Check Docker version.

    docker --version
  • docker info

    • Display system-wide information.

    docker info
  • docker help

    • Get help on Docker commands.

    docker help

Images

  • docker images

    • List all Docker images.

    docker images
  • docker pull [image]

    • Pull an image from a registry.

    docker pull ubuntu:latest
  • docker rmi [image_id]

    • Remove a Docker image.

    docker rmi ubuntu:latest

Containers

  • docker ps

    • List running containers.

  • docker ps -a

    • List all containers (running and stopped).

  • docker run [options] [image]

    • Run a command in a new container.

  • docker stop [container_id]

    • Stop a running container.

  • docker start [container_id]

    • Start a stopped container.

  • docker restart [container_id]

    • Restart a container.

  • docker rm [container_id]

    • Remove a stopped container.

Networks

  • docker network ls

    • List all networks.

  • docker network create [network_name]

    • Create a new network.

  • docker network rm [network_name]

    • Remove a network.

Volumes

  • docker volume ls

    • List all volumes.

  • docker volume create [volume_name]

    • Create a new volume.

  • docker volume rm [volume_name]

    • Remove a volume.

Docker Compose

  • docker-compose up

    • Create and start containers.

  • docker-compose down

    • Stop and remove containers, networks, images, and volumes.

  • docker-compose build

    • Build or rebuild services.

Inspect and Logs

  • docker inspect [container_id]

    • Return low-level information on Docker objects.

  • docker logs [container_id]

    • Fetch the logs of a container.

Clean Up

  • docker system prune

    • Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

  • docker container prune

    • Remove all stopped containers.

  • docker volume prune

    • Remove all unused volumes.

  • docker image prune

    • Remove unused images.

This cheat sheet covers the basic and most commonly used Docker commands to get you started. Feel free to ask if you need more details or advanced commands!

Creating a Dockerfile

A Dockerfile is a text file that contains a series of instructions on how to build a Docker image. Here's a basic overview and examples of common Dockerfile instructions:

Basic Structure of a Dockerfile

  1. FROM

    • Specifies the base image.

  2. MAINTAINER

    • Sets the author field of the generated images.

  3. RUN

    • Executes commands in a new layer on top of the current image.

  4. COPY

    • Copies files from the host machine to the Docker image.

  5. ADD

    • Copies files/directories from the host machine to the Docker image, and also supports extracting tar files.

  6. CMD

    • Specifies the command to run within the container.

  7. ENTRYPOINT

    • Sets a default application to be used every time a container is created with the image.

  8. EXPOSE

    • Informs Docker that the container listens on the specified network ports at runtime.

  9. ENV

    • Sets environment variables.

  10. VOLUME

    • Creates a mount point with the specified path and marks it as holding externally mounted volumes from the native host or other containers.

Example Dockerfile

Here's an example Dockerfile for a simple web server using Nginx:

Building and Running the Docker Image

  1. Build the Docker Image

    • Use the docker build command to create an image from the Dockerfile.

  2. Run the Docker Container

    • Use the docker run command to start a container from the image.

Best Practices

  • Keep Dockerfile Instructions Ordered: Use a logical order such as FROM, MAINTAINER, RUN, COPY, CMD.

  • Use .dockerignore: Create a .dockerignore file to exclude files and directories from the build context to reduce the size of the image.

  • Minimize Layers: Combine multiple RUN commands to reduce the number of layers.

  • Leverage Caching: Order the instructions to leverage Docker’s build cache.

This should give you a good starting point for creating your own Dockerfiles! If you have any specific questions or need further details, feel free to ask.

Last updated

Was this helpful?