Docker
Steve Mak
Posted on May 14, 2020
Original Reference
https://docs.docker.com/engine/reference/commandline/docker/
General Management
# show both docker client and server version
docker version
# show docker version
docker -v
# show currently running containers
docker ps
Image Management
# List locally docker images
docker image ls
# Remove locally docker image
docker image rm [image]
# OR
docker rmi [image]
# Build a locally docker image with specified dockerfile location (-f) and image tag (-t)
docker build -f [/path/to/a/Dockerfile] -t [repository/tag] .
# Make an image from existing container
docker commit [container] [image]
# Export locally docker image to a file
docker save [image] > [image_file]
# Load a docker image from file to docker host
docker load < [image_file]
# Tag a locally image
docker tag [image_id] [repository]:[tag]
# Rename existing image name and tag
docker image tag [image_name/tag] [new_image_name/new_tag]
# Login to the Docker Hub account
docker login --username=[account] --email=[email]
# Push image to Docker Hub
docker push [repository]:[tag]
Container Management
# List all running containers
docker container ls
# List all containers includes running or stopped
docker container ls --all
# 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]
# Start a new container from a docker image and remove itself when process is exited or closed
docker run --rm hello-world
# Start a new container and enter to the container /bin/bash
docker run -it ubuntu
# 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]
# Execute a command to a running container
docker exec -it [container] ps aux
# Enter to the /bin/bash of a running container
docker exec -it [container] bash
# Copy a file from container to local file system
docker cp [container]:[/container/filesystem/file] [/local/filesystem/file]
# Show the log output from container
docker logs [container]
# Stop a container
docker stop [container]
# Start a container
docker start [container]
# Rename a container
docker rename [container-name] [new-container-name]
# Remove a stopped container
docker container rm [container]
# Force to remove a container either running or stopped
docker rm -f [container]
# Remove all containers
docker container prune
# Update configuration of one or more containers
docker container update [OPTIONS] CONTAINER [CONTAINER...]
Volume Management
# List all volumes
docker volume ls
# Create a new volume
docker volume create [volume_name]
Network Management
# List all network interfaces
docker network ls
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
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"]
Example 3
FROM ubuntu
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol
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"]
💖 💪 🙅 🚩
Steve Mak
Posted on May 14, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.