Wednesday, 30 June 2021

Beginner common mistakes with Docker container

Container run/re-run

When you create a container and then exit it. The container only stops but it still is lying there in stopped/Exited state. You cannot re-run a new container with the same name.

Running httpd container

[root@jkcli ~]# docker container run --name jkapache -p 8080:80 httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
b4d181a07f80: Pull complete
4b72f5187e6e: Pull complete
12b2c44d04b2: Pull complete
ef481fc2a03a: Pull complete
d483d945fcab: Pull complete

Later when i exit this container and try to run same container it will give me below error. 

[root@jkcli ~]# docker container run --name jkapache -p 8080:80 httpd -d

docker: Error response from daemon: Conflict. The container name "/jkapache" is already in use by container "28c5bc35be99e59baf5ce076bdfad683188b64d2e47d7483e8cfc86a764f832c". You have to remove (or rename) that container to be able to reuse that name.

see 'docker run --help'.

However if you check "docker container ls" command, it will show no container running. 

[root@jkcli ~]# docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@jkcli ~]# 


This is because docker container ls command will show only running containers at that point in time. 
we have to use -a to list all containers.


[root@jkcli ~]# docker container ls -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                          PORTS     NAMES
28c5bc35be99   httpd     "httpd-foreground"       2 minutes ago   Exited (0) About a minute ago             jkapache
1cb4b275776a   nginx     "/docker-entrypoint.…"   10 days ago     Exited (0) 10 days ago                    jk
[root@jkcli ~]#


Remove docker container
Now remove these docker container and then we can try running the docker like below,


[root@jkcli ~]# docker container ls -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
28c5bc35be99   httpd     "httpd-foreground"       15 minutes ago   Exited (0) 14 minutes ago             jkapache
1cb4b275776a   nginx     "/docker-entrypoint.…"   10 days ago      Exited (0) 10 days ago                jk
[root@jkcli ~]# docker container rm 28c5bc35be99
28c5bc35be99

Running docker container with detatch
[root@jkcli ~]# docker container run -d --name jkapache -p 8080:80 httpd
cb72f5d90279fcdef02b3715a04cec20043c23f10a8e548ac7a0cf4789640788
[root@jkcli ~]# docker container ls
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                                   NAMES
cb72f5d90279   httpd     "httpd-foreground"   4 minutes ago   Up 4 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   jkapache
[root@jkcli ~]#


No comments:

Post a Comment