Docker in Practice
Getting Started with Docker: A Brief and Practical Introduction
Docker allows you to deploy apps without setup difficulties or dependency issues. It packages everything into portable containers that run stable across environments. Simplify your deployment, scale applications, and ensure consistent behavior!
🐳 Docker Hub
- A public registry (platform) for downloading a docker image
- Ex.: A docker image that includes Ubuntu with CUDA 10.2 pre-installed
🐳 The Docker Pipeline
✅ Simple Start from a Dockerfile
- Build a docker image from a Dockerfile:
docker build -t towards-simple-world docker/ # build an image and tag it as 'towards-simple-world'
# 'docker/' is the folder where the Dockerfile is located
# or (if the Dockerfile uses arguments):
docker build --build-arg IMAGE_NAME=nvidia/cuda -t towards-simple-world docker/
- Run a docker container from the docker image:
docker run --rm # Automatically remove the container when it exits
--gpus all # Give the container access to all available GPUs
-v $(pwd)/:/app # Mount current directory to /app in the container
-w /app # Set working directory to /app
-ti # Allow a terminal session and keep stdin open for input
-e DISPLAY=$DISPLAY # Pass the display environment variable
-v /tmp/.X11-unix:/tmp/.X11-unix # Mounts the X11 socket
towards-simple-world # Image tag
/bin/bash # Open the bash shell inside the starting container
- Run a python program inside the docker container:
python file.py # The container is running, now you can execute the Python file
✅ Docker in PyCharm with Debugging Support
To debug and run the program in PyCharm as usual, you need to create the correct Python Interpreter.
- Open Interpreter Settings
- Go to File → Settings (
Ctrl + Alt + S
) - Navigate to Project → Python Interpreter
- Click Add Interpreter → select Docker
- Go to File → Settings (
- Configure Docker Interpreter
- Image: select the desired docker image
- Python Interpreter Path:
python
- Interpreter Type:
Remote Python Docker
- Configure Run/Debug Configuration
- Go to Run → Edit Configurations
- Click Add New → choose Python
- Set up as usual, but make sure to select the docker interpreter
- Working directory:
/app
- Path mappings:
- Project root →
/app
- Project root →
- Docker Container Settings
- Click the Configuration button in Docker settings
- Add docker run options:
--rm --gpus all -v $(pwd)/:/app -w /app
🛠️ Most Useful Docker Commands
Command | Description |
---|---|
docker build -t my-image . |
Build a docker image from a Dockerfile located in the current directory and tag it (-t) as my-image |
docker run -ti <more options> my-image /bin/bash |
Run a container from a docker image with different options, including terminal allocation (-t), keeping stdin open (-i) for input, and launching the bash shell |
Check It | |
docker images |
List all local docker images |
docker ps |
List all running containers |
docker ps -a |
List all containers, including stopped ones |
docker logs <container-id> |
View logs of a container |
docker top <container_id> |
Show running processes inside container |
docker volume ls |
List volumes |
Modify and Save | |
docker pull <image> |
Download a docker image from DockerHub |
docker push <image> |
Upload a docker image to DockerHub |
docker tag <source> <target> |
Tag an image with a new name or version. |
docker commit <container_id> my-image |
Save changes inside a running container (e.g., installed packages) to a docker image |
Cleanup | |
docker stop <container-id> |
Stop a running container |
docker rm <container-id> |
Remove a stopped container |
docker rmi <image-id> |
Remove a docker image |
docker system prune |
Remove unused data (containers, images, networks) |
docker image prune |
Remove unused images |
docker container prune |
Remove stopped containers |