Docker Cheat Sheet
Tags: docker, dockercommands, dockercheatsheet
Published: 2023-12-21 07:05:05


[![Docker](https://plus.unsplash.com/premium_photo-1661962427464-d3362324a45d?w=500&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTd8fGRvY2tlcnxlbnwwfHwwfHx8MA%3D%3D "Docker")](https://plus.unsplash.com/premium_photo-1661962427464-d3362324a45d?w=500&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTd8fGRvY2tlcnxlbnwwfHwwfHx8MA%3D%3D "Docker")

Dockerfile
----------

A Dockerfile is a script used to build Docker images. It consists of a series of commands:

**FROM**

`FROM image_name:tag`

_Description:_ Specifies the base image used to build the new image.

**RUN**

`RUN command`

_Description:_ Executes a command in the new image's filesystem during the build process.

**CMD**

`CMD command`

_Description:_ Specifies the default command to run when a container is launched from the image.

**WORKDIR**

`WORKDIR /path/to/directory`

_Description:_ Sets the working directory for subsequent instructions.

**COPY**

`COPY source destination`

_Description:_ Copies files or directories from the host system into the image.

**ADD**

`ADD source destination`

_Description:_ Similar to COPY, but can also extract tarballs or fetch URLs.

**EXPOSE**

`EXPOSE port`

_Description:_ Informs Docker that the container listens on specific network ports at runtime.

**ENV**

`ENV key=value`

_Description:_ Sets environment variables in the image.

**ENTRYPOINT**

`ENTRYPOINT command`

_Description:_ Configures the container to run as an executable.

**ARG**

`ARG variable_name`

_Description:_ Defines a variable that users can pass at build-time to the builder with the docker build command.

**USER**

`USER username`

_Description:_ Sets the user the application will run as within the container.

Docker Registry
---------------

Login to a Docker registry:

`docker login`

_Description:_ Logs in to a Docker registry interactively.

Logout from a Docker registry:

`docker logout`

_Description:_ Logs out from a Docker registry.

Build an image from a Dockerfile:

`docker build -t image_name .`

_Description:_ Builds a Docker image from a Dockerfile in the current directory.

Push an image to a Docker registry:

`docker push image_name`

_Description:_ Pushes a Docker image to a Docker registry.

Docker Containers
-----------------

Run a container interactively:

`docker run -it image_name`

_Description:_ Starts a new container interactively using the specified image.

Run a detached container:

`docker run -d image_name`

_Description:_ Starts a new container in detached mode (background).

Run a container with a custom name:

`docker run --name custom_name image_name`

_Description:_ Starts a new container with a specific name.

Run a container with environment variables:

`docker run -e "VAR=value" image_name`

_Description:_ Sets environment variables in the container.

Run a container and map ports:

`docker run -p host_port:container_port image_name`

_Description:_ Maps a container port to a host port.

Run a container and mount a volume:

`docker run -v /host/path:/container/path image_name`

_Description:_ Mounts a host directory into the container.

Stop a running container:

`docker stop container_id`

_Description:_ Stops a running container.

Start a stopped container:

`docker start container_id`

_Description:_ Starts a stopped container.

Pause a running container:

`docker pause container_id`

_Description:_ Pauses the execution of a container.

Unpause a paused container:

`docker unpause container_id`

_Description:_ Resumes a paused container.

Restart a container:

`docker restart container_id`

_Description:_ Restarts a running or stopped container.

Attach to a running container:

`docker exec -it container_id /bin/bash`

_Description:_ Attaches to a running container and opens a shell.

Remove a stopped container:

`docker rm container_id`

_Description:_ Deletes a stopped container.

Remove a running container:

`docker rm -f container_id`

_Description:_ Forces removal of a running container.

Show container resource usage:

`docker stats container_id`

_Description:_ Displays live resource usage statistics of a container.

Show container logs:

`docker logs container_id`

_Description:_ Displays logs from a container.

Copy files from/to a container:

`docker cp /local/path container_id:/container/path`

_Description:_ Copies files between a container and the local filesystem.

Inspect a container:

`docker inspect container_id`

_Description:_ Shows detailed information about a container.

Create a container without starting it:

`docker create image_name`

_Description:_ Creates a new container without starting it.

Docker Networking
-----------------

List networks:

`docker network ls`

_Description:_ Lists all networks created on the Docker host.

Create a network:

`docker network create my_network`

_Description:_ Creates a user-defined bridge network named 'my\_network'.

Inspect a network:

`docker network inspect my_network`

_Description:_ Displays detailed information about the 'my\_network' network.

Remove a network:

`docker network rm my_network`

_Description:_ Removes the 'my\_network' network.

Connect a container to a network:

`docker network connect my_network container_name`

_Description:_ Connects a container to the 'my\_network' network.

Disconnect a container from a network:

`docker network disconnect my_network container_name`

_Description:_ Disconnects a container from the 'my\_network' network.

Attach a container to multiple networks:

`docker network connect my_network_2 container_name`

_Description:_ Attaches the container to an additional network 'my\_network\_2'.

Create an overlay network:

`docker network create --driver overlay my_overlay_network`

_Description:_ Creates a multi-host overlay network 'my\_overlay\_network'.

Remove all unused networks:

`docker network prune`

_Description:_ Removes all networks not used by at least one container.

Set up a custom bridge network:

`docker network create --driver bridge --subnet=192.168.5.0/24 --gateway=192.168.5.1 my_custom_network`

_Description:_ Creates a custom bridge network 'my\_custom\_network' with specified subnet and gateway.

Limit container bandwidth on a network:

`docker network create --driver bridge --subnet=192.168.5.0/24 --gateway=192.168.5.1 --opt "com.docker.network.bridge.enable_icc=false" my_secure_network`

_Description:_ Creates a secure bridge network 'my\_secure\_network' disabling inter-container communication.

Docker Volumes
--------------

List volumes:

`docker volume ls`

_Description:_ Lists all volumes on the Docker host.

Create a volume:

`docker volume create my_volume`

_Description:_ Creates a named volume named 'my\_volume'.

Inspect a volume:

`docker volume inspect my_volume`

_Description:_ Displays detailed information about the 'my\_volume' volume.

Remove a volume:

`docker volume rm my_volume`

_Description:_ Removes the 'my\_volume' volume.

Remove all unused volumes:

`docker volume prune`

_Description:_ Removes all volumes not used by at least one container.

Mount a volume while running a container:

`docker run -v my_volume:/path/in/container image_name`

_Description:_ Mounts the 'my\_volume' volume to a specific path inside the container.

Mount a host directory as a volume:

`docker run -v /host/path:/container/path image_name`

_Description:_ Mounts a directory from the host system into the container.

Specify volume driver:

`docker volume create --driver my_driver my_volume`

_Description:_ Creates a volume using a specific volume driver 'my\_driver'.

Copy files from a container volume to the local host:

`docker cp container_id:/path/in/container /local/host/path`

_Description:_ Copies files from a container volume to a directory on the local host.

Mount a read-only volume:

`docker run -v my_volume:/path/in/container:ro image_name`

_Description:_ Mounts the 'my\_volume' volume as read-only inside the container.

Mount a named volume with specific options:

`docker run -v my_volume:/path/in/container:options image_name`

_Description:_ Mounts the 'my\_volume' volume with specific options (e.g., read-write permissions).

Backup a volume to a tar archive:

`docker run --rm -v my_volume:/data -v /backup:/backup ubuntu tar cvf /backup/my_volume_backup.tar /data`

_Description:_ Creates a tar archive of the 'my\_volume' volume in the /backup directory.

Docker Compose
--------------

Create and start containers:

`docker-compose up`

_Description:_ Builds, (re)creates, starts, and attaches to containers for a service.

Stop and remove containers:

`docker-compose down`

_Description:_ Stops and removes containers, networks, volumes, and images created by 'up' command.

Build services:

`docker-compose build`

_Description:_ Builds or rebuilds services defined in the docker-compose.yml file.

Start services:

`docker-compose start`

_Description:_ Start services defined in the docker-compose.yml file.

Stop services:

`docker-compose stop`

_Description:_ Stops services defined in the docker-compose.yml file without removing containers.

Restart services:

`docker-compose restart`

_Description:_ Restarts services defined in the docker-compose.yml file.

Pause services:

`docker-compose pause`

_Description:_ Pauses all services in the docker-compose.yml file.

Unpause services:

`docker-compose unpause`

_Description:_ Unpauses all paused services in the docker-compose.yml file.

View service logs:

`docker-compose logs`

_Description:_ Displays log output from services.

Scale services:

`docker-compose scale service_name=num_of_instances`

_Description:_ Scale services to a specified number of instances.