Docker – Some Commands

To run the docker container with host port matching and with mounts

#docker run -d -p <host-ip>:<host-port>:<container-port> -v <host-path>:<container-path>

If you have volume(s) exposed from a container for e.g. container1 and want to have all the mounts on container2 use the “–volumes-from” while running container2.

ISSUE 1 : I was not able to share host mount to docker mount eventhough I am trying – “docker run -v : ” . Reading through https://forums.docker.com/t/docker-fails-to-mount-v-volume-from-nfs-mounted-directory/582/8 found that restarting docker service worked strangely.

ISSUE2:

when running a redhat (fedora/RH/CentOS) based distro with Selinux enforcing (default configuration) the volume configuration doesn’t work. Changes in the directory are not updated, connecting into the container and doing an “ls” command will return permission denied.

The fix is a patch merged into docker a couple of years ago that allows a z/Z to be placed at the end of the volume configuration. It copies the selinux permission “stuff” into the container file system.

Specifically this piece: “-v $PWD:/app”

Becomes: “-v $PWD:/app:Z”

 

To get to console of running container

Use Ctrl+p Ctrl+q to come out of console

#docker exec -ti  <container> /bin/bash

To get the host ports which docker ports are bind to- check DOCKER chain

Ref: https://docs.docker.com/engine/reference/commandline/inspect/

[root@ip-172-31-35-29 ~]# docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' 8d0d0a2e4539
 8153/tcp -> 32772  8154/tcp -> 32771
[root@ip-172-31-35-29 ~]#

Docker CLI – https://docs.docker.com/engine/reference/commandline/docker/#child-commands

Playing with networks

#docker run --net=<network> -d <container>
#docker network ls
#docker network create <network>
#docker network connect <network> <container>

Restart of containers

Flag Description
no Do not automatically restart the container. (the default)
on-failure Restart the container if it exits due to an error, which manifests as a non-zero exit code.
unless-stopped Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted.
always Always restart the container if it stops.

Below section gives the commands on how to clean up – delete exited containers and images and deleting the volumes.

