Docker Snippets

Summary

In this post, I will present some simple but useful Docker snippets.

Details

1. Image

Show all images.

(base) zhoun14@zhoun14:~$ sudo docker images
[sudo] password for zhoun14: 
REPOSITORY                          TAG                         IMAGE ID               CREATED             SIZE
nanzhoumails/ubuntu-images    pure-sudo-ubuntu    f279d4808a3d        About an hour ago   115MB

Delete an image.

(base) zhoun14@zhoun14:~$ sudo docker image rm nanzhoumails/ubuntu-images:pure-sudo-ubuntu
Untagged: nanzhoumails/ubuntu-images:pure-sudo-ubuntu
Untagged: nanzhoumails/ubuntu-images@sha256:feeb413b7c4972111ceff1aedc99e6bc3c8a8cd33d975dc0280540b7a0482215
Deleted: sha256:f279d4808a3d861a5e2516cd23a86952b023f5348cad3aa97d35affa173df6db
Deleted: sha256:0d1f7f7d3080fb95c5c8df968f38d845b840febf1ccae02525fee8d5900937e2
(base) zhoun14@zhoun14:~$ sudo docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE

Run a container from a image foreground without clean up (–rm).

(base) zhoun14@zhoun14:~$ sudo docker run -it --name my_ubuntu ubuntu
root@47d83458b7f0:/#

2. Container

Show all containers.

(base) zhoun14@zhoun14:~$ sudo docker container ls -a
CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS                     PORTS               NAMES
47d83458b7f0        ubuntu                                      "/bin/bash"              2 minutes ago       Exited (0) 2 seconds ago                       my_ubuntu

Delete a container.

(base) zhoun14@zhoun14:~$ sudo docker container rm 152a75e1acb5

3. Commit & Push

We want to set up a new environment in the container from the latest pure ubuntu.

(base) zhoun14@zhoun14:~$ sudo docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
Status: Downloaded newer image for ubuntu:latest

Then we create a container and install some package. Say we install the sudo tool.

(base) zhoun14@zhoun14:~$ sudo docker run -it --name my_ubuntu ubuntu
root@c9961f450ef1:/# apt-get update && \
>       apt-get -y install sudo

We exit the container and commit it to an image.

(base) zhoun14@zhoun14:~$ sudo docker ps -a
CONTAINER ID        IMAGE                                       COMMAND                  CREATED             STATUS                            PORTS               NAMES
c9961f450ef1        ubuntu                                      "/bin/bash"              10 minutes ago      Exited (130) About a minute ago                       my_ubuntu

(base) zhoun14@zhoun14:~$ sudo docker commit --author "nanzhoumails@gmail.com" -m "add sudo" my_ubuntu nanzhoumails/ubuntu-images:pure-sudo-ubuntu

(base) zhoun14@zhoun14:~$ sudo docker images
REPOSITORY                          TAG                          IMAGE ID            CREATED             SIZE
nanzhoumails/ubuntu-images    pure-sudo-ubuntu    f279d4808a3d        4 seconds ago       115MB

We finally push the image to our private docker repository so that we can use it anywhere.

(base) zhoun14@zhoun14:~$ sudo docker push nanzhoumails/ubuntu-images:pure-sudo-ubuntu
The push refers to repository [docker.io/nanzhoumails/ubuntu-images]
7ebf7c42b39e: Pushed 
b57c79f4a9f3: Layer already exists 
d60e01b37e74: Layer already exists 
e45cfbc98a50: Layer already exists 
762d8e1a6054: Layer already exists 

Now we can see it in our docker hub.

4. Copy Files

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Leave a Reply

Your email address will not be published. Required fields are marked *