Contents

Docker Basics

TL/DR;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$docker login
$docker pull <image>
$docker run -it <image> bash
$exit
$docker ps -a
$docker images
$docker restart
$docker exec -it <container> bash 
$docker commit <container> <image>
$docker tag <source> <target> $docker push <image>
$docker rmi <image>

Image and container

List docker images

1
docker images

List containers

1
docker ps -a

Start a container

1
2
3
4
docker run <image>

# example
docker run -v ~/repo/dhub/work:/work -p 8888:8888 --name my-env datascientistus/ds-python-env2

Stop a container

1
2
3
4
5
6
docker stop <container or container ID>

# example
docker stop my-env
# or
docker stop a8de52ea8c03

Remove a container

1
docker rm <container or container ID>

Restart a container

1
docker restart <container or container ID>

Run something with running docker image

1
docker run -it ubuntu bash

Run something (e.g. bash) on running container

1
2
3
4
docker exec -it <container or container ID> bash

# For example
docker exec -it 06ac5464b94f bash

Detatch: Ctrl + p + q

1
2
(Ctrl + p + q)
# read escape sequence

On the docker container,

  • exit: stop the container process
  • detach (Ctrl + p + q): the container process remains.

Attach: Revisit the detached process

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
docker attach <container or container ID>

## for example

% docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS          PORTS     NAMES
06ac5464b94f   ubuntu    "bash"    47 minutes ago   Up 32 minutes             sad_austin

% docker attach 06ac5464b94f
root@06ac5464b94f:/#

# exit stops the container
exit

% docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                       PORTS                    NAMES
06ac5464b94f   ubuntu              "bash"                   49 minutes ago   Exited (0) 11 seconds ago                             sad_austin

Exercise: restart, exec, exit, see images

Images

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 % docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                        PORTS                    NAMES
06ac5464b94f   ubuntu              "bash"                   52 minutes ago   Exited (137) 35 seconds ago                            sad_austin

% docker restart 06ac5464b94f
06ac5464b94f

# Below start the different process
% docker exec -it 06ac5464b94f bash
root@06ac5464b94f:/# 

# exit
exit

% docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED             STATUS                         PORTS                    NAMES
06ac5464b94f   ubuntu              "bash"                   About an hour ago   Up 2 minutes                                            sad_austin

Commit image

Save local image from a container

1
2
3
4
5
6
% docker commit 06ac5464b94f ubuntu:updated
sha256:c57dfbc430344f1a469821e20ed01589617e40c57fa58b71792f63b741cb088a

% docker images
REPOSITORY                       TAG         IMAGE ID       CREATED          SIZE
ubuntu                           updated     ubuntu                           latest      37f74891464b   4 weeks ago      69.2MB

Change docker repository name

As we created a repository named tatoflam/my-ubuntu on docker hub, we need to set the same name for the local image.

In this case,

1
% docker tag ubuntu:updated tatoflam/my-ubuntu

This adds the tag name for the same image:

1
2
3
4
% docker images
REPOSITORY                       TAG         IMAGE ID       CREATED          SIZE
ubuntu                           updated     c57dfbc43034   13 minutes ago   69.2MB
tatoflam/my-ubuntu               latest      c57dfbc43034   13 minutes ago   69.2MB

Tips:
The full image name is <hostname>:<port>/<username>/<repository>:<tag>

For example, registry-1.docker.io/library/ubuntu:latest

Push the docker images

1
2
3
4
5
6
% docker push tatoflam/my-ubuntu
Using default tag: latest
The push refers to repository [docker.io/tatoflam/my-ubuntu]
40fc4e8520dd: Pushed 
c5ca84f245d3: Mounted from library/ubuntu 
latest: digest: sha256:1a3c6fc4dc497d324ae307cb081a5ca3a07b083a1ae746ce206be376cb5004f5 size: 736

Remove the local image and pull the new one on the docker hub

Remove the docker image (with the specific image name)

1
2
3
4
% docker rmi tatoflam/my-ubuntu
Untagged: tatoflam/my-ubuntu:latest
Untagged: tatoflam/
my-ubuntu@sha256:1a3c6fc4dc497d324ae307cb081a5ca3a07b083a1ae746ce206be376cb5004f5

Check the image name removed (the original one remains)

1
2
3
4
% docker images
REPOSITORY                       TAG         IMAGE ID       CREATED          SIZE
ubuntu                           updated     c57dfbc43034   19 minutes ago   69.2MB
ubuntu                           latest      37f74891464b   4 weeks ago      69.2MB

Pull the pushed image from Dockerhub.

1
2
3
4
5
6
% docker pull tatoflam/my-ubuntu
Using default tag: latest
latest: Pulling from tatoflam/my-ubuntu
Digest: sha256:1a3c6fc4dc497d324ae307cb081a5ca3a07b083a1ae746ce206be376cb5004f5
Status: Downloaded newer image for tatoflam/my-ubuntu:latest
docker.io/tatoflam/my-ubuntu:latest

Check the image.

1
2
3
4
% docker images
REPOSITORY                       TAG         IMAGE ID       CREATED          SIZE
ubuntu                           updated     c57dfbc43034   19 minutes ago   69.2MB
tatoflam/my-ubuntu               latest      ubuntu                           latest      37f74891464b   4 weeks ago      69.2MB

What docker run does?

  • docker run
    • run = create(create container) + start (start container and run default command, then exit)
  • docker create
  • docker ps -a
  • docker start
  • docker start -a
    • docker start -a show the output of the default command.

What is docker run "-it"?

  • -i : input available
  • -t: display format pretty

Stop or(and) remove

  • Remove container
    • docker rm <container>
  • Stop container
    • docker stop <container>
  • Remove every stopped containers
    • docker system prune

Name container

1
docker run --name <name> <image>

for example

1
2
3
4
docker run --name sample_container ubuntu
tato@tmbp-2330 repo % docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
24778a850909   ubuntu    "/bin/bash"   15 seconds ago   Exited (0) 14 seconds ago             sample_container

Detached mode and foreground mode

  • detached mode : Detach container after run it (background)
    1
    
    $ docker run -d <image>
    
  • (short-term) foreground mode : Remove container after exit it (one-time container)
    1
    
    $ docker run --rm <image> 
    

For example

1
2
3
4
5
6
$ docker run -d --name detached_contaner ubuntu
8b26fbe30c045a9d31c0a2524f2bf1703c270006b9796fbe5795d0948f70c01a

$ docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
8b26fbe30c04   ubuntu    "/bin/bash"   12 seconds ago   Exited (0) 11 seconds ago             detached_contaner
1
2
3
4
5
$ docker run --rm --name removed_contaner ubuntu "echo" "helloworld"

$ docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
8b26fbe30c04   ubuntu    "/bin/bash"   2 minutes ago   Exited (0) 2 minutes ago             detached_contaner

No container running with --rm as the container is removed.