[root@ip-172-31-4-29 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
51c5ddc6dabe        gocd-agent:latest   "/docker-entrypoint.s"   10 hours ago        Up 10 hours                             backstabbing_saha
[root@ip-172-31-4-29 ~]#
[root@ip-172-31-4-29 ~]#
[root@ip-172-31-4-29 ~]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
gocd-agent                 latest              763b559426e1        10 hours ago        613.2 MB
                                   4ae80d3a2da7        2 days ago          430.8 MB
gocd/gocd-agent-centos-7   v17.3.0             e6e5e4b7ce4e        12 days ago         429.1 MB
centos                     7                   98d35105a391        2 weeks ago         192.5 MB
[root@ip-172-31-4-29 ~]# docker stop 51c5ddc6dabe
51c5ddc6dabe
[root@ip-172-31-4-29 ~]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
gocd-agent                 latest              763b559426e1        10 hours ago        613.2 MB
                                   4ae80d3a2da7        2 days ago          430.8 MB
gocd/gocd-agent-centos-7   v17.3.0             e6e5e4b7ce4e        12 days ago         429.1 MB
centos                     7                   98d35105a391        2 weeks ago         192.5 MB
[root@ip-172-31-4-29 ~]# docker rmi 4ae80d3a2da7
Error response from daemon: conflict: unable to delete 4ae80d3a2da7 (must be forced) - image is being used by stopped container 14a569dafa76
[root@ip-172-31-4-29 ~]#

[root@ip-172-31-4-29 ~]#
[root@ip-172-31-4-29 ~]# docker ps -a
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS                        PORTS               NAMES
51c5ddc6dabe        gocd-agent:latest                  "/docker-entrypoint.s"   10 hours ago        Exited (143) 24 seconds ago                       backstabbing_saha
b454b0f8ec04        4ae80d3a2da7                       "/docker-entrypoint.s"   34 hours ago        Exited (143) 10 hours ago                         cranky_carson
5a30871811f5        4ae80d3a2da7                       "/docker-entrypoint.s"   34 hours ago        Exited (0) 34 hours ago                           lonely_poincare
9b6485c3e1f4        4ae80d3a2da7                       "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             trusting_murdock
dad5249bf3ca        4ae80d3a2da7                       "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             condescending_stallman
14a569dafa76        4ae80d3a2da7                       "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             sleepy_torvalds
8776afb9dfd6        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             determined_pare
f3cc3d9feadb        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             stupefied_engelbart
c7d0689ed31a        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             reverent_colden
e52babb81ab1        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             fervent_lumiere
2c36bc211ec9        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             awesome_montalcini
2b543f2e3f9d        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             cocky_ramanujan
a42588bf3702        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             nauseous_ardinghelli
1ff3a4eadf53        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             tender_lumiere
8bbd6dd24ec0        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             nauseous_payne
ba13010a14fd        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   2 days ago          Exited (0) 2 days ago                             goofy_swirles
d498ed39adc4        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   7 days ago          Exited (0) 2 days ago                             tiny_varahamihira
0be016d2384c        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   7 days ago          Exited (0) 7 days ago                             sleepy_joliot
57bdd36d916e        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   7 days ago          Exited (0) 7 days ago                             naughty_pare
de58b285e514        gocd/gocd-agent-centos-7:v17.3.0   "/docker-entrypoint.s"   7 days ago          Exited (0) 7 days ago                             sick_bassi
[root@ip-172-31-4-29 ~]# docker ps -aq -f status=exited
51c5ddc6dabe
b454b0f8ec04
5a30871811f5
9b6485c3e1f4
dad5249bf3ca
14a569dafa76
8776afb9dfd6
f3cc3d9feadb
c7d0689ed31a
e52babb81ab1
2c36bc211ec9
2b543f2e3f9d
a42588bf3702
1ff3a4eadf53
8bbd6dd24ec0
ba13010a14fd
d498ed39adc4
0be016d2384c
57bdd36d916e
de58b285e514
[root@ip-172-31-4-29 ~]# docker ps -aq --no-trunc | xargs docker rm
51c5ddc6dabe4b6572d278de9fe6532488ae7f62814186282602d03af3c26e77
b454b0f8ec04a50fa612a9934b83d868f8ebec9bc5f42b01bf49a28051e5035d
5a30871811f54358074a8a3652288d3fc3921a46e52fb25259eb631b29bf4b5e
9b6485c3e1f4ca56a2bc3a678df92209a98553f051752269c7dc0eb932634c39
dad5249bf3ca861a60bd20e19cd5be5003a4ea61e279f179dd8e9d6cf616b823
14a569dafa762f31bd8b6a65eed53b0eef403e03dec9df54acc0258b24447652
8776afb9dfd6594fa646ef34d7533f6eed6996b02fde7f7fdc5dee107d1b777d
f3cc3d9feadb6f0fb850541279f2936f9619134eebdb912f2b0f5d39efb43812
c7d0689ed31a01044e2d5687be39123b1be38be0d20a041ce43926de4e1baa0c
e52babb81ab15c6a9b3208e23b61d45eb03b1a1f52c84da80a30c827724f38cd
2c36bc211ec9a1219775530ebb3cea0bc853c92006be15181af8624ffd90ce57
2b543f2e3f9d5071c67698e24e3f1536ae8284266aa93a2cf3477a1430088f47
a42588bf3702f9368ffe15db0ec1b50e53149434d50b5e15616d8e0354b87b5e
1ff3a4eadf53dc3e5afc9461ee37743f272af928e8def06f4226df5bb7de5347
8bbd6dd24ec0ea0daec18edfcf7ded79ca8fd687f706fd59adcd459892089c02
ba13010a14fd692d0fa2ff9d22a2e452fde863abd7b48c15fdeff13dd6ec3659
d498ed39adc4c353559b903663d7c409a526afac34699a23ec0d8988ea7bcf6f
0be016d2384c453ff08dda701bfb99697aafd0200be13f246839ed1652fc8126
57bdd36d916ec90346a025166a0afa551456fbca82037d30e381e49faaa2b440
de58b285e514f05d32f530574eb18a68091be7e56656294498bd126b02017c3d
[root@ip-172-31-4-29 ~]# docker ps -aq -f status=exited
[root@ip-172-31-4-29 ~]#

Check dangling volumes and delete them

#docker volume ls -qf dangling=true
#docker volume rm $(docker volume ls -qf dangling=true)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s