2.1.2 Running commands in Docker
View in the book.
Buy the book.
Create a Bash shell in docker
$ docker run -it ubuntu bash
root@18e78382b32e:/#
Install python
apt-get update
apt-get install -y python3
Run python interactively
# python3
>>> print("Hello Docker")
Hello Docker
>>> exit()
#
Create a Python script and run it
# echo 'print("Hello Docker")' > hello.py
# python3 hello.py
Hello Docker
When you’re done:
root@5aa832cc450b:/# exit
exit
List docker images
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5aa832cc450b ubuntu "bash" 4 minutes ago Exited (127) 15 seconds ago blissful_clarke
Start an existing image and attach
$ CONTAINER_ID=c5e023cab033
$ docker start $CONTAINER_ID
$ docker attach $CONTAINER_ID
# echo "run more commands"
# exit
Delete images
$ docker system prune -a
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all images without at least one container associated to them
- all build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
5aa832cc450b21238fd9e136e42a313ed6560e9aa3b09d7e6bf7413a4b04af3b
Deleted Images:
untagged: ubuntu:latest
untagged: ubuntu@sha256:2b7412e6465c3c7fc5bb21d3e6f1917c167358449fecac8176c6e496e5c1f05f
deleted: sha256:e4c58958181a5925816faa528ce959e487632f4cfd192f8132f71b32df2744b4
deleted: sha256:256d88da41857db513b95b50ba9a9b28491b58c954e25477d5dad8abb465430b
Total reclaimed space: 153.6MB