Docker
Tags: cheatsheet, docker
Published: 2020-05-14 08:08:15

#### *Original Reference*
*https://docs.docker.com/engine/reference/commandline/docker/*

# General Management
``` bash
# show both docker client and server version
docker version
```
``` bash
# show docker version
docker -v
```
``` bash
# show currently running containers
docker ps
```
<hr />
# Image Management
``` bash
# List locally docker images
docker image ls
```
``` bash
# Remove locally docker image
docker image rm [image]
# OR
docker rmi [image]
```
``` bash
# Build a locally docker image with specified dockerfile location (-f) and image tag (-t)
docker build -f [/path/to/a/Dockerfile] -t [repository/tag] .
```
``` bash
# Make an image from existing container
docker commit [container] [image]
```
``` bash
# Export locally docker image to a file
docker save [image] > [image_file]
```
``` bash
# Load a docker image from file to docker host
docker load < [image_file]
```
``` bash
# Tag a locally image
docker tag [image_id] [repository]:[tag]
```
``` bash
# Rename existing image name and tag
docker image tag [image_name/tag] [new_image_name/new_tag]
```
``` bash
# Login to the Docker Hub account
docker login --username=[account] --email=[email]
```
``` bash
# Push image to Docker Hub
docker push [repository]:[tag]
```
<hr />
# Container Management
``` bash
# List all running containers
docker container ls
```
``` bash
# List all containers includes running or stopped
docker container ls --all
```
``` bash
# Start a new container from a docker image with container name (--name), detach process (-d) and port forward settings (-p)
docker run --name [container_name] -d -p [host_port]:[container_port]/[protocol] [image]
```
``` bash
# Start a new container from a docker image and remove itself when process is exited or closed
docker run --rm hello-world
```
``` bash
# Start a new container and enter to the container /bin/bash
docker run -it ubuntu
```
``` bash
# Start a new container with mounting the host file system
docker run --name [container_name] -it -d --mount src="c:/",target=/some/path,type=bind [image]
```
``` bash
# Execute a command to a running container
docker exec -it [container] ps aux
```
``` bash
# Enter to the /bin/bash of a running container
docker exec -it [container] bash
```
``` bash
# Copy a file from container to local file system
docker cp [container]:[/container/filesystem/file] [/local/filesystem/file]
```
``` bash
# Show the log output from container
docker logs [container]
```
``` bash
# Stop a container
docker stop [container]
```
``` bash
# Start a container
docker start [container]
```
``` bash
# Rename a container
docker rename [container-name] [new-container-name]
```
``` bash
# Remove a stopped container
docker container rm [container]
```
``` bash
# Force to remove a container either running or stopped
docker rm -f [container]
```
``` bash
# Remove all containers
docker container prune
```
``` bash
# Update configuration of one or more containers
docker container update [OPTIONS] CONTAINER [CONTAINER...]
```
<hr />
# Volume Management
``` bash
# List all volumes
docker volume ls
```
``` bash
# Create a new volume
docker volume create [volume_name]
```
<hr />
# Network Management
``` bash
# List all network interfaces
docker network ls
```
<hr />
# Example of Dockerfile
#### Example 1
``` dockerfile
FROM nginx
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
COPY html-src /usr/share/nginx/html
HEALTHCHECK --interval=5s --timeout=3s CMD curl -fs http://localhost:80 || exit 1
```

#### Example 2
``` dockerfile
FROM debian:stable
RUN apt-get update && apt-get install -y --force-yes apache2
EXPOSE 80 443
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
```

#### Example 3
``` dockerfile
FROM ubuntu
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol
```
#### Example 4
``` dockerfile
FROM ubuntu
# RUN will be processed in image build time ONLY
RUN apt-get update
RUN apt-get install git -y
RUN apt-get install nodejs -y
RUN apt-get install npm -y
WORKDIR /app
RUN git clone https://github.com/ssmak/playable.git playable.git
WORKDIR /app/playable.git
RUN npm install
EXPOSE 8080
# CMD will be processed in container start ONLY
CMD ["npm", "start"]
```