Docker Usage
Recommended Reading: 【Development Environment】 Operating System Index
1. Overview
2. Commands
1. Overview
⑴ Overview
① (Differentiating Concept) environment: A bundle where packages are stored
② docker: A container that not only stores packages but also instances that are the intermediate results of computations
⑵ Input Files
①
Dockerfile
: A set of Docker commands
○ Concept similar to source code. The file name must be ‘Dockerfile’
○ If using docker attach, must use SHELL commands
○
RUN
: Installs during the creation of the image
○
CMD
: Installs only once when the image is turned into a container (when the image is executed)
○
ENTRYPOINT
: Like running jupyter
②
build-image.sh
: A set of Linux commands
③
docker-compose.yml
: Configuration environment variables
④
app.py
: Python executable file
⑤
requirements.txt
: Required Python packages
⑶ Docker Creation Results
① image = blueprint
○ A blueprint with all the necessary packages and libraries installed for the desired project or functionality, along with various settings and initial values
○ Can be downloaded from https://hub.docker.com/
○ Examples:
linux/amd64 python:3.9
,linux/amd64 nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04
② container = instance
○ The instance that is actually run or deployed by the user on the host, based on the image
③ node or host
○ The device or environment running Docker
2. Commands
○ Install Docker
○ Step 1. Install docker-ce
# For Ubuntu
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce
# For CentOS
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce
# For Fedora
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install docker-ce
○ Step 2. Start docker
sudo systemctl start docker
○ Step 3. (
Optional
) Enable docker to start on boot
sudo systemctl enable docker
○ Step 4. Verify docker installation
sudo docker --version
○ Step 5. Run a container
# run a test container
sudo docker run hello-world
# run my new container 'jb'
sudo docker run -dit --name jb ubuntu
○ Step 6. Pull an image: Followed by the repository URL and image tag
sudo docker pull ubuntu:20.04
○ Step 7. Run a container from the image
sudo docker run -it --name my_container_name ubuntu:20.04 /bin/bash
○ Docker Commands:
○
docker images
: List Docker images.
○
docker build
: Generates a Docker container.
○
docker run
: Executes a Docker container.
○
docker ps
: List Docker containers. When you rundocker ps -a
, you can also see stopped containers.
○
docker attach Container_ID
: Enter a specific Docker container.
○
docker exec Container_ID
: Passes commands to a running Docker container. Almost similar todocker attach
.
○
docker stats
: Check the resources allocated to each container.
○
docker stop ID
: Stop a specific container.
○
docker start ID
: Start a specific container.
○
docker rm ID
: Remove a specific container.
○
docker rmi ID
: Remove a specific Docker image. Containers that use the image must be removed first.
○
docker cp <CONTAINER_ID>:<CONTAINER_PATH> <HOST_PATH>
: Copy files from inside the container to the host.
○
docker cp <HOST_PATH> <CONTAINER_ID>:<CONTAINER_PATH>
: Copy files from the host to the container.
○ Shortcuts:
○
ctrl + c
: When a specific Docker container is active, pressing this key combination will stop that Docker container.
○
ctrl + p + q
: When a specific Docker container is active, pressing this key combination will detach from the active Docker container.
3. Miscellaneous
⑴ Docker Web GUI Management Tool
Input: 2023.06.03 01:28