Last Updated: September 30, 2021
·
140.9K
· tcnksm

Docker Cheat Sheet with examples

Original post is here

Docker Cheat Sheet is a nice documentation. It provides us Docker basic commands and system and It's easy to understand. But there are less exaples, I reconstructed it with real examples. You should refer above document about installation.

Set up

Pull a base image.

docker pull ubuntu

It's annoy to restore Container ID, you may forget to restore. You can set below alias. With this, you can get the ID of the last-run Container (15 Docker tips in 5 minutes)

alias dl='docker ps -l -q'

Container

To create a Container.

docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"

To stop a Container.

docker stop `dl`

To start a Container.

docker start `dl`

To restart a Container.

docker restart `dl`

To Connect to a running Container.

docker attach `dl`

To copy file in a Container to the host.

docker cp `dl`:/etc/passwd .

To mount the directory in host to a Container.

docker run -v /home/vagrant/test:/root/test ubuntu echo yo

To delete a Container.

dockr rm `dl`

Info of Container

To show running Containers. With -a option, it shows running and stopped Containers.

docker ps

To show Container information like IP adress.

docker inspect `dl`

To show log of a Container.

docker logs `dl`

To show running process in a Container.

docker top `dl`

Image

To create a image from a Container. For tag name, <username>/<imagename> is recommended.

docker run -d ubuntu /bin/sh -c "apt-get install -y hello"
docker commit -m "My first container" `dl` tcnksm/hello

To create a image with Dockerfile.

echo -e "FROM base\nRUN apt-get install hello\nCMD hello" > Dockerfile
docker build tcnksm/hello .

To login to a image.

docker run -rm -t -i tcnksm/hello /bin/bash

To push a imges to remote repository. You need to sign up to Docker index in advance. Exmple uploaded image.

docker login
docker push tcnksm/hello

To delete a image

docker rmi tcnkms/hello

Info of Image

To show all images

docker images

To show image information like IP adress.

docker inspect tcnksm/hello

To show command history of a image.

docker history tcnksm/hello

3 Responses
Add your response

I spent an hour or so working with Docker, and while their documentation says "not for production"...I see a bunch of companies just pushing this technology forward.

Good stuffs and super easy to use and understand.

over 1 year ago ·

Thank you for your comment.

Yeah, but now Docker's version is just 0.7. So big change may be happened in next version. This is because "not for production", I think.

But I think this technology will change development environment and need to be used in production soon.

over 1 year ago ·

There are at least two more options to conveniently retain a Docker container ID:

# Assign to a shell variable.
DOCKER_CID=$(docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")
# Specify a CID file.
docker run -d -cidfile /tmp/my_container.cid ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
# then elsewhere one could just
cat /tmp/my_container.cid

In my opinion the second option is particularly useful when one uses named containers - a CID file could always be named after the container name and easily referenced that way.

over 1 year ago ·