DEV Community

Steve Mak
Steve Mak

Posted on • Updated on

Docker

Original Reference

https://docs.docker.com/engine/reference/commandline/docker/

General Management

# show both docker client and server version
docker version
Enter fullscreen mode Exit fullscreen mode
# show docker version
docker -v
Enter fullscreen mode Exit fullscreen mode
# show currently running containers
docker ps
Enter fullscreen mode Exit fullscreen mode

Image Management

# List locally docker images
docker image ls
Enter fullscreen mode Exit fullscreen mode
# Remove locally docker image
docker image rm [image]
# OR
docker rmi [image]
Enter fullscreen mode Exit fullscreen mode
# Build a locally docker image with specified dockerfile location (-f) and image tag (-t)
docker build -f [/path/to/a/Dockerfile] -t [repository/tag] .
Enter fullscreen mode Exit fullscreen mode
# Make an image from existing container
docker commit [container] [image]
Enter fullscreen mode Exit fullscreen mode
# Export locally docker image to a file
docker save [image] > [image_file]
Enter fullscreen mode Exit fullscreen mode
# Load a docker image from file to docker host
docker load < [image_file]
Enter fullscreen mode Exit fullscreen mode
# Tag a locally image
docker tag [image_id] [repository]:[tag]
Enter fullscreen mode Exit fullscreen mode
# Rename existing image name and tag
docker image tag [image_name/tag] [new_image_name/new_tag]
Enter fullscreen mode Exit fullscreen mode
# Login to the Docker Hub account
docker login --username=[account] --email=[email]
Enter fullscreen mode Exit fullscreen mode
# Push image to Docker Hub
docker push [repository]:[tag]
Enter fullscreen mode Exit fullscreen mode

Container Management

# List all running containers
docker container ls
Enter fullscreen mode Exit fullscreen mode
# List all containers includes running or stopped
docker container ls --all
Enter fullscreen mode Exit fullscreen mode
# 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]
Enter fullscreen mode Exit fullscreen mode
# Start a new container from a docker image and remove itself when process is exited or closed
docker run --rm hello-world
Enter fullscreen mode Exit fullscreen mode
# Start a new container and enter to the container /bin/bash
docker run -it ubuntu
Enter fullscreen mode Exit fullscreen mode
# 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]
Enter fullscreen mode Exit fullscreen mode
# Execute a command to a running container
docker exec -it [container] ps aux
Enter fullscreen mode Exit fullscreen mode
# Enter to the /bin/bash of a running container
docker exec -it [container] bash
Enter fullscreen mode Exit fullscreen mode
# Copy a file from container to local file system
docker cp [container]:[/container/filesystem/file] [/local/filesystem/file]
Enter fullscreen mode Exit fullscreen mode
# Show the log output from container
docker logs [container]
Enter fullscreen mode Exit fullscreen mode
# Stop a container
docker stop [container]
Enter fullscreen mode Exit fullscreen mode
# Start a container
docker start [container]
Enter fullscreen mode Exit fullscreen mode
# Rename a container
docker rename [container-name] [new-container-name]
Enter fullscreen mode Exit fullscreen mode
# Remove a stopped container
docker container rm [container]
Enter fullscreen mode Exit fullscreen mode
# Force to remove a container either running or stopped
docker rm -f [container]
Enter fullscreen mode Exit fullscreen mode
# Remove all containers
docker container prune
Enter fullscreen mode Exit fullscreen mode
# Update configuration of one or more containers
docker container update [OPTIONS] CONTAINER [CONTAINER...]
Enter fullscreen mode Exit fullscreen mode

Volume Management

# List all volumes
docker volume ls
Enter fullscreen mode Exit fullscreen mode
# Create a new volume
docker volume create [volume_name]
Enter fullscreen mode Exit fullscreen mode

Network Management

# List all network interfaces
docker network ls
Enter fullscreen mode Exit fullscreen mode

Example of Dockerfile

Example 1

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
Enter fullscreen mode Exit fullscreen mode

Example 2

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"]
Enter fullscreen mode Exit fullscreen mode

Example 3

FROM ubuntu
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol
Enter fullscreen mode Exit fullscreen mode

Example 4

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"]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